JFrame仅在调整大小时更新

时间:2014-05-23 23:45:51

标签: java swing jframe

我正在为高尔夫模拟器编写程序。到目前为止,它基本上按预期工作,但窗口只在我拖动以调整大小时才更新。为什么会这样?我附上了以下相关课程(还有其他课程)。非常感谢帮助。更新为MWE。程序应该更改背景颜色,但仅在调整窗口大小时执行此操作。接下来是FieldComponent类,然后是主类。

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class FieldComponent extends Canvas {

    private BufferedImage back;
    private int time;

public FieldComponent() {
    setVisible(true);
}

public void update(Graphics window) {
    paint(window);
}


public void paint(Graphics window) {
    Graphics2D twoDGraph = (Graphics2D) window;
    if (back == null) {
        back = (BufferedImage) createImage(getWidth(), getHeight());
    }
    Graphics graphToBack = back.getGraphics(); 
    if(time%2==0){
        graphToBack.setColor(Color.RED);
    }
    else{
        graphToBack.setColor(Color.GREEN);
    }
    graphToBack.fillOval(200, 300, 600, 600);
    time++; 
    twoDGraph.drawImage(back, null, 0, 0);
}

}

主要课程:

import javax.swing.JFrame;
import java.awt.Component;

public class GolfRunner extends JFrame {

    private static final int width = 1000;
    private static final int height = 800;

    public GolfRunner() {
        super("Golf Simulator");
        setSize(width,height); 
        FieldComponent golfgame = new FieldComponent();
        ((Component)golfgame).setFocusable(true); 
        getContentPane().add(golfgame);
        setVisible(true);
    }

    public static void main(String args[]) {
        GolfRunner run = new GolfRunner();
    }

}

1 个答案:

答案 0 :(得分:2)

我修改了您的代码以符合当代Swing标准。

  1. 您必须从调用SwingUtilities invokeLater方法开始,将Swing组件放在Event Dispatch thread(EDT)上。

  2. 您使用Swing组件,例如JFrame。扩展Swing组件的唯一原因是当您想要覆盖其中一个组件方法时。

  3. 不要使用像Canvas这样的AWT组件。使用JPanel作为画布。

  4. Swing会自动双重缓冲图形。您不必在单独的BufferedImage上绘图。

  5. 您在JPanel中设置了首选大小,而不是JFrame。设置图纸的大小,JFrame pack方法将处理JFrame的大小。

  6. 我把这些类放在一起,以便更容易粘贴。你应该把课程分开。

    package com.ggl.testing;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GolfRunner implements Runnable {
    
        private static final int    width   = 1000;
        private static final int    height  = 800;
    
        private JFrame frame;
    
        @Override
        public void run() {
            frame = new JFrame();
            frame.setTitle("Golf Simulator");
            FieldComponent golfgame = new FieldComponent();
            ((Component) golfgame).setFocusable(true);
            frame.getContentPane().add(golfgame);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(new GolfRunner());
        }
    
        public class FieldComponent extends JPanel {
    
            private static final long   serialVersionUID    = 
                    -6481773088613540357L;
    
            private int             time;
    
            public FieldComponent() {
                this.time = 1200;
                this.setPreferredSize(new Dimension(width, height));
            }
    
            @Override
            protected void paintComponent(Graphics window) {
                super.paintComponent(window);
    
                Graphics2D twoDGraph = (Graphics2D) window;
    
                if (time % 2 == 0) {
                    twoDGraph.setColor(Color.RED);
                } else {
                    twoDGraph.setColor(Color.GREEN);
                }
                twoDGraph.fillOval(100, 100, 600, 600);
    
                time++;
            }
    
        }
    
    }