使用for循环与actionListener对象的java绘图
我想知道为什么我不能通过使用for循环来使用相同的效果,而不是使用带有actionlistener对象的内部类来刷新图像上的点图。代码如下:使用for循环与actionListener对象的java绘图,java,swing,graphics,actionlistener,bufferedimage,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; Buf
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();
}
}
导入java.awt.image.buffereImage;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.Random;
公共类MyCanvas
{
JLabel视图;
缓冲图像表面;
随机=新随机();
public void initMyCanvas()
{
表面=新的BuffereImage(10001000,BuffereImage.TYPE_INT_RGB);
视图=新JLabel(新图像图标(曲面));
Graphics g=surface.getGraphics();
g、 setColor(颜色为橙色);
g、 fillRect(0,010001000);
ActionListener=新建ActionListener(){
已执行的公共无效行动(行动事件ae){
int[]xLoc=新的int[10];
int[]yLoc=新的int[10];
系统输出打印项次(“图纸”);
Graphics g=surface.getGraphics();
g、 setColor(颜色为橙色);
g、 fillRect(0,010001000);
Random rn=新的Random();
对于(int ji=0;ji方法2的问题是您在EDT中调用您的循环,因为它会阻塞并且无法绘制组件。请阅读
1) SwingTimer
适用于按间隔绘制。使用Timer,您无需使用Thread.sleep(1);
2) 你可以试着用它来重新粉刷。你可以找到很多这样的例子
3) 也可以在paintComponent()
方法中使用自定义绘画,然后观察下一步和下一步
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();
}
}