Simple Graphics Question
I'm new to Java, but have read many books on it. None of the books explains much about graphics, except for Paint(), Repaint() and Update(). I would like to start with a simple applet (non-swing) game in which created objects have the ability to draw themselves onscreen. Is there a simple way to do this that I'm missing? Must all graphic work be done in the main applet class's paint method? I'd like to see a short demo class that would create an object which paints a simple circle on the screen of the main applet.
Thanks in advance for any help. (if you have more time, I'd like to know if the main applet could then detect the drawn shape).
[665 byte] By [
duplcata] at [2007-9-27 23:26:37]

I'm sorry to tell this for you, but I don't think that there is any way to do what you are trying to implement. The pictures require something to be drawn on except if you are using bufferedstrategy and fullscreen(and even then the pictures require something to be drawn on)
So basically what you need to do is if you want animation
run()
-rendering loop
--paint(g);
--otherstuff significant for game();
-end rendering loop
paint(graphics g){
for (int i = 0; i<sprites.lenght;i++){
sprites.draw(g)
}
}
and there you have it. Of course there are billion examples where they show how to do double buffering, so therefore I'm not going to write it here.
}>
public class gameApplet extends Applet {
Graphics graphics
Sprite sprite;
public void init() {
graphics = getGraphics();
sprite = new Sprite(graphics);
sprite.start();
}
public void paint(Graphics g) {
graphics = g;
}
}
class Sprite implements Runnable {
Graphics g;
Thread runner;
public Sprite(Graphics graphics) {
g = graphics;
}
public void start() {
runner = new Thread(this);
runner.start();
}
public void run() {
while(true) {
g.drawString(0, 0, "Non-Component is drawing in my applet! What is the world coming to?");
try {
runner.sleep(100);
} catch(Exception e) {}
}
}
}
This is untested code, but I think it may get you moving in the direction you're looking for.
Hi, i think its better to have one running thread that controls all the sprites. At each run() interval it calls Sprite.update(howMuchTimeHasPassed) and Sprite.render(Graphics);In a normal game with a mabye 100 Sprites the system will overload with so many threads.Harley.