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
Code :
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:
Code :
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.
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:
Code :
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));
}
}
}
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.
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.
Re: Drawing Objects on Screen
Quote:
Originally Posted by
Gravity Games
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.
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?
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.
Re: Drawing Objects on Screen
Ah, thank you. I had forgot to use .get to access the components. That was the problem.