Java图形无法重新定位矩形

Java图形无法重新定位矩形,java,swing,Java,Swing,我试图重新定位一个矩形,但因为我不明白为什么它保持在同一个位置。 它会创建一个红色矩形,但不会更改颜色或移动到新位置 这是我的密码: package grap_prj.dom.shenkar; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.concurrent.TimeUnit; import ja

我试图重新定位一个矩形,但因为我不明白为什么它保持在同一个位置。 它会创建一个红色矩形,但不会更改颜色或移动到新位置

这是我的密码:

package grap_prj.dom.shenkar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.concurrent.TimeUnit;

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

public class graphic_main extends JPanel{

static Rectangle rec = new Rectangle ();

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    rec.setSize(10, 10);
    rec.setLocation(10, 10);

    g2d.setColor(Color.RED);
    g2d.drawRect((int)rec.getX(),(int)rec.getY(), 10, 10);
    g2d.fillRect((int)rec.getX(),(int)rec.getY(), 10, 10);
}

public static void update_ui (Graphics g)
{
    System.out.println("in update");
    rec.setLocation(50, 50);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawRect((int)rec.getX(),(int)rec.getY(), 10, 10);
    g2d.fillRect((int)rec.getX(),(int)rec.getY(), 10, 10);  
}

public static void main(String[] args) {

    JFrame frame = new JFrame("Simple Graphics");
    frame.add(new graphic_main());
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {
        TimeUnit.SECONDS.sleep(10);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    graphic_main.update_ui(frame.getGraphics());
    frame.revalidate();
 }
}
更新:
我在代码中做了一些更改,但仍然是相同的情况。我更改了位置,但添加了一个新的矩形,而不是移动现有的矩形。

每当覆盖
paintComponent()
时,需要调用
super.paintComponent()


您还可以从
repaint()
调用
repaint()
。您需要决定一些会导致它重新绘制的操作。

每当您重写
paintComponent()
时,您需要调用
super.paintComponent()


您还可以从
repaint()
调用
repaint()
。您需要决定一些会导致它重新绘制的操作。

每当您重写
paintComponent()
时,您需要调用
super.paintComponent()


您还可以从
repaint()
调用
repaint()
。您需要决定一些会导致它重新绘制的操作。

每当您重写
paintComponent()
时,您需要调用
super.paintComponent()


您还可以从
repaint()
调用
repaint()
。您需要决定一些操作,以使其重新绘制。

您将继续将位置设置为10,10,因此矩形将始终绘制为10,10


设置位置50,50后,您不会绘制任何内容。下一步,您将再次设置10,10。

您将继续将位置设置为10,10,因此矩形将始终在10,10处绘制


设置位置50,50后,您不会绘制任何内容。下一步,您将再次设置10,10。

您将继续将位置设置为10,10,因此矩形将始终在10,10处绘制


设置位置50,50后,您不会绘制任何内容。下一步,您将再次设置10,10。

您将继续将位置设置为10,10,因此矩形将始终在10,10处绘制

设置位置50,50后,您不会绘制任何内容。下一步,您将再次设置10,10

  • 决不能在
    paintComponent(…)
    方法内部调用
    update()
    repaint()
    。曾经这可能会导致重复出现或无效的非受控动画
  • 不要在paint或paintComponent方法中更改对象的状态。您无法完全控制何时甚至是否调用这些方法
  • 不要忘记在paintComponent覆盖中调用super的方法,以允许JPanel执行其内部管理图形,包括擦除旧的脏像素
  • 即使将图形上下文的颜色更改为蓝色,只要调用paintComponent,它也会立即变回红色。所以你的颜色改变是无用的代码。解决方案:使颜色成为可设置的变量
  • 如果要执行Swing动画,请使用

  • 有关Swing动画的示例,请看我的示例


    再举一个例子,看看这个:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleAnimation extends JPanel {
    
       private static final int PREF_W = 800;
       private static final int PREF_H = 650;
       private static final Color[] COLORS = { Color.red, Color.orange,
             Color.yellow, Color.green, Color.blue, Color.magenta };
       private static final int RECT_WIDTH = 40;
       private static final int TIMER_DELAY = 10;
       private int x = 0;
       private int y = 0;
       private int colorIndex = 0;
       private Color color = COLORS[colorIndex];
    
       public SimpleAnimation() {
          new Timer(TIMER_DELAY, new TimerListener()).start();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(color);
          g.fillRect(x, y, RECT_WIDTH, RECT_WIDTH);
       }
    
       private class TimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             x++;
             y++;
             if (x + RECT_WIDTH > getWidth()) {
                x = 0;
             }
             if (y + RECT_WIDTH > getHeight()) {
                y = 0;
             }
             if (x % 40 == 0) {
                colorIndex++;
                colorIndex %= COLORS.length;
                color = COLORS[colorIndex];
             }
             repaint();
          }
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("SimpleAnimation");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new SimpleAnimation());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
  • 决不能在
    paintComponent(…)
    方法内部调用
    update()
    repaint()
    。曾经这可能会导致重复出现或无效的非受控动画
  • 不要在paint或paintComponent方法中更改对象的状态。您无法完全控制何时甚至是否调用这些方法
  • 不要忘记在paintComponent覆盖中调用super的方法,以允许JPanel执行其内部管理图形,包括擦除旧的脏像素
  • 即使将图形上下文的颜色更改为蓝色,只要调用paintComponent,它也会立即变回红色。所以你的颜色改变是无用的代码。解决方案:使颜色成为可设置的变量
  • 如果要执行Swing动画,请使用

  • 有关Swing动画的示例,请看我的示例


    再举一个例子,看看这个:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleAnimation extends JPanel {
    
       private static final int PREF_W = 800;
       private static final int PREF_H = 650;
       private static final Color[] COLORS = { Color.red, Color.orange,
             Color.yellow, Color.green, Color.blue, Color.magenta };
       private static final int RECT_WIDTH = 40;
       private static final int TIMER_DELAY = 10;
       private int x = 0;
       private int y = 0;
       private int colorIndex = 0;
       private Color color = COLORS[colorIndex];
    
       public SimpleAnimation() {
          new Timer(TIMER_DELAY, new TimerListener()).start();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(color);
          g.fillRect(x, y, RECT_WIDTH, RECT_WIDTH);
       }
    
       private class TimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             x++;
             y++;
             if (x + RECT_WIDTH > getWidth()) {
                x = 0;
             }
             if (y + RECT_WIDTH > getHeight()) {
                y = 0;
             }
             if (x % 40 == 0) {
                colorIndex++;
                colorIndex %= COLORS.length;
                color = COLORS[colorIndex];
             }
             repaint();
          }
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("SimpleAnimation");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new SimpleAnimation());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
  • 决不能在
    paintComponent(…)
    方法内部调用
    update()
    repaint()
    。曾经这可能会导致重复出现或无效的非受控动画
  • 不要在paint或paintComponent方法中更改对象的状态。您无法完全控制何时甚至是否调用这些方法
  • 不要忘记在paintComponent覆盖中调用super的方法,以允许JPanel执行其内部管理图形,包括擦除旧的脏像素
  • 即使将图形上下文的颜色更改为蓝色,只要调用paintComponent,它也会立即变回红色。所以你的颜色改变是无用的代码。解决方案:使颜色成为可设置的变量
  • 如果要执行Swing动画,请使用

  • 有关Swing动画的示例,请看我的示例


    再举一个例子,看看这个:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleAnimation extends JPanel {
    
       private static final int PREF_W = 800;
       private static final int PREF_H = 650;
       private static final Color[] COLORS = { Color.red, Color.orange,
             Color.yellow, Color.green, Color.blue, Color.magenta };
       private static final int RECT_WIDTH = 40;
       private static final int TIMER_DELAY = 10;
       private int x = 0;
       private int y = 0;
       private int colorIndex = 0;
       private Color color = COLORS[colorIndex];
    
       public SimpleAnimation() {
          new Timer(TIMER_DELAY, new TimerListener()).start();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(color);
          g.fillRect(x, y, RECT_WIDTH, RECT_WIDTH);
       }
    
       private class TimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             x++;
             y++;
             if (x + RECT_WIDTH > getWidth()) {
                x = 0;
             }
             if (y + RECT_WIDTH > getHeight()) {
                y = 0;
             }
             if (x % 40 == 0) {
                colorIndex++;
                colorIndex %= COLORS.length;
                color = COLORS[colorIndex];
             }
             repaint();
          }
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("SimpleAnimation");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new SimpleAnimation());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
  • 决不能在
    paintComponent(…)
    方法内部调用
    update()
    repaint()
    。曾经这可能会导致重复出现或无效的非受控动画
  • 不要在paint或paintComponent方法中更改对象的状态。您无法完全控制何时甚至是否调用这些方法
  • 不要忘记在paintComponent覆盖中调用super的方法,以允许JPanel执行其内部管理图形,包括擦除旧的脏像素
  • 即使将图形上下文的颜色更改为蓝色,只要调用paintComponent,它也会立即变回红色。所以你的颜色改变是无用的代码。解决方案:使颜色成为可设置的变量
  • 如果要执行Swing动画,请使用

  • 有关Swing动画的示例,请看我的示例