Veneta
asked on
Help with java applet
I have created a java applet and htm file called Checker4 which is a checker which consists of 4 fillRectangles. A top left, top right, bottom left and bottom right. The applet box is 200 width and 200 height. I also have a drawOval which is width 90 and height 90. The problem is that I need the drawOval to start at the top left box znd visit the top right, bottom right, bottom left and back to the top left rectanlge. I have managed to make the drawOval visit the topleft, top right and bottom right but cannot progress any further to the bottom left and back to the top left rectangle. My code is as follows:
/* an applet which draws a continous moving checker*/
import java.awt.*;
public class Checker4 extends TaskingApplet
{
Image offscreenImage;
Graphics offscreenGraphics;
int xpos;//y co-ordinate
int ypos;//x co-ordinate
public void init()
{
offscreenImage = createImage(this.size().wi dth,
this.size().height);
offscreenGraphics = offscreenImage.getGraphics ();
}
public void run()
{
while (true)
{
for (xpos = 5; xpos <=105; xpos = xpos + 5)
{
repaint();
try (Thread.sleep(50);}
catch (InterrupterdException e) {}
}
for (ypos = 105; ypos >5; ypos = xpos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( xpos = 105; xpos >0; xpos = ypos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for { ypos = 5; ypos <=105; ypos = xpos-5)
{
repaint();
try {Thread.sleep (50);}
catch (InterruptedException e) {}
}
}
}
public void update (Graphics g)
{
paint (g);
}
public void paint (Graphics g)
{
offscreenGraphics.setColor (Color.bla ck};
offscreenGraphics.fillRect (0,0,100,1 00);
offscreenGraphics.setColor (Color.whi te);
offscreenGraphics.fillRect (100,0,100 ,100);
offscreenGraphics.fillRect (101,10,10 0,100);
offscreenGraphics.setColor (Color.bla ck);
offscreenGraphics.fillRect (101,101,1 00,100);
//draw Checker
offscreenGraphics.setColor (Color.blu e);
offscreenGraphics.fillOval (xpos,ypos ,90,90);
//copy the image to the screen
g.drawImage(offscreenImage ,0,0,this) ;
}
}
I would appreciate if someone would look at my code and let me know what I am doing wrong and how I should implement my program so the drawOval travels from the topleft box in a clockwise direction. Thank you for your time.
Veneta
/* an applet which draws a continous moving checker*/
import java.awt.*;
public class Checker4 extends TaskingApplet
{
Image offscreenImage;
Graphics offscreenGraphics;
int xpos;//y co-ordinate
int ypos;//x co-ordinate
public void init()
{
offscreenImage = createImage(this.size().wi
this.size().height);
offscreenGraphics = offscreenImage.getGraphics
}
public void run()
{
while (true)
{
for (xpos = 5; xpos <=105; xpos = xpos + 5)
{
repaint();
try (Thread.sleep(50);}
catch (InterrupterdException e) {}
}
for (ypos = 105; ypos >5; ypos = xpos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( xpos = 105; xpos >0; xpos = ypos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for { ypos = 5; ypos <=105; ypos = xpos-5)
{
repaint();
try {Thread.sleep (50);}
catch (InterruptedException e) {}
}
}
}
public void update (Graphics g)
{
paint (g);
}
public void paint (Graphics g)
{
offscreenGraphics.setColor
offscreenGraphics.fillRect
offscreenGraphics.setColor
offscreenGraphics.fillRect
offscreenGraphics.fillRect
offscreenGraphics.setColor
offscreenGraphics.fillRect
//draw Checker
offscreenGraphics.setColor
offscreenGraphics.fillOval
//copy the image to the screen
g.drawImage(offscreenImage
}
}
I would appreciate if someone would look at my code and let me know what I am doing wrong and how I should implement my program so the drawOval travels from the topleft box in a clockwise direction. Thank you for your time.
Veneta
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks rrz,
The code for TaskingApplet is as follows:
public class TaskingApplet extends java.applet.Applet
{ implents Runnable
Thread task;//declare a variable to hold new thread
public void init()
{
//put initialisation code here
}
public void start()//creates a new thread
{
if (task == null);
{
task = new Thread(this);//create new thread
task.start();//call the thread's start//method - dont confuse with//the applet's start method
}
}
public void stop()//stop thread here
{
if (task!= null)
{
task stop();call its stop metod
task = null;//ready for garbage collector
}
}
public void run()
{
//put main code here
}
}
The code for TaskingApplet is as follows:
public class TaskingApplet extends java.applet.Applet
{ implents Runnable
Thread task;//declare a variable to hold new thread
public void init()
{
//put initialisation code here
}
public void start()//creates a new thread
{
if (task == null);
{
task = new Thread(this);//create new thread
task.start();//call the thread's start//method - dont confuse with//the applet's start method
}
}
public void stop()//stop thread here
{
if (task!= null)
{
task stop();call its stop metod
task = null;//ready for garbage collector
}
}
public void run()
{
//put main code here
}
}
You had a lot of mistakes in your code. I made the necessary changes.
public class TaskingApplet extends java.applet.Applet implements Runnable
{
Thread task;
public void start()
{
if (task == null);
{
task = new Thread(this);
task.start();
}
}
public void stop(){task = null;}
public void run(){}
}
import java.awt.*;
public class Checker4 extends TaskingApplet
{
Image offscreenImage;
Graphics offscreenGraphics;
int xpos=5;//y co-ordinate
int ypos=5;//x co-ordinate
public void init()
{
offscreenImage = createImage(getSize().widt h,getSize( ).height);
offscreenGraphics = offscreenImage.getGraphics ();
}
public void run()
{
Thread thisthread = Thread.currentThread();
while (task==thisthread)
{
for (xpos = 5; xpos <=105; xpos = xpos + 5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( ypos = 5; ypos <=105; ypos = ypos+5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( xpos = 105; xpos >0; xpos = xpos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for (ypos = 105; ypos >5; ypos = ypos -5)
{
repaint();
try {Thread.sleep (50);}
catch (InterruptedException e) {}
}
}
}
public void update (Graphics g)
{
paint (g);
}
public void paint (Graphics g)
{
offscreenGraphics.setColor (Color.bla ck);
offscreenGraphics.fillRect (0,0,100,1 00);
offscreenGraphics.setColor (Color.whi te);
offscreenGraphics.fillRect (100,0,100 ,100);
offscreenGraphics.fillRect (0,100,100 ,100);
offscreenGraphics.setColor (Color.bla ck);
offscreenGraphics.fillRect (101,101,1 00,100);
//draw Checker
offscreenGraphics.setColor (Color.blu e);
offscreenGraphics.fillOval (xpos,ypos ,90,90);
//copy the image to the screen
g.drawImage(offscreenImage ,0,0,this) ;
}
}
I would appreciate some points.
rrz
public class TaskingApplet extends java.applet.Applet implements Runnable
{
Thread task;
public void start()
{
if (task == null);
{
task = new Thread(this);
task.start();
}
}
public void stop(){task = null;}
public void run(){}
}
import java.awt.*;
public class Checker4 extends TaskingApplet
{
Image offscreenImage;
Graphics offscreenGraphics;
int xpos=5;//y co-ordinate
int ypos=5;//x co-ordinate
public void init()
{
offscreenImage = createImage(getSize().widt
offscreenGraphics = offscreenImage.getGraphics
}
public void run()
{
Thread thisthread = Thread.currentThread();
while (task==thisthread)
{
for (xpos = 5; xpos <=105; xpos = xpos + 5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( ypos = 5; ypos <=105; ypos = ypos+5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for ( xpos = 105; xpos >0; xpos = xpos -5)
{
repaint();
try {Thread.sleep(50);}
catch (InterruptedException e) {}
}
for (ypos = 105; ypos >5; ypos = ypos -5)
{
repaint();
try {Thread.sleep (50);}
catch (InterruptedException e) {}
}
}
}
public void update (Graphics g)
{
paint (g);
}
public void paint (Graphics g)
{
offscreenGraphics.setColor
offscreenGraphics.fillRect
offscreenGraphics.setColor
offscreenGraphics.fillRect
offscreenGraphics.fillRect
offscreenGraphics.setColor
offscreenGraphics.fillRect
//draw Checker
offscreenGraphics.setColor
offscreenGraphics.fillOval
//copy the image to the screen
g.drawImage(offscreenImage
}
}
I would appreciate some points.
rrz
rrz,
But basically your providing the same answer I did :)
But basically your providing the same answer I did :)
Yes,objects your comments were correct. But Veneta had a lot more mistakes than the ones you pointed out. I think he(or she) took the program from the book: Sam's "Teach Yourself Java in 21 days" and attempted to dress it up some. I guess it is a good way to learn. My suggestion to Veneta is to take smaller gulps next time. Just try to learn one thing at a time. rrz
And there are others that you also missed.
I was just focussing on the question at hand, ie. one step at a time :)
I was just focussing on the question at hand, ie. one step at a time :)
What did I miss? I realize that you have much more experience than I do. I am willing to give points to learn something. rrz
What did I miss? I realize that you have much more experience than I do. I am willing to give points to learn something. rrz
Main change I'd make would be to move the painting of the offscreen image out of paint.
And probably factoring out, some of the 'static' painting from the 'dynamic' painting.
The thread exit code could also be improved to stop immediatly instead of once the animation loop is complete.
And probably factoring out, some of the 'static' painting from the 'dynamic' painting.
The thread exit code could also be improved to stop immediatly instead of once the animation loop is complete.
Why would we want "to move the painting of the offscreen image out of paint"? What 'static' painting are you referring to? Are you referring to fact the checker is at most in front 2 squares at any point in time? The thread(in my code) stops when the applet stops. The animation is never complete. The checker keeps repeating its path.
rrz
rrz
The offscreen image gets (unnecessarily) repainted every time repaint is called, it only need to be repainted when the oval movesThe thread (in your code) won't stop until the oval has finished moving around the board.
Most of these are trivial changes and will have no real impact on this simple case but
Most of these are trivial changes and will have no real impact on this simple case but
It should say ypos = ypos - 2 (not xpos - 5).
As xpos isn't changing this loop will never end.
Same problem with the other for loops, only the first one seems correct.