Original Post
Okay I know that to some of you this may seem like a lot to be working on all at once. As a matter of fact my abilities with 2D graphics in Java suck. This is why I started a simple graphics program to draw 2 rectangles to the screen. First of, I can't figure out why this is not working. Here's my code: ------------------------------ main.java -----------------------------
import java.awt.*;
import java.applet.*;
public class MainDr extends Applet implements Runnable
{
Thread drawThread = new Thread();
boolean running = false;
Animate draw;
public void init()
{
Animate draw = new Animate();
setBackground(Color.darkGray);
drawThread = new Thread(this);
running = true;
drawThread.start();
System.out.println("done.");
}
public void destroy()
{
running = false;
drawThread = null;
}
public void run()
{
while (running)
{
try {
drawThread.sleep(50);
}
catch (InterruptedException e){
System.out.println(e);
}
draw.repaint();
}
}
} -------------------------- Animate.java --------------------------------
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
public class Animate extends Applet {
int x =60, y=60;
Graphics screen;
public Animate()
{
screen = getGraphics();
}
public void paint(Graphics g)
{
if (g == null)
{
g = getGraphics();
}
g.setColor(Color.red);
x++;
y++;
// This will draw a rectangle (xco,yco,xwidth,height);
g.drawRect(5,5,20,20);
g.drawRect(x, y, 20, 20);
}
} ---------------------------------------------------------------- EDIT**** Please check out my second post instead (its an update).. Before it would work sometimes while other times it wouldn't work. I figured out that I was compiling in the wrong way. SO now ... I get the following error all the time with the above code. Quote:The second problem is that 'when' the program does work. The line 'draw.repaint();' isn't working. Since its a thread. I expect that the line will be called over and over again. and thus, moving the (x and y) cords so that you see a moving rectangle. It seems that the repaint() method isn't begin called. Thus, only repainting the screen if you were to (minimize/maximize) the screen. I have found many tutorials online on how to use the paint method. However, I have yet to come across a tutorial on "calling a paint method in another class", such as mine. How can this madness be done? Any help would be much appreciated!!!! [Edited by - Instigator on January 13, 2007 6:06:32 PM]
java.lang.NullPointerException at MainDr.run(MainDr.java:56) at java.lang.Thread.run(Thread.java:619) at MainDr.init(MainDr.java:21) at sun.applet.AppletPanel.run(AppletPanel.java:417) at java.lang.Thread.run(Thread.java:619)