簡體   English   中英

如何讓框架不斷重繪直到窗口關閉?

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

我目前正在制作一個立方體(用 point2d 和 line2d 繪制以制作形狀)圍繞一個框架移動,類似於 pong 或窗口屏幕保護程序氣泡。 我能夠將立方體打印到框架上,但我不明白如何在第一次打印后讓它移動。 我想使用計時器動作偵聽器使球四處移動。

查看器

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);
    }
}

成分

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);
        }

    }
}

立方體

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;
    }
}

任何幫助是極大的贊賞! 謝謝你。

首先使多維數據集成為類的成員,而不是局部變量。 其次從您的繪制組件中刪除所有移動邏輯。 第三,在更新立方體位置后請求重繪。

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);
}

現在,您有一個可以改變位置的立方體。 您還有一種方法可以更改位置。 所以在你的 main 方法中創建一個調用 update 方法的計時器。

    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();

請注意,您可以使用 lambdas 使其更加簡潔,並且不需要命名類TimerListener

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

這是黑暗中的一個鏡頭,但是您是否嘗試將代碼放入:

While (true) {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM