LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java threads (https://www.linuxquestions.org/questions/programming-9/java-threads-333583/)

poeta_boy 06-14-2005 04:07 PM

java threads
 
Hello:

I have this little program, trying to make an animation out of the movement of a rectangle...

/**********************************************************/

import java.applet.*;
import java.awt.*;


public class Broadway extends Applet implements Runnable
{
Thread animation;
int locx,locy; // location of rectangle
int width, height; // dimensions of rectangle
static final byte UP = 0; // direction of motion
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;
byte state; // state the rect is in
// length of pausing interval
static final int REFRESH_RATE = 1000; // in ms

Graphics offscreen;
Image image;

// applet methods:


public void init()
{
System.out.println(">> init <<");
setBackground(Color.black);
locx = 80; // parameters of center rect
locy = 100;
width = 110;
height = 90;
state = UP;
image = createImage(width,height);
offscreen = image.getGraphics();
}

public void start()
{
System.out.println(">> start <<");
animation = new Thread(this);
if (animation != null)
{
animation.start();
}
}


public void paint(Graphics g)
{
System.out.println(">> paint <<");

offscreen.setColor(Color.black);
offscreen.fillRect(0,0,300,300); // clear buffer
System.out.println(">> paint 1<<");
offscreen.setColor(Color.yellow);
offscreen.fillRect(0,0,90,90);
System.out.println(">> paint 2 <<");
offscreen.fillRect(250,0,40,190);
System.out.println(">> paint <<");
offscreen.fillRect(80,110,100,20); // hidden rectangle
System.out.println(">> paint 3 <<");
offscreen.setColor(Color.blue);
offscreen.fillRect(80,200,220,90);
System.out.println(">> paint 4 <<");
offscreen.fillRect(100,10,90,80);
System.out.println(">> paint 5 <<");
offscreen.setColor(Color.lightGray);
offscreen.fillRect(locx,locy,width,height);
System.out.println(">> paint 6 <<");
offscreen.setColor(Color.red);
offscreen.fillRect(200,0,45,45);
System.out.println(">> paint 7 <<");
offscreen.fillRect(0,100,70,200);
offscreen.setColor(Color.magenta);
offscreen.fillRect(200,55,60,135);
System.out.println(">> paint 8 <<");
g.drawImage(image,0,0,this);

}

public void update(Graphics g)
{
paint(g);
}
// update the center rectangle
void updateRectangle()
{
switch (state) {
case DOWN:
locy += 2;
if (locy >= 110) {
state = UP;
}
break;
case UP:
locy -= 2;
if (locy <= 90) {
state = RIGHT;
}
break;
case RIGHT:
locx += 2;
if (locx >= 90) {
state = LEFT;
}
break;
case LEFT:
locx -= 2;
if (locx <= 70) {
state = DOWN;
}
break;
}
}
public void run()
{
while (true) {
repaint();
updateRectangle();
try {
Thread.sleep (REFRESH_RATE);
} catch (Exception exc) { System.out.println("ERROR");};
}
}

public void stop() {
System.out.println(">> stop <<");
if (animation != null) {
//animation.stop();
animation = null;
}
}
}

/**********************************************************/

But the paint( g ) method isn't quite working... as you can see, this method has some println indicating the steps the program currently is....all the println are ok, meaning that the rectangles are there, but they arent.... I just see 2 out of the 9 rectangles that it should show...

please any ideas?

I copied this code from a book and I find quite strange the line:

g.drawImage(image,0,0,this);

even when the drawing is being done in the "offscreen" buffer, it tells graphics g to draw the image "image".... how does it link the offscreen to the g object?

Thanks a lot in advance

Poeta

poeta_boy 06-14-2005 04:34 PM

hello!
nevermind... I found the error, the drawing surface wasn't big enough I just did:

width = 410;
height = 490;

and it goes smoothly

Thanks!


All times are GMT -5. The time now is 10:48 PM.