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.

Results 1 to 8 of 8

Thread: Drawing Objects on Screen

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

    Default Drawing Multiple Objects on Screen at once

    For some reason, I'm having a hard time drawing objects on screen. Here's my code:

    Level.java
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.util.*;
    import java.io.File;
     
    public class Level {
    	public Tile tile;
    	private Scanner s;
    	private int objectId;
    	private int spx;
    	private int spy;
    	private String oidstr;
    	private String xstr;
    	private String ystr;
    	private ArrayList<String> objarray= new ArrayList<String>();
    	private ArrayList<String> xarray= new ArrayList<String>();
    	private ArrayList<String> yarray= new ArrayList<String>();
    	public Level(){
    		setStage();
    	}
     
    	public void tick(){
     
    	}
     
    	public void render(Graphics g){
    	}
     
    	public void setStage(){
    		tile=new Tile(0,0);
    		tile=new Tile(0,0);
    		}
    	}
    }

    Tile.java:
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
     
    import javax.swing.JFrame;
     
    public class Tile extends Rectangle{
    	private static final long serialVersionUID=1L;
    	public Tile(int x, int y){
    		this.x=x;
    		this.y=y;
    	}
    	public void render(Graphics g){
    		g.setColor(Color.red);
    		g.drawRect(x,y,16,16);
    	}
    }

    The problem is that the Tile object will only be created at the last instance of "tile=new Tile". I can't seem to figure out how to create them both. Just writing new Tile doesn't work either, as the objects won't draw.
    Last edited by Gravity Games; August 27th, 2012 at 11:36 AM. Reason: I changed the title to be more descriptive
    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
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing Objects on Screen

    I figured out I need an arraylist to create more than one of the same object. Unfortunately, I can't figure out how to call the render method on each individual object. Here's what I'm got:

    Level.java:
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.util.*;
    import java.io.File;
     
    public class Level {
    	public ArrayList<Tile> tile=new ArrayList<Tile>();
    	private Scanner s;
    	private int objectId;
    	private int spx;
    	private int spy;
    	private String oidstr;
    	private String xstr;
    	private String ystr;
    	private ArrayList<String> objarray= new ArrayList<String>();
    	private ArrayList<String> xarray= new ArrayList<String>();
    	private ArrayList<String> yarray= new ArrayList<String>();
    	public Level(){
            setStage();
    	}
     
    	public void tick(){
     
    	}
     
    	public void render(Graphics g){
    		for(int d=0;d<tile.size();d++){
    			tile<Tile>.render(g);
    		}
    	}
    	public void setStage(){
    		tile.add(new Tile(0,0));
    		tile.add(new Tile(0,0));
                    }
    	}
    }
    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)

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Drawing Objects on Screen

    You would draw your image in the paintComponent(...) method of a JPanel or other JComponent derived class. You could loop through your ArrayList in this paintComponent method.

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

    Default Re: Drawing Objects on Screen

    How would I do that? Level.java's draw method is called by a JPanel if thats what you mean.
    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)

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Drawing Objects on Screen

    Quote Originally Posted by Gravity Games View Post
    How would I do that? Level.java's draw method is called by a JPanel if thats what you mean.
    Have you gone through the Swing graphics/drawing tutorial? I'm meaning nothing more than is described there.

    Your levels are held by a class derived from JComponent correct? Simply draw the level in that object's paintComponent method. The background image would likely be in a BufferedImage, and the sprites would be drawn individually by going through the array list.

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

    Default Re: Drawing Objects on Screen

    Oh, I'm already doing that (I had no problem when drawing one object), the problem is defining the individual objects of the arraylist. For example:

    The array list contains (Object 1, object 2, object 3)

    and the loop needs to do this:

    object1.render
    object2.render
    object3.render

    How do I call the objects in the ArrayList of Tile objects?
    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)

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Drawing Objects on Screen

    In the paintComponent method, loop through the ArrayList calling render on the components in the list, passing in the Graphics object.

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

    Default Re: Drawing Objects on Screen

    Ah, thank you. I had forgot to use .get to access the components. That was the problem.
    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)

Similar Threads

  1. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  2. Drawing in Full Screen
    By MizukiTHPS in forum AWT / Java Swing
    Replies: 7
    Last Post: March 27th, 2012, 11:48 AM
  3. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  4. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM
  5. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM