使用while循环和repaint()方法时遇到了一大堆麻烦。正如您在此代码中看到的那样,我放了2行来确定代码是否正常工作。那些是'System.out.println(x +“”+ y +“Running”);'和'System.out.println(“paint”);'。第一个工作正常。我检查了控制台。但第二个不起作用。任何人都可以帮忙吗?为了清楚说明,我提出了一些额外的解释。
[此代码是一个线程,并在这样的main方法中的actionPerformed方法中调用。 “
giraffe.addActionListener(new ActionListener() {//giraffe is an Jbutton.
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String tmp = e.getActionCommand();
if (tmp.equals("Giraffe")) {
AnimalObJects1 g1 = new AnimalObJects1(x, y);
array.add(g1);
panel.add(g1);
new Thread(g1).start();
}
}
});
“]
package Zoo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.*;
import java.util.*;
import javax
.swing.*;
public class AnimalObJects1 extends JPanel implements Animal, Runnable {
int x, y;
Rectangle2D.Double rectangle;
Ellipse2D.Double ellipse;
Line2D.Double line;
Color color = new Color(0, 0, 0);
ArrayList<Object> components = new ArrayList<Object>();
ArrayList<Color> colors = new ArrayList<Color>();
Random generator = new Random();
public AnimalObJects1(int x, int y) {
this.x = x;
this.y = y;
this.setOpaque(false);
this.setPreferredSize(new Dimension(400, 400));
}
public void move() {
}
public void run() {
while (true) {
System.out.println(x + " " + y + "Running");
int a = generator.nextInt(4);
if (a == 0)
x = x + generator.nextInt(30);
else if (a == 1)
y = y + generator.nextInt(30);
else if (a == 2)
x = x - generator.nextInt(30);
else
y = y - generator.nextInt(30);
this.setLocation(x, y);
this.repaint();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
System.out.println("paint");
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
g2.scale(0.5, 0.5);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(992, 296, 78, 520);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(780, 108, -1, 0);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(597, 582, 473, 234);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(828, 759, 242, 56);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(943, 245, 127, 51);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(991, 224, 128, 65);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1008, 188, 111, 73);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1008, 225, 211, 46);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1007, 188, 27, -39);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1009, 153, 34, 37);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1087, 150, 32, 38);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(948, 44, -112, 3);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1010, 138, 33, 15);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1088, 137, 32, 12);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1196, 224, 23, 18);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 255);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1065, 215, 51, 26);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 255);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1067, 64, 0, 0);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1081, 212, 23, 30);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(981, 37, 0, 0);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(671, 773, 62, 160);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(749, 783, 51, 149);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(941, 792, 52, 131);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1018, 784, 54, 132);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(194, 382, 0, 0);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(671, 915, 42, 128);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(750, 913, 35, 130);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(938, 899, 45, 135);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1021, 893, 40, 140);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(671, 1043, 43, 30);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(751, 1039, 33, 32);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(938, 1032, 44, 27);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1021, 1033, 38, 24);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(454, 670, 164, 38);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(947, 43, -1, 0);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(454, 677, 38, 26);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
ellipse = new Ellipse2D.Double(244, 110, 0, 0);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(671, 901, 23, 19);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(749, 898, 25, 29);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(939, 886, 20, 11);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(939, 887, 23, 25);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(255, 255, 50);
g2.setColor(color);
rectangle = new Rectangle2D.Double(1020, 879, 30, 26);
g2.fill(rectangle);
colors.add(color);
components.add(rectangle);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1028, 303, 34, 44);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1006, 373, 21, 34);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1036, 424, 27, 31);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1002, 497, 31, 40);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1039, 555, 22, 39);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(967, 645, 59, 38);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(672, 631, 55, 32);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(789, 601, 37, 27);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(761, 668, 33, 24);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(677, 745, 27, 34);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(874, 703, 54, 36);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(795, 779, 31, 17);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(994, 754, 58, 36);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(689, 852, 21, 18);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(766, 950, 11, 21);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(959, 834, 23, 32);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(950, 982, 20, 23);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1034, 905, 16, 28);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
ellipse = new Ellipse2D.Double(1011, 244, 17, 22);
g2.fill(ellipse);
colors.add(color);
components.add(ellipse);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1113, 209, 1112, 209);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1068, 210, 1068, 210);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1068, 210, 1112, 208);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1112, 208, 1067, 208);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1067, 208, 1111, 207);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1111, 207, 1066, 206);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1066, 206, 1061, 212);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1061, 212, 1067, 211);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1067, 211, 1064, 210);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1064, 210, 1065, 209);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1065, 209, 1065, 209);
g2.draw(line);
colors.add(color);
components.add(line);
color = new Color(0, 0, 0);
g2.setColor(color);
line = new Line2D.Double(1065, 209, 1066, 209);
g2.draw(line);
colors.add(color);
components.add(line);
g2.scale(2, 2);
}
}
答案 0 :(得分:0)
虽然不完全确定这是否是问题&#34; GUI事物&#34;像重新绘制等等应始终由事件线程执行,在你的情况下
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
this.setLocation(x, y);
this.repaint();
});