简体   繁体   English

Java 图形更新/重绘图形

[英]Java Graphics Update/Repaint Graphic

I'm trying to work with the Java paint utility and it's been a bit of a hassle.我正在尝试使用 Java 绘图实用程序,但它有点麻烦。

I'm trying to do something which I assume is quite basic.我正在尝试做一些我认为非常基本的事情。 I'm drawing a square Graphic to a JPanel and then trying to move it using repaint我正在为 JPanel 绘制一个方形图形,然后尝试使用重绘来移动它

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

public class testGui {

   static gui      gc_gui;
   static int      gv_x;
   static int      gv_y;

   public static void main(String[] args) {

      gc_gui = new gui();
      gv_x = 50;
      gv_y = 50;

      gc_gui.cv_frame.setVisible(true);

   }

   public static class gui {

      JFrame    cv_frame;
      content   cv_content;

      public gui() {

         cv_frame = new JFrame();
         cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         cv_frame.setTitle("Test GUI");
         cv_frame.setSize(600, 400);
         cv_frame.setLayout(new FlowLayout());   

         cv_content = new content();
         cv_content.setBackground(Color.Black);
         cv_content.setPreferredSize(new Dimension(500, 300));
         cv_frame.add(cv_content);

         gv_x = 0;
         gv_y = 0;

         cv_content.update();

      }

   }

   public static class content extends JPanel {

      public void paint(Graphics graphic) {
         super.paint(graphic);
         draw(graphic);
      }

      public void update() {
         super.repaint();
      }

      public void draw(Graphics graphic) {
         Graphics2D graphic2D = (Graphics2D) graphic;
         graphic2D.setPaint(Color.Red);
         graphic2D.fillRect(gv_x, gv_y, 100, 100);
      }

   }

}

I don't know why the call to the update function isn't doing anything though.我不知道为什么对更新函数的调用没有做任何事情。

It draws the square at 50x and 50y, the sets it to 0x and 0y immediately and then when I call repaint I expected it to be moved to it's new coordinates although it's still at 50x and 50y.它在 50x 和 50y 处绘制正方形,立即将其设置为 0x 和 0y,然后当我调用 repaint 时,我预计它会被移动到它的新坐标,尽管它仍然在 50x 和 50y。

Why is this?为什么是这样?

Your solution is to use KeyBindings.您的解决方案是使用 KeyBindings。

https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

and also.并且。 You need to create a Swing Timer, Thread, or Loop , that manages the frames to be painted.您需要创建一个 Swing Timer、Thread 或 Loop 来管理要绘制的帧。 and such而这样的

Here is a link for Swing Timers as they are pretty easy to implement:这是 Swing Timers 的链接,因为它们很容易实现:

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

A lot of programs I see also have this ( AKA. working with threads.):我看到的很多程序也有这个(又名。使用线程。):

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        createAndShowGUI();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM