import javax.swing.*; import java.awt.*; public class MyRunnable extends JFrame implements Runnable{ int x=100,y=100; static int c=1; Thread ball1,ball2; //声明两个线程; MyRunnable(){ super("MyRunnable"); setSize(400,400); setVisible(true); ball1=new Thread(this); //(1) ball2=new Thread(this); ball1.start(); //(2) ball2.start(); } public void run(){ while(true){ if(Thread.currentThread()==ball1){ // (3) x=x+50; // (4) if(x>800){x=100;} //控制小球的横坐标范围 if(y>600){y=100;} repaint(); } if(Thread.currentThread()==ball2){ y=y+50; if(x>800){x=100;} if(y>600){y=100;} repaint(); } try{ ball1.sleep(800); //(5) ball2.sleep(100); }catch(InterruptedException e){} } } public void paint(Graphics g){ c++; if(c%3==0) g.setColor(Color.red); //(6) else if(c%3==1) g.setColor(Color.green);解释划线句子的功能
时间: 2024-03-04 17:51:58 浏览: 128
1. 创建了两个线程对象ball1和ball2,并将该对象设置为这个类的实例(this)。
2. 启动了两个线程对象ball1和ball2。
3. 使用if语句判断当前线程对象是哪一个,ball1还是ball2。
4. 在ball1线程中,每次循环将小球的横坐标x增加50,如果x>800,则将x设为100,控制小球在水平方向上移动的范围。
5. 使用sleep方法使ball1线程睡眠800毫秒,控制小球的移动速度。
6. 在paint方法中,根据计数器c的值,设置小球的颜色为红色或绿色。
相关问题
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();
}
});
}
}
```
现在代码就可以正常运行了。
import javax.swing.*; import java.awt.*; public class DrawCircle extends JPanel implements Runnable{ int angle=0; Thread t; public DrawCircle(){ this.setPreferredSize(new Dimension(500,500)); this.setBackground(Color.blue); t=new Thread(this); t.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.fillArc(50, 50, 300, 300, 0, angle); } public static void main(String args[]){ JFrame f=new JFrame("画扇形"); f.getContentPane().add(new DrawCircle()); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void run() { // TODO Auto-generated method stub try{ for(int i=0;i<10;i++){ Thread.sleep(100); angle=angle+30; repaint();//重画 重新调用paintComponent } }catch(Exception ex){ } } }
这是一个Java程序,用于在窗口中绘制一个圆形扇形。程序中使用了JPanel和Thread两个类来实现动态绘制效果。
程序的主要代码如下:
```
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JPanel implements Runnable {
int angle = 0;
Thread t;
public DrawCircle() {
this.setPreferredSize(new Dimension(500,500));
this.setBackground(Color.blue);
t = new Thread(this);
t.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillArc(50, 50, 300, 300, 0, angle);
}
public static void main(String args[]) {
JFrame f = new JFrame("画扇形");
f.getContentPane().add(new DrawCircle());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
try {
for(int i = 0; i < 10; i++) {
Thread.sleep(100);
angle = angle + 30;
repaint(); //重画 重新调用paintComponent
}
} catch(Exception ex) {
}
}
}
```
程序中首先创建了一个JFrame对象,用于显示绘制结果。然后创建一个DrawCircle对象作为JPanel容器,设置其大小和背景色,并将其添加到JFrame中。接着创建一个新的线程对象,并通过start方法启动该线程。线程的主体是run方法,在其中使用了for循环来控制圆形扇形的绘制,每次循环都休眠100ms,并更新扇形的角度。最后调用JPanel的repaint方法,重新绘制整个扇形。程序中使用了try-catch结构来捕获异常,保证程序的稳定性。
这个程序的运行结果是在一个蓝色的窗口中绘制出一个不断变化的圆形扇形。
阅读全文