帮我精简以下代码:while (isDrawing1) { move += gap; repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } while (isDrawing2) { move += gap; repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } while (isDrawing3) { move += gap; repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } while (isDrawing4) { move += gap; repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } }
时间: 2024-02-11 10:09:55 浏览: 22
你可以将重复的代码提取出来,只保留一次即可。可以使用数组或集合来存储 `isDrawing` 变量,然后遍历数组或集合,对每个元素执行相同的操作。例如:
```
boolean[] isDrawing = {isDrawing1, isDrawing2, isDrawing3, isDrawing4};
while (true) {
for (boolean isDrawingItem : isDrawing) {
if (isDrawingItem) {
move += gap;
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在这个例子中,我们使用了一个布尔型数组 `isDrawing` 来存储 `isDrawing1` 到 `isDrawing4` 四个变量。然后,我们使用 `for` 循环遍历该数组,对每个数组元素执行相同的操作。这样,我们就避免了重复代码的问题。
相关问题
import java.util.*; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class HeartAnimation extends JPanel implements Runnable { private List<Point> heartPoints; private int size; private Color color; private int x; private int y; public HeartAnimation(int size, Color color, int x, int y) { this.size = size; this.color = color; this.x = x; this.y = y; this.setPreferredSize(new Dimension(size, size)); this.heartPoints = new ArrayList<>(); this.createHeart(); } private void createHeart() { for (double t = 0; t <= Math.PI * 2; t += 0.01) { int x = (int) (16 * Math.pow(Math.sin(t), 3)); int y = (int) (-13 * Math.cos(t) + 5 * Math.cos(2 * t) + 2 * Math.cos(3 * t) + Math.cos(4 * t)); Point point = new Point(x * 10, y * 10); this.heartPoints.add(point); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(this.color); for (Point point : this.heartPoints) { int x = point.x + this.x; int y = point.y + this.y; g.drawLine(x, y, x, y); } } @Override public void run() { while (true) { try { Thread.sleep(10); this.x += 1; this.y += 1; this.repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); HeartAnimation heartAnimation = new HeartAnimation(400, Color.RED, 100, 100); frame.add(heartAnimation); frame.pack(); frame.setVisible(true); Thread thread = new Thread(heartAnimation); thread.start(); } }帮我检查一下这个代码为什么无法运行
这份代码存在两个类的定义,需要将第二个类 `HeartAnimation` 的代码复制到第一个类 `HelloWorld` 的后面并将其删除。此外,在 `main` 方法中启动线程时,应该使用 `SwingUtilities.invokeLater()` 方法以确保在事件分派线程中启动线程,代码如下:
```
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Thread thread = new Thread(heartAnimation);
thread.start();
}
});
```
修改后的代码如下:
```
import java.util.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HelloWorld extends JPanel implements Runnable {
private List<Point> heartPoints;
private int size;
private Color color;
private int x;
private int y;
public HelloWorld(int size, Color color, int x, int y) {
this.size = size;
this.color = color;
this.x = x;
this.y = y;
this.setPreferredSize(new Dimension(size, size));
this.heartPoints = new ArrayList<>();
this.createHeart();
}
private void createHeart() {
for (double t = 0; t <= Math.PI * 2; t += 0.01) {
int x = (int) (16 * Math.pow(Math.sin(t), 3));
int y = (int) (-13 * Math.cos(t) + 5 * Math.cos(2 * t) + 2 * Math.cos(3 * t) + Math.cos(4 * t));
Point point = new Point(x * 10, y * 10);
this.heartPoints.add(point);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.color);
for (Point point : this.heartPoints) {
int x = point.x + this.x;
int y = point.y + this.y;
g.drawLine(x, y, x, y);
}
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
this.x += 1;
this.y += 1;
this.repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HelloWorld heartAnimation = new HelloWorld(400, Color.RED, 100, 100);
frame.add(heartAnimation);
frame.pack();
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Thread thread = new Thread(heartAnimation);
thread.start();
}
});
}
}
```
现在代码就可以正常运行了。
while (true){ repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); }优化这段代码
这段代码是一个无限循环,每100毫秒重绘一次界面。为了优化这段代码,可以将Thread.sleep(100)替换为Object.wait(100),这样可以避免线程被中断的问题,并且在等待期间可以释放对象锁,以便其他线程可以访问该对象。修改后的代码如下:
```
while (true){
repaint();
try {
synchronized(this) {
wait(100);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
```
另外,如果界面不需要实时重绘,可以考虑使用定时器(Timer)或者线程池(ThreadPoolExecutor)来控制重绘频率,以便更好地控制系统资源的使用。