【问题标题】:Rectangle getting bigger instead of changing position矩形变大而不是改变位置
【发布时间】:2015-03-25 14:14:39
【问题描述】:

在另一个程序上我遇到了同样的问题,所以我给了你我正在尝试用一个较小的类做的事情,这样你就可以更轻松地阅读代码。 基本上,我正在尝试改变 X 的位置,但它似乎只是变大了。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;


public class test extends JPanel{

    public int x = 200;
    public int y = 620;

    Rectangle cube = new Rectangle(x, y, 80, 80);

    int cubex = cube.x;


    public void paint(Graphics g){

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.fill(cube);

    }

    public void moveCube(){
        cube.setLocation(cubex -= 1, y);
        cube.setSize(80, 80);
        repaint();
    }

    public static void main(String[] args) throws InterruptedException {

        test t = new test();

        JFrame frame = new JFrame();

        frame.add(t);

        frame.setSize(700, 1000);
        frame.setVisible(true);
        frame.setTitle("The Cube");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        frame.setBackground(new Color(240, 84, 84));

        while(true){

            t.moveCube();
            Thread.sleep(10);
        }
    }

}

非常感谢您的帮助:)

【问题讨论】:

    标签: java cube


    【解决方案1】:

    我试过你的程序。您正在正确移动对象,但您没有清除之前绘制的矩形;因此它似乎正在扩大。

    【讨论】:

    • 您能解释一下如何做到这一点吗?我对此真的很陌生:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2017-06-23
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多