0

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;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I included my code. Any help would be appreciated. –  Dec 02 '16 at 00:26
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Dec 02 '16 at 15:37
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 4) Your description of what should happen is unclear to me. Should the wall reverse direction once it hits the top, or should it be moved at the bottom & start again, or..? – Andrew Thompson Dec 02 '16 at 15:39
  • Thank you for your answers. To be more precise, i want a line that starts at the bottom and shifts left and right randomly. Until it reaches the top wall then it keeps shifting left and right without exceeding the applet frame @Andrew Thompson –  Dec 07 '16 at 01:13
  • *"Thank you for your answers."* You're welcome, but they weren't answers, just comments. Further, I asked some questions in those comments (#'s 1 & 3) that I expect to hear answers for, before offering further help. – Andrew Thompson Dec 07 '16 at 01:36
  • To answer your questions, I'm trying to build a game and the tutorial I was following was coding a game using an applet so I'm following in his footsteps. The website I was following is http://www.kilobolt.com/game-development-tutorial.html –  Dec 07 '16 at 04:13
  • *".. I'm following in his footsteps"* "Walking with dinosaurs" is how I'd describe it. Find a new tutorial. If it involves applets or AWT components, skip it. Look.. this advice is coming from the top ranked provider of answers on [applets](http://stackoverflow.com/tags/applet/topusers) & the 2nd highest for [AWT](http://stackoverflow.com/tags/awt/topusers) but it has gotten to the stage that even ***I*** could not be bothered spending time on them anymore (I also wrote the blog article linked above in the 1st comment). That should (perhaps) be seen as a strong hint that you won't get .. – Andrew Thompson Dec 07 '16 at 04:30
  • .. good help on either through stack overflow. Time to move to different approaches (application using Swing ..or even Java-FX). – Andrew Thompson Dec 07 '16 at 04:32

0 Answers0