Paint Question
Hello,
I'm taking Java currently, so I'm pretty new to the paint function. I'm trying to make a tiled background to display. It's pretty simple but how do I get it to paint on the frame? "this" wouldn't work. All I got was an error. The code is included below. <?> is the part I'm trying to figure out what to replace with. Thanks!
import java.awt.*;
import javax.swing.*;
public class Rpg extends JFrame
{
public static void main(String[] args)
{
new Rpg();
}
public Rpg()
{
Frame FRAME = new Frame("RPG");
FRAME.setSize(600, 500);
FRAME.show();
new World();
}
}
class MAP_STRUCTURE
{
int type; // type of ground
}
class World
{
private ImageIcon Tile;
public World()
{
MAP_STRUCTURE map[][] = new MAP_STRUCTURE[10][10];
int counter = 0;
for(int a = 0; a <= 10; a ++)
for(int b = 0; b <= 10; b ++)
map[a].type = 1 + (int) (Math.random() * 2);
for(int a = 0; a <= 10; a ++)
for(int b = 0; b <= 10; b ++)
LoadTile(map[a].type);
}
private void LoadTile(int type)
{
switch (type)
{
case 1: Tile = new ImageIcon("grass.gif"); break;
case 2: Tile = new ImageIcon("road.gif"); break;
}
}
public void paint(Graphics g, Frame FRAME)
{
for(int a = 0; a <= 10; a ++)
for(int b = 0; b <= 10; b ++)
Tile.paintIcon(<?>, g, a, b);
}
}
[1580 byte] By [
eienxuea] at [2007-10-2 2:08:22]

You're mixing Swing and AWT. Don't do that unless you really know what you're doing. In particular I see no need in your code to extend JFrame.
You're trying to call graphics routines from a class that isn't part of either the Swing or AWT class hierarchy. World should either subclass Component or be invoked by something that does, like:
import java.awt.*;
public class Rpg extends Canvas {
private World w;
public static void main(String[] args) {
Frame f = new Frame("RPG");
f.add(new Rpg());
f.pack();
f.show();
}
public Rpg() {
setSize(600, 500);
w = new World();
}
public void paint(Graphics g) {
w.drawSelf(g);
}
}
Then you write drawSelf in your World class. The Rpg's paint method is called by the GUI thread because it's a component that's been inserted into the component hierarchy.
Please follow Java naming conventions. The only things that should be in ALL-CAPS are constants, which are declared as static final fields. Methods, fields, and local variables should start with a lower-case letter and be in mixedCase.
On these forums, please wrap code in [code][/code] tags so it's legible.
Wow, I totally did not think of that. Sorry about messing up with the naming conventions. So far I worked out most of the errors and bugs, but I'm lost about how to paint to the canvas from another class.
The problem is in drawSelf, where it says "<What Should Go here?>"
import java.awt.*;
public class Rpg extends Canvas {
private World w;
public static void main(String[] args) {
Frame f = new Frame("RPG");
f.add(new Rpg());
f.pack();
f.show();
}
public Rpg() {
setSize(600, 500);
w = new World();
}
public void paint(Graphics g) {
w.drawSelf(g);
}
}
class World {
private Image tile;
private Map_Structure map[][] = new Map_Structure[10][10];
public World() {
for(int a = 0; a <= 10; a ++)
for(int b = 0; b <= 10; b ++)
map[a][b].type = 1 + (int) (Math.random() * 2);
}
private void loadTile(int type) {
switch (type) {
case 1: tile = Toolkit.getDefaultToolkit().getImage("grass.gif"); break;
case 2: tile = Toolkit.getDefaultToolkit().getImage("road.gif"); break;
}
}
public void drawSelf(Graphics g) {
for(int a = 0; a <= 10; a ++)
for(int b = 0; b <= 10; b ++) {
loadTile(map[a][b].type);
g.drawImage(tile, a, 16 * b, <What Should Go here?>);
}
}
}
class Map_Structure {
int type; // type of tile
}
I've tried a few methods but they didn't work. I've also search the forums as well. Thanks
Apparently you can just put null there if you don't want to do any ImageObserving.You probably shouldn't put loadImage in the drawSelf method. Put it in the constructor or someplace like that, so you only load each image once.
Thank you so much. I got it to work after fixing my code (at first null gave me errors).
Just something I noticed, when I run the program, I see a white screen. However, when I resize the window, the map shows up. Should I use a repaint function on a timer to refresh the map every second or so? Is there a way to just refresh what is within the window?
Thanks.
You're saying that the window is OK, but suddenly goes white, and stays that way until you resize?
I mean like when I run the program. There's the frame with a white background. There's nothing on it. However, when I resize it or minimize it then maximize it, the tiles (the images that I drew on the canvas) show up. I guess I'm saying that the canvas never "updates" after being drawn on.I played around with update() but I kept on getting overflow errors which was rather weird (I probably made it call update() too many times per second)...but it did fix the problem.
Actually...there's this issue with the GUI loading images and trying to display before the loading is finished. You can use a java.awt.MediaTracker to pause execution until the images are loaded. The fact that you're loading in your paint method could cause that. I'm guessing that's the cause of the problem.
If you're getting an overflow error, I'm guessing that you're somehow calling update() recursively without an end condition.
You can add repaint() but personally I'd do that as a last resort. It's a bit of a kluge, IMHO. Also in general you want to reduce the amount of GUI events, to keep the GUI snappy.