Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: Components?

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Components?

    Hi, Gravity games here, and I was trying to add an object to my JPanel. However, for some reason it says I need to specify a component. Whats a component and how do I use it to add my object?

    (Sorry if this is a n00by question, I'm still learning Java...)
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Components?

    Component is a class in the standard Java API. It's the basis of all AWT and Swing gui elements. See: Java Component Javadoc.

    What the compiler is telling you is that you need to add an object which is or inherits the Component class. Basically, anything which is compatible with the AWT or Swing GUI toolkits.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    Even when I extend component, the object won't get spawned by the JPanel. What should I do?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    Please post the full text of the error message and the code that is causing it.
    BTW The class is named: Component not component.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    The line that throws an error:

    add(GroundTileATop(spx,spy));

    Error Eclipse gives in the console:
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    The method GroundTileATop(int, int) is undefined for the type MainJPanel
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    The method GroundTileATop(int, int) is undefined for the type MainJPanel
    The compiler can not find a definition for the GroundTileATop() method.
    Check the spelling and the arguments.
    Is it a method that returns an object that extends the Component class?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    No, GroundTileATop IS the object. Should I try returning the object instead?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    Can you post the definitional statement(s) for GroundTileATop?
    I'm not sure what you mean when you say it is the object. The statement with the error is coded like a method call. Method calls use the (..) syntax after the name.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    Alright, here's the code of GroundTileATop:
    package scorpioengine.main;
     
    import java.awt.*;
     
    public class GroundTileATop extends Tile{
        int width = 32;
        int height = 32;
    	public GroundTileATop(int x, int y) {
    		super(x, y);
        }
     
        void draw(Graphics g) {
            g.setColor(Color.RED);
            g.fillRect(getX(), getY(), width, height);
        }
    }
    ...and for Tile...
    package scorpioengine.main;
     
    import java.awt.Component;
    import java.awt.Graphics;
     
    public abstract class Tile extends Component{
        private int x, y;
     
        public Tile(int x, int y){
            this.x = x;
            this.y = y;
        }
     
        public int getX() {
            return x;
        }
     
        public int getY() {
            return y;
        }
     
        abstract void draw(Graphics g); 
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Components?

    Quote Originally Posted by Gravity Games View Post
    No, GroundTileATop IS the object. Should I try returning the object instead?
    GroundTileATop is a class, not an object (nor a method...the code you posted shows it being used as one would call a method). It seems you are trying to create an instance of that class. Thus, you must create it using the new operator See Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    eg
    add(new GroundTileATop(spx,spy));

    ...although, I'm not sure why you are extending Component, when you don't use or override any of its methods.

  11. #11
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    Wow, I can't believe I made such a dumb mistake...now it doesn't crash, but I can't get the object to show up properly. What now?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  12. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Components?

    What now?
    Post code. If "add(new GroundTileATop(spx,spy));" is failing to have the visual effect you expect in your gui, then we really need to see the code of which it is part.

  13. #13
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    EDIT: Duplicate post for some reason, I guess I must have double clicked submit or something.
    Last edited by Gravity Games; August 4th, 2012 at 08:35 AM. Reason: Duplicate Post
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  14. #14
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    Well, here's the full JPanel Code:
    package scorpioengine.main;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.util.*;
     
    import javax.swing.*;
    //These import lines assure that all needed Java files are imported into the game.
    //The "*"s tell the game to import the entire package of files.
     
    import scorpioengine.main.intro.IntroJpanel;
     
    public class MainJPanel extends JPanel implements ActionListener, Runnable{
     
    	public static GroundTileATop GroundATop;
    	public static int objectsSpawned=0;
     
    	private int OW;
    	private int character=1;
    	private int characters=2;
    	private int cutscene;
    	private int forcedcharacter;
    	private boolean downpressed=false;
    	public boolean running=true;
    	private int keyDown=KeyEvent.VK_DOWN;
    	private int keyLeft=KeyEvent.VK_LEFT;
    	private int keyRight=KeyEvent.VK_RIGHT;
    	private boolean uppressed=false;
    	private int keyUp=KeyEvent.VK_UP;
    	public boolean selectpressed=false;
    	public int keySelect=KeyEvent.VK_Z;
    	private int objectId;
    	private int spx;
    	private int spy;
    	private Image A,B,C,D,E,F,G,H,I,K,L,M,N,O,P,Q,R,S,T,U,V,Y,Point,ChompTilesBG,PointerL,PointerR,ChompPortrait,chomppointer,eggplantpointer;
    	public int menuoption=1;
    	public int optionsoption=1;
    	public int quikplay=0;
    	public int gamestate=1;
    	public String GroundTileATop = "GroundTileATop";
     
    	private Map m;
    	private MapPointer mp;
    	private Level l;
    	private Collection<Tile> elements = new ArrayList<Tile>();
    	private Scanner s;
    	private String currenttile;
    	public Thread game;
     
    	public MainJPanel(){
    		//game controls
    		l = new Level();
    		m = new Map();
    		mp = new MapPointer();
     
    		File();
    		game = new Thread(this);
    		game.start();
     
    		addKeyListener(new KeyAdapter(){
     
    			public void keyPressed(KeyEvent e){
    				int keycode=e.getKeyCode();
     
    				if(keycode==keyDown){
    					if(gamestate==1){
    					if (menuoption<4){
    						downpressed=true;
    						menuoption+=1;
    					}
    					else{
    						menuoption=1;
    						downpressed=true;
    					}
    					}
    					if(gamestate==7){
    						if(m.getMap(mp.getTileX(),mp.getTileY()+1).equals("1")){
    						mp.movement(0, 1);
    						}
    					}
    				}
    				if(keycode==keyUp){
    					if(gamestate==1){
    					if (menuoption>1){
    					    uppressed=true;
    						menuoption-=1;
    					}
    					else{
    						menuoption=4;
    						uppressed=true;
    					}
    					}
    					if(gamestate==7){
    						if(m.getMap(mp.getTileX(),mp.getTileY()-1).equals("1")){
    						mp.movement(0, -1);
    						}
    					}
    				}
    				if(keycode==keyLeft){
    					if(gamestate==2){
    						if (character>=1){
    							character-=1;
    						}
    						if (character<1){
    							character=characters;
    						}
    					}
    					if(gamestate==7){
    						if(m.getMap(mp.getTileX()-1,mp.getTileY()).equals("1")){
    						mp.movement(-1, 0);
    						}
    					}
    				}
    	if(keycode==keyRight){
    		if(gamestate==2){
    			if (character<=characters){
    				character+=1;
    			}
    			if (character>characters){
    				character=1;
    			}
    		}
    		if(gamestate==7){
    			if(m.getMap(mp.getTileX()+1,mp.getTileY()).equals("1") || m.getMap(mp.getTileX()+1,mp.getTileY()).equals("2")){
    			mp.movement(1, 0);
    			}
    		}
    				}
    				if(keycode==keySelect && !selectpressed){
    	if(gamestate==1){
    					if(menuoption==1){
    						selectpressed=true;
    					gamestate=2;
    					}
    					if(menuoption==2){
    						selectpressed=true;
    						gamestate=3;
    					}
    					if(menuoption==3){
    						selectpressed=true;
    						gamestate=4;
    					}
    					if(menuoption==4){
    						selectpressed=true;
    						gamestate=5;
    					}
    				}
    	if(gamestate==2 && !selectpressed){
    		selectpressed=true;
    		cutscene=0;
    		gamestate=6;
    				}
    if(gamestate==6 && !selectpressed){
    	gamestate=7;
    	selectpressed=true;
    }
    if(gamestate==7 && !selectpressed){
    	if(m.getMap(mp.getTileX(),mp.getTileY()).equals("1")){   //this controls which tiles act as stage tiles so the game will switch to level mode
    		gamestate=8;
    		selectpressed=true;
    }
    }
     
    		}
    			}
    			public void keyReleased(KeyEvent e){
    	if(e.getKeyCode()==keyDown){
    					downpressed=false;
    				}
    				if(e.getKeyCode()==keyUp){
    					uppressed=false;
    				}
    				if(e.getKeyCode()==keySelect){
    					selectpressed=false;
    				}
    			}
     
    			public void keyTyped(KeyEvent e){
    				int keycode=e.getKeyCode();
     
    			}
     
    			});
     
     
    		setFocusable(true);
     
    		ImageIcon img = new ImageIcon("C://CWWGFX//8x8ALarge.PNG");    //Filepath to the "A" of the main font
    		A=img.getImage();
    		img = new ImageIcon("C://CWWGFX//8x8BLarge.PNG");    //Filepath to the "B" of the main font
    		B=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8CLarge.PNG");    //Filepath to the "C" of the main font
    		C=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8DLarge.PNG");    //Filepath to the "D" of the main font
    		D=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8ELarge.PNG");    //Filepath to the "E" of the main font
    		E=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8FLarge.PNG");    //Filepath to the "F" of the main font
    		F=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8GLarge.PNG");    //Filepath to the "F" of the main font
    		G=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8HLarge.PNG");    //Filepath to the "H" of the main font
    		H=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8ILarge.PNG");    //Filepath to the "I" of the main font
    		I=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8KLarge.PNG");    //Filepath to the "K" of the main font
    		K=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8LLarge.PNG");    //Filepath to the "L" of the main font
    		L=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8MLarge.PNG");    //Filepath to the "M" of the main font
    		M=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8NLarge.PNG");    //Filepath to the "N" of the main font
    		N=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8OLarge.PNG");    //Filepath to the "O" of the main font
    		O=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8PLarge.PNG");    //Filepath to the "P" of the main font
    		P=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8QLarge.PNG");    //Filepath to the "Q" of the main font
    		Q=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8RLarge.PNG");    //Filepath to the "R" of the main font
    		R=img.getImage();
    		img = new ImageIcon("C://CWWGFX//8x8SLarge.PNG");    //Filepath to the "S" of the main font
    		S=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8TLarge.PNG");    //Filepath to the "T" of the main font
    		T=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8ULarge.PNG");    //Filepath to the "U" of the main font
    		U=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8VLarge.PNG");    //Filepath to the "V" of the main font
    		V=img.getImage();
    		img=new ImageIcon("C://CWWGFX//8x8YLarge.PNG");    //Filepath to the "Y" of the main font
    		Y=img.getImage();
    		img=new ImageIcon("C://CWWGFX//OptionTriangleLarge.PNG");    //Filepath to the option triangle of the main menu
    		Point=img.getImage();
    		img=new ImageIcon("C://CWWGFX//ChompTilesBG.PNG");    //Filepath to the Chomp portrait of the character select
    		ChompTilesBG=img.getImage();
    		img=new ImageIcon("C://CWWGFX//Pointertriangleleft.PNG");    //Filepath to the left arrow of the character select
    		PointerL=img.getImage();
    		img=new ImageIcon("C://CWWGFX//Pointertriangleright.PNG");    //Filepath to the right arrow of the character select
    		PointerR=img.getImage();
    		img=new ImageIcon("C://CWWGFX//ChompPortraitLarge.PNG");    //Filepath to the Chomp portrait of the character select
    		ChompPortrait=img.getImage();
    		img = new ImageIcon("C://CWWGFX//ChompPointerLarge.png");
    		chomppointer=img.getImage();
    		img = new ImageIcon("C://CWWGFX//EggplantPointerLarge.png");
    		eggplantpointer=img.getImage();
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		repaint();
    	}
     
    	public void paint(Graphics g){ //this is the graphics subroutine
    		super.paint(g);
    		if(gamestate==1){
    		if(menuoption==1){
    		g.drawImage(Point,14,32,null);
    		}
    		if(menuoption==2){
    			g.drawImage(Point,14,64,null);
    		}
    		if(menuoption==3){
    			g.drawImage(Point,14,96,null);
    		}
    		if(menuoption==4){
    			g.drawImage(Point,14,128,null);
    		}
     
    		g.drawImage(S,32,32,null);
    		g.drawImage(T,48,32,null);
    		g.drawImage(A,64,32,null);
    		g.drawImage(R,80,32,null);
    		g.drawImage(T,96,32,null);
     
    		g.drawImage(L,32,64,null);
    		g.drawImage(O,48,64,null);
    		g.drawImage(A,64,64,null);
    		g.drawImage(D,80,64,null);
     
    		g.drawImage(O,32,96,null);
    		g.drawImage(P,48,96,null);
    		g.drawImage(T,64,96,null);
    		g.drawImage(I,80,96,null);
    		g.drawImage(O,96,96,null);
    		g.drawImage(N,112,96,null);
    		g.drawImage(S,128,96,null);
     
    		g.drawImage(E,32,128,null);
    		g.drawImage(D,48,128,null);
    		g.drawImage(I,64,128,null);
    		g.drawImage(T,80,128,null);
    		g.drawImage(O,96,128,null);
    		g.drawImage(R,112,128,null);
    		}
    		if (gamestate==2){
    			g.drawImage(PointerL,132,224,null);
    			g.drawImage(PointerR,398,224,null);
    			if(character==1){
    			g.drawImage(ChompPortrait,220,150,null);
    			g.drawImage(C,246,280,null);
    			g.drawImage(H,262,280,null);
    			g.drawImage(O,278,280,null);
    			g.drawImage(M,294,280,null);
    			g.drawImage(P,310,280,null);
    			}		
    			if(character==2){
    				g.drawImage(M,220,150,null);
    				g.drawImage(N,246,280,null);
    				g.drawImage(O,262,280,null);
    				g.drawImage(L,278,280,null);
    				g.drawImage(L,294,280,null);
    				g.drawImage(L,310,280,null);
    				}		
    		}
    		if (gamestate==4){
    			g.drawImage(ChompTilesBG,0,0,null);
    			if (optionsoption==1){
    			g.drawImage(Point,14,32,null);
    			}
    			g.drawImage(Q,32,32,null);
    			g.drawImage(U,48,32,null);
    			g.drawImage(I,64,32,null);
    			g.drawImage(K,80,32,null);
    			g.drawImage(P,112,32,null);
    			g.drawImage(L,128,32,null);
    			g.drawImage(A,144,32,null);
    			g.drawImage(Y,160,32,null);
    			if (quikplay==0){
    				g.drawImage(O,192,32,null);
    				g.drawImage(F,208,32,null);
    				g.drawImage(F,224,32,null);
    			}
    			if (quikplay==1){
    				g.drawImage(O,192,32,null);
    				g.drawImage(N,208,32,null);
    			}
    			g.drawImage(K,32,64,null);
    			g.drawImage(E,48,64,null);
    			g.drawImage(Y,64,64,null);
    			g.drawImage(C,96,64,null);
    			g.drawImage(O,112,64,null);
    			g.drawImage(N,128,64,null);
    			g.drawImage(F,144,64,null);
    			g.drawImage(I,160,64,null);
    			g.drawImage(G,176,64,null);
     
    			g.drawImage(L,32,96,null);
    			g.drawImage(I,48,96,null);
    			g.drawImage(V,64,96,null);
    			g.drawImage(E,80,96,null);
    			g.drawImage(S,96,96,null);
     
    			g.drawImage(B,32,128,null);
    			g.drawImage(O,48,128,null);
    			g.drawImage(S,64,128,null);
    			g.drawImage(S,80,128,null);
    			g.drawImage(I,112,128,null);
    			g.drawImage(N,128,128,null);
    			g.drawImage(T,144,128,null);
    			g.drawImage(R,160,128,null);
    			g.drawImage(O,176,128,null);
    		}
    		if (gamestate==6){
    			g.drawImage(L,310,280,null);
    		}
    		if (gamestate==7){
    			for(int y = 0; y < (m.getmapsizey()); y++){
    				for(int x = 0; x < m.getmapsizex(); x++){
    					if(m.getMap(x,y).equals("g")){
    						g.drawImage(m.getGrassEK(),x*32,y*32,null);
    					}
    					if(m.getMap(x,y).equals("t")){
    						g.drawImage(m.getGrassEKTop(),x*32,y*32,null);
    					}
    					if(m.getMap(x,y).equals("w")){
    						g.drawImage(m.getWaterEK(),x*32,y*32,null);
    					}
    				}
    			}
    			for(int y = (m.getmapsizey()+1); y < (m.getmapsizey()*2); y++){
    				for(int x = 0; x < m.getmapsizex(); x++){
    					if(m.getMap(x,y).equals("1")){
    						g.drawImage(m.getPath1(),x*32,(y-14)*32,null);
    					}
    					if(m.getMap(x,y).equals("2")){
    						g.drawImage(m.getPath2(),x*32,(y-14)*32,null);
    					}
    				}
    			}
    			if (character==1){
    			g.drawImage(chomppointer,mp.getTileX()*32,(mp.getTileY()-15)*32,null);
    			}
    			if (character==2){
    				g.drawImage(eggplantpointer,mp.getTileX()*32,(mp.getTileY()-15)*32,null);
    				}
    		}
    		if (gamestate==8){
    			add(new GroundTileATop(spx,spy));
    		}
    	}
     
    public void File(){
     
    		try{
    		s = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		while(s.hasNextLine()){
    			addElements(s.nextLine());
    		}
    		s.close();
    		}catch(Exception e){
    			System.out.println("error loading map2");
    		}
     
    	}
    public void fpsSetter() {
    	try{game.sleep(16);
    	} catch(Exception e){
    		e.printStackTrace();
    	}
    }
     
    public void run() {
    	while (running){
    	fpsSetter();
    	repaint();
    	}
    }
    public void addElements(String line) {
    	Scanner scanner = new Scanner(line);
    	objectId = scanner.nextInt();
    	spx = scanner.nextInt();
    	spy = scanner.nextInt();
    	System.out.println("Object: " + objectId + " At: " + spx + "," + spy);
    }
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  15. #15
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Components?

    Does the game actually get into state 8? Check by using System.out.println(...) in your paint() method. Do you really mean to create and add more and more of these components every time the panel paints?

    I haven't looked too closely, but I think using a timer to trigger the repaints would make more sense than extending runnable. It's described in Oracle's Tutorial. The Swing section might also prove valuable for its discussion of painting and layout.

  16. #16
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    Yeah it gets into state 8, and I'm not using a timer because at one point it worked fine and then at another randomly threw an error. Also, I only want to draw the objects once, and I'll use a seperate method once I get the add object to actually work.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  17. #17
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Components?

    it gets into state 8
    How big is the newly added component? And where is it? Use getBounds() in the paint() method to find out.

    I'm not using a timer because at one point it worked fine and then at another randomly threw an error.
    I can only speak for myself, but the Swing engineers know a lot more about threading than I do. If I wanted regular repaints I'd use the mechanisms they provide. And if I encountered a randomly occurring error I'd address that (eliminate the "randomly", then fix the error.)

    -----

    I've only just noticed that you are extending Component, not JComponent. There's no rule against that (adding instances of Component to a JPanel) but its perhaps more straightforward to stick to the Swing classes. I'm also conscious of copeg's remark earlier about the subclassing: you aren't adding any behaviour.

    The component you add has a draw() method, but I can't see anywhere that you call it.

  18. #18
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    How should I call the draw method then? Just use GroundTileATop.g.draw?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    How should I call the draw method then? Just use GroundTileATop.g.draw?
    You need to study how to call a method and pass it an argument. What you have coded is NOT a method call. See the tutorial:
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    I thought classes and objects were interchangable...well I guess that's the kind of thing a bad tutorial can do to you...well I'm making the object part of the JPanel class for simplicity, but I can only draw one (i'm assuming because the spx and spy variables change too quickly for all the integers to be registered. How would I get all the objects to show? I can only get the last one in the list to show.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    spx and spy variables change too quickly for all the integers to be registered.
    What does "registered" mean? How does that relate to the values of the two variables?
    I can only get the last one in the list to show.
    There are many ways that can happen.
    One is that all the references in the list point to the same object. Check that you make new objects each time you add one to the list.

    Try debugging the code by printing out each item as you take it from the list to show to see if they are different.


    Otherwise you will have to post the code so it can be tested to see the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    I already have printlines showing each variable, and each one shows, but I think the problem is that the first integer is replaced a frame later. I don't want to use arrays since they're stuck at the initial size you set them at, would arraylists work?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Components?

    ArrayLists would work for most places that you would use an array.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Components?

    Quote Originally Posted by Gravity Games View Post
    ... I don't want to use arrays since they're stuck at the initial size you set them at, would arraylists work?
    Just for clarity, it is possible to resize an array, so to speak, by creating a new, larger array and dumping the old array into it.
    Another note on the subject, if you know for sure your largest level will be of size S, you can just create an array of size S and use it for any level.
    Not saying either of these notes are the way to go, just adding on some understanding.

  25. #25
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Components?

    ...and of course ANOTHER problem comes up. Now I can't get the game to read a certain line of an ArrayList...

    What am I doing wrong here?
    g.fillRect(Integer.parseInt(xarray[a]), Integer.parseInt(yarray[a]), 32, 32);
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

Page 1 of 2 12 LastLast

Similar Threads

  1. Layout components...help please
    By alphasil in forum AWT / Java Swing
    Replies: 6
    Last Post: January 26th, 2012, 08:28 AM
  2. Components I add to JApplet won't appear!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2012, 11:47 AM
  3. Laying out components.
    By newbie in forum AWT / Java Swing
    Replies: 1
    Last Post: August 14th, 2011, 04:37 AM
  4. how to position gui components
    By programmer1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 11th, 2011, 12:45 PM
  5. Interaction between components.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2011, 07:32 PM