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

时间:2013-11-18 18:55:02

标签: java swing graphics actionlistener bufferedimage

我想知道为什么我不能通过使用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循环实现的那个迭代在同一个&#39;部分&#39;多次编码以创建图表。

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接口时遇到问题。而且,对我自己来说,这也是一件很神奇的事情!

1 个答案:

答案 0 :(得分:0)

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

1)Swing Timer适合于间隔绘图。使用计时器,您无需使用Thread.sleep(1);

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

3)也可以在paintComponent()方法中使用自定义绘画,然后观看exampleread oracle