Java - 如何移动由Graphics2D绘制的矩形? [英] Java - How to move a rectangle that's drawn by Graphics2D?

查看:298
本文介绍了Java - 如何移动由Graphics2D绘制的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图移动由Graphics2D绘制的矩形,但它不起作用。当我做x + = 1;它实际上将其移动1个像素并停止。如果我说的话,x + = 200;它在ONCE上移动200个像素,而不是在每次更新中,但是ONCE。

  public void paint(Graphics g)
{
super.paint(g);

g.setColor(Color.WHITE);
g.fillRect(0,0,this.getWidth(),this.getHeight());

g.setColor(Color.RED);
g.fillRect(x,350,50,50);

x + = 1;
}

int x在void paint之外调用,以确保它不会每次增加150时间。
画好,只是不动,我尝试使用一个线程,并使用一个while循环,所以当线程运行时,它会移动,但没有运气。

解决方案

不要使用while循环或其他线程,而应该使用 java.swing.Timer 来进行动画。以下是基本构造:

$ p code $ Timer $(int delay,ActionListener listener)

其中delay是您想要在重绘之间延迟的时间,而 listener 是具有回调函数的侦听器去表演。你可以做这样的事情,在那里你改变 x 位置,然后调用 repaint();

  ActionListener listener = new AbstractAction(){
public void actionPerformed(ActionEvent e){
if(x> = D_W){
x = 0;
drawPanel.repaint();
} else {
x + = 10;
drawPanel.repaint();
}
}
};
定时器定时器=新定时器(250,侦听器);
timer.start();






  import java.awt中*。 
import java.awt.event。*;
import javax.swing。*;

public class KeyBindings extends JFrame {

private static final int D_W = 500;
private static final int D_H = 200;
int x = 0;
int y = 0;

DrawPanel drawPanel = new DrawPanel();

public KeyBindings(){
ActionListener listener = new AbstractAction(){
public void actionPerformed(ActionEvent e){
if(x> = D_W){
x = 0;
drawPanel.repaint();
} else {
x + = 10;
drawPanel.repaint();
}
}
};
Timer timer = new Timer(100,listener);
timer.start();
add(drawPanel);

pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

private class DrawPanel extends JPanel {

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(x,y,50,50);
}

public Dimension getPreferredSize(){
return new Dimension(D_W,D_H);



public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
public void run( ){
new KeyBindings();
}
});
}
}



以下是一个正在运行的示例


I'm trying to move a rectangle drawn by Graphics2D over, but it just doesn't work. When I do x += 1; It actually moves it 1 pixel over and stops. if I do say, x += 200; It moves it 200 pixels over ONCE not in every update, but ONCE.

public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(Color.WHITE);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());

    g.setColor(Color.RED);
    g.fillRect(x, 350, 50, 50);

    x += 1;
}

int x is called outside void paint to make sure it's not incremented as 150 each time. Draws fine, just doesn't move, I tried using a thread and using a while loop so while the thread is running, it moves, but no luck.

解决方案

Instead of using a while loop or a different thread, you should be using a java.swing.Timer for animation. Here's the basic construct

Timer(int delay, ActionListener listener)

where delay is the time you want to be delayed between repaints, and listener is the listener with the callback function to perform. You could do something like this, where you change the x location, then call repaint();

    ActionListener listener = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (x >= D_W) {
                x = 0;
                drawPanel.repaint();
            } else {
                x += 10;
                drawPanel.repaint();
            }
        }
    };
    Timer timer = new Timer(250, listener);
    timer.start();


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyBindings extends JFrame {

    private static final int D_W = 500;
    private static final int D_H = 200;
    int x = 0;
    int y = 0;

    DrawPanel drawPanel = new DrawPanel();

    public KeyBindings() {
        ActionListener listener = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if (x >= D_W) {
                    x = 0;
                    drawPanel.repaint();
                } else {
                    x += 10;
                    drawPanel.repaint();
                }
            }
        };
        Timer timer = new Timer(100, listener);
        timer.start();
        add(drawPanel);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class DrawPanel extends JPanel {

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.GREEN);
            g.fillRect(x, y, 50, 50);
        }

        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KeyBindings();
            }
        });
    }
}

Here's a running example

这篇关于Java - 如何移动由Graphics2D绘制的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆