interactive
11-20-2002, 08:50 PM
Alright I'm working on a college assignment I'm trying to draw a image make it basicly bounce between sides of the screen (this is a applet mind you). Problem is I need to add a button to change direction (ie from vertical to horizontal or vice versa). I'm pretty new to java to please be kind ;).
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Applet1 extends Applet implements Runnable, ActionListener
{
Thread t;
int x = 0;
Image img;
boolean hor=true;//this will be used for direction
public void init()
{
t = new Thread(this);
t.start();
img = getImage(getCodeBase(),"test.gif");
Button b = new Button("Change");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equals("Change"))
{
if(hor==true)
{
hor=false;
}else
{
hor=true;
}
}
}
public void paint(Graphics g)
{
//code for movement goes here
x=x+1;
if(x==200)
{
x=0;
}
System.out.println(hor);
if(hor==true)
{
g.drawImage(img,x,50,this);
}else
{
g.drawImage(img,50,x,this);
}
}
public void run()
{
while(true)
{
repaint();
try
{
t.sleep(30);
} catch(InterruptedException e){}
}
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Applet1 extends Applet implements Runnable, ActionListener
{
Thread t;
int x = 0;
Image img;
boolean hor=true;//this will be used for direction
public void init()
{
t = new Thread(this);
t.start();
img = getImage(getCodeBase(),"test.gif");
Button b = new Button("Change");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equals("Change"))
{
if(hor==true)
{
hor=false;
}else
{
hor=true;
}
}
}
public void paint(Graphics g)
{
//code for movement goes here
x=x+1;
if(x==200)
{
x=0;
}
System.out.println(hor);
if(hor==true)
{
g.drawImage(img,x,50,this);
}else
{
g.drawImage(img,50,x,this);
}
}
public void run()
{
while(true)
{
repaint();
try
{
t.sleep(30);
} catch(InterruptedException e){}
}
}
}
