使用for循环与actionListener对象的java绘图



我想知道为什么我不能使用for循环来使用相同的效果,而使用带有actionlistener对象的内部类来刷新图像上的点图。以下是代码:

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class MyCanvas
{
    JLabel view;
    BufferedImage surface;
    Random random = new Random();
    public void initMyCanvas()
    {
        surface = new BufferedImage(1000,1000,BufferedImage.TYPE_INT_RGB);
        view = new JLabel(new ImageIcon(surface));
        Graphics g = surface.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0,0,1000,1000);
        ActionListener listener=new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                int[] xLoc = new int[10]; 
                int[] yLoc = new int[10];
                System.out.println("drawing..");
                Graphics g = surface.getGraphics();
                g.setColor(Color.ORANGE);
                g.fillRect(0,0,1000,1000);
                Random rn=new Random();
                for (int ji=0;ji<10;ji++){
                    xLoc[ji]=rn.nextInt(500);
                    yLoc[ji]=rn.nextInt(500);
                }
                drawNodes(xLoc,yLoc,g);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Timer timer = new Timer(1000, listener);
        timer.start();
    }
    public static void main(String[] args)
    {
        MyCanvas canvas = new MyCanvas();
        canvas.initMyCanvas();
        JFrame frame = new JFrame();
        int vertexes = 0;
        vertexes = 10;
        int canvasSize = vertexes * vertexes;
        frame.setSize(canvasSize, canvasSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(canvas.view);        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    public void drawNodes(int[] x, int[] y, Graphics g)
    {
            // Treat each location as a 10x10 block. If position 1,1 then go to (5,5) - If position 3,5 then go to (25, 45) eg: (x*10)-5, (y*10)-5
            g.setColor(Color.white);
            System.out.println(" In drawNodes");
            for (int i=0;i<x.length;i++){
                g.fillOval(x[i], y[i], 8, 8);
            }
            g.dispose();
            view.repaint();
    }
}

与用for循环实现的方法相比,前者多次迭代代码的同一"部分"以创建绘图。

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class MyCanvas
{
    JLabel view;
    BufferedImage surface;
    Random random = new Random();
    public void initMyCanvas()
    {
        surface = new BufferedImage(1000,1000,BufferedImage.TYPE_INT_RGB);
        view = new JLabel(new ImageIcon(surface));
        Graphics g = surface.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0,0,1000,1000);
        for (int i=0; i<5;i++){
            int[] xLoc = new int[10]; 
            int[] yLoc = new int[10];
            System.out.println("drawing..");
            g.setColor(Color.ORANGE);
            g.fillRect(0,0,1000,1000);
            Random rn=new Random();
            for (int ji=0;ji<10;ji++){
                xLoc[ji]=rn.nextInt(500);
                yLoc[ji]=rn.nextInt(500);
            }
            drawNodes(xLoc,yLoc,g);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        MyCanvas canvas = new MyCanvas();
        canvas.initMyCanvas();
        JFrame frame = new JFrame();
        int vertexes = 0;
        vertexes = 10;
        int canvasSize = vertexes * vertexes;
        frame.setSize(canvasSize, canvasSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(canvas.view);        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    public void drawNodes(int[] x, int[] y, Graphics g)
    {
            // Treat each location as a 10x10 block. If position 1,1 then go to (5,5) - If position 3,5 then go to (25, 45) eg: (x*10)-5, (y*10)-5
            g.setColor(Color.white);
            System.out.println(" In drawNodes");
            for (int i=0;i<x.length;i++){
                g.fillOval(x[i], y[i], 8, 8);
            }
            g.dispose();
            view.repaint();
    }
}

我之所以这么问,是因为我正在Xtend中编写等效的实现,作为特定领域语言构建工作的一部分;我在编写内部类和/或实现ActionListener接口方面遇到了问题。再说一遍,这对我自己来说也是一件很好的事情!!

方法2的问题是在EDT中调用循环,因为它会阻塞并且无法绘制组件。阅读Swing Concrency。

1) 摆动Timer适用于有间隔的绘图。有了定时器,您就不必使用Thread.sleep(1);

2) 您可以尝试使用SwingWorker进行重新绘制。你可以找到很多这样的例子。

3) 也使用paintComponent()方法中的自定义绘画观看下一个示例并阅读oracle

最新更新