How do I make the frame keep repainting until window close?

Nonstopbrain :

I'm currently making a cube (drawing with point2d and line2d to make the shape) move around a frame, similar to that of pong or the window screen saver bubbles. i am able to print the cube onto the frame but i don't understand how to make it move around after the first print. I want to use a timer action listener to make the ball move around.

Viewer

import javax.swing.JFrame;
public class CubeBounceViewer {

    public static void main(String[] agrs) {
        JFrame frame = new JFrame();

        frame.setTitle("Cube Bounce");
        frame.setSize(1280,720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        CubeBounceComponent component = new CubeBounceComponent();
        frame.add(component);
    }
}

Component

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.Timer;

public class CubeBounceComponent extends JComponent{

    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;

        //Creates a object list with size equal to value of instance
        Cube cube = new Cube();

        class TimerListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                cube.move();
            }
        }

        ActionListener listener = new TimerListener();

        final int DELAY = 100;
        Timer t = new Timer(DELAY, listener);
        t.start();

        cube.draw(g2);
        cube.move();
        if (cube.getX()>=1265 || cube.getX()<=15 && cube.getY()<=15 || cube.getY()>=705) {
            cube.changeDirection(-1, -1);
        } else if(cube.getX()>=1265 || cube.getX()<=15) {
            cube.changeDirection(-1, 1);
        } if (cube.getY()<=15 || cube.getY()>=705) {
            cube.changeDirection(1,-1);
        }

    }
}

Cube

import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Cube {
    private int centerX,centerY,directionX, directionY;

    public Cube() {
        centerX=640;
        centerY=360;
        directionX=1;
        directionY=1;
    }

    public void draw(Graphics2D g2) {
        Point2D.Double backTopLeft = new Point2D.Double(centerX-10,centerY-15);
        Point2D.Double backTopRight = new Point2D.Double(centerX+15,centerY-15);
        Point2D.Double backBottomLeft = new Point2D.Double(centerX-10,centerY+10);
        Point2D.Double backBottomRight = new Point2D.Double(centerX+15,centerY+10);
        Point2D.Double frontTopLeft = new Point2D.Double(centerX-15,centerY-10);
        Point2D.Double frontTopRight = new Point2D.Double(centerX+10,centerY-10);
        Point2D.Double frontBottomLeft = new Point2D.Double(centerX-15,centerY+15);
        Point2D.Double frontBottomRight = new Point2D.Double(centerX+10,centerY+15);

        Line2D.Double backTop = new Line2D.Double(backTopLeft,backTopRight);
        Line2D.Double backRight = new Line2D.Double(backTopRight,backBottomRight);
        Line2D.Double backLeft = new Line2D.Double(backTopLeft,backBottomLeft);
        Line2D.Double backBottom = new Line2D.Double(backBottomLeft,backBottomRight);
        Line2D.Double frontTop = new Line2D.Double(frontTopLeft,frontTopRight);
        Line2D.Double frontRight = new Line2D.Double(frontTopRight,frontBottomRight);
        Line2D.Double frontLeft = new Line2D.Double(frontTopLeft,frontBottomLeft);
        Line2D.Double frontBottom = new Line2D.Double(frontBottomLeft,frontBottomRight);
        Line2D.Double topLeft = new Line2D.Double(backTopLeft,frontTopLeft);
        Line2D.Double topRight = new Line2D.Double(backTopRight,frontTopRight);
        Line2D.Double bottomLeft = new Line2D.Double(backBottomLeft,frontBottomLeft);
        Line2D.Double bottomRight = new Line2D.Double(backBottomRight,frontBottomRight);

         g2.draw(backTop);
         g2.draw(backRight);
         g2.draw(backLeft);
         g2.draw(backBottom);
         g2.draw(frontTop);
         g2.draw(frontRight);
         g2.draw(frontLeft);
         g2.draw(frontBottom);
         g2.draw(topLeft); 
         g2.draw(topRight);
         g2.draw(bottomLeft);
         g2.draw(bottomRight); 
    }

    public void changeDirection(int x, int y) {
        directionX*=x;
        directionY*=y;
    }
    public void move() {
        centerX+=1*directionX;
        centerY+=1*directionY;
    }

    public int getX() {
        return centerX;
    }
    public int getY() {
        return centerY;
    }
}

Any help is greatly appreciated! Thank you.

matt :

First make the cube a member of your class, not a local variable. Second remove all of the moving logic from your paint component. Third Request a repaint after you've updated the cubes position.

Cube cube = new Cube();

public void updatePosition(){


    cube.move();
    if (cube.getX()>=1265 || cube.getX()<=15 && cube.getY()<=15 || cube.getY()>=705) {
        cube.changeDirection(-1, -1);
    } else if(cube.getX()>=1265 || cube.getX()<=15) {
        cube.changeDirection(-1, 1);
    } if (cube.getY()<=15 || cube.getY()>=705) {
        cube.changeDirection(1,-1);
    }
    repaint();
}

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    cube.draw(g2);
}

Now, you have a cube that can change position. You also have a method to change the position. So in your main method create a timer that calls the update method.

    class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            component.updatePosition();
        }
    }

    ActionListener listener = new TimerListener();

    final int DELAY = 100;
    Timer t = new Timer(DELAY, listener);
    t.start();

Note you can use lambdas to make this a little more concise, and not need the named class TimerListener.

Timer t = new Timer(DELAY, evt->component.updatePosition() )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25289&siteId=1