Currently, I have a line which moves from the bottom of the screen to the top where it disappears. I would like to have the line continuously moving inside my applet so it does not disappear. But the applet has to be moving in other words it has to update its position. Or the line has to move without exceeding the boundaries of the applet.
StartingClass
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
public class StartingClass extends Applet implements Runnable {
Wall wall = new Wall();
private Image image;
private Graphics second;
@Override
public void init() {
setSize(400, 600);
setBackground(Color.BLACK);
setFocusable(true);
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while (true) {
wall.update();
try {
repaint();
Thread.sleep(17);
// System.out.println("test");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void update(Graphics g) {
if (image == null) {
// System.out.println(this.getHeight());
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
for (int i = 0; i <= 100; i += 5) {
g.fillOval(189, wall.getPositionY() + i, wall.getCircleWidth(), wall.getCircleHeight());
}
}
}
Wall
public class Wall {
final int GAMESPEED = 2;
final int circleWidth = 15;
final int circleHeight = 15;
int positionY = 600;
public void update() {
positionY -= GAMESPEED;
}
public int getPositionY() {
return positionY;
}
public int getCircleWidth() {
return circleWidth;
}
public int getCircleHeight() {
return circleHeight;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
}