Results 1 to 4 of 4
  1. #1
    Bobbyloujo is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 2 instances of an object that draws an image

    I've been trying to teach myself Java and I've made some progress. I'm finally starting to understand the basics. Today I figured out how to make a class that draws an image to my JFrame. The JFrame is setup in the class that the program starts in, then an instance of the object that draws the image is created and placed in the JFrame. That all works fine and dandy, I can even move the image around if I so wish. Here's where I've run into problems: I tried to create two instances of the object at different positions. When I run the program only the second instance shows up. I have no idea why this is :/

    Here's the code for the class that my program starts in:
    Java Code:
    package javasprite;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame implements Runnable{
    	public static JFrame main;
    	public static Sprite sprite;
    	public static Sprite sprite2;
    	
    	
    	public Main(){
    		
    	}
    	
    	public static void main(String[] args){
    		sprite = new Sprite(0, 0);
    		sprite2 = new Sprite(122, 126);
    		main = new JFrame();
    		main.add(sprite);
    		main.add(sprite2);
    		main.setSize(200,200);
    		main.setVisible(true);
    		main.setFocusable(true);
    		main.setTitle("Sprite");
    		main.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Thread thread = new Thread(new Main());
    		thread.start();
    	}
    
    	@Override
    	public void run() {
    		
    		while(true){
    			sprite.repaint();
    			sprite2.repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    And for the "Sprite" object:
    Java Code:
    package javasprite;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class Sprite extends JPanel {
    	private Image img;
    	private int x;
    	private int y;
    
    	public Sprite(int x, int y){
    		this.x = x;
    		this.y = y;
    		try{
    			img = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("javasprite/character.png"));
    		} catch (IOException e){
    			
    		}
    	}
    
    //note that there is no "@Override" here. There isn't intended to be. This method is just for doing things that I want the object to do every frame.
    	public void update() {
    		x++;
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		g.drawImage(img, x, y, this);
    		update();
    	}
    }
    So, if this were a game, "Sprite" would be like a type of enemy. There could be more than one of this type of enemy so I should be able to create more than one instance of "Sprite". But when I try to make two, only one shows up :(

    I hope that's enough information. Can anyone help me out?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: 2 instances of an object that draws an image

    You've several problems, but the main one I see is that you're not taking layout managers into account when adding components to a container. Hint: JFrame contentPanes use BorderLayouts by default. This information and the tutorials should get you started towards solving this. Suggestion 1: don't use null layout but rather consider changing to another layout. Suggestion 2: even if you don't use an @Override annotation, if your method has the same "signature" as a parent method, the parent's method will be overridden. I urge you to rename your update method for this reason. Suggestion 3: don't put program logic within a paint or paintComponent method. Use a Swing Timer to advance your variable and to call repaint(). Don't do this in paint or paintComopnent. Suggestion 4: read the Swing painting tutorials to see why you should draw in the JPanel's paintComponent method not its paint method.

  3. #3
    Bobbyloujo is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: 2 instances of an object that draws an image

    Thank you for the reply. I followed you suggestions by renaming my update method to "step" and moving everything from my paint method to paintComponent. I read the tutorials on painting and layouts but I'm just totally lost. I'm trying to create an object that acts like a video game character, it creates an image of the character that can be moved about the screen. For this I would need to be able to position my character using absolute coordinates. I have no idea which layout would be best to accomplish this :/ I still can't get two characters to spawn. I tried setting boundaries for the JPanel based off the the x and y coordinates of the character but that still only showed one character. I feel so helpless haha. Any other hints?

  4. #4
    Bobbyloujo is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: 2 instances of an object that draws an image

    Sorry for double posting, I couldn't figure out how to edit my old post.

    I've found a solution so I figured I'd post it in case someone finds this thread in a Google search or something. What I didn't understand was a that a JPanel is like a sheet of paper. Things can be drawn onto it. Also, the paper is not transparent. I believe my problem was that with each sprite I created I drew them to a new piece of paper which overlapped and covered up the other one, making it not visible. I remedied this by created a new class which I called "Screen" and used it as my sheet of paper. My "Sprite" class now extends rectangle. In the Sprite class I created a "draw(Graphics g)" method and put my "g.drawImage([arguments]);" in there. Then I went back to my Screen class and in its paintComponent method I made it call the draw(g) method of each instance of Sprite.

    I think I understand what Fubarable was saying about the layouts. I could use this to organize my JPanels if I had more than one and I want them to be side by side, for example.

Similar Threads

  1. Multiple object instances in array of objects
    By Drizzt in forum New To Java
    Replies: 6
    Last Post: 01-12-2012, 05:07 PM
  2. Preventing duplicate instances of an object
    By monkeyhead in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 08:28 PM
  3. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:10 PM
  4. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:13 PM
  5. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-08-2008, 12:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •