Java 从线程调用repaint()
我只是想弄清楚线程是如何工作的(也就是说,我是个初学者)。除此之外,本程序不起任何作用 但是,它并没有按预期工作。我的目标是使圆圈变得越来越亮,以便它消失在背景中。但是线程中的repaint()调用似乎不起作用 这是我的密码:Java 从线程调用repaint(),java,multithreading,swing,repaint,Java,Multithreading,Swing,Repaint,我只是想弄清楚线程是如何工作的(也就是说,我是个初学者)。除此之外,本程序不起任何作用 但是,它并没有按预期工作。我的目标是使圆圈变得越来越亮,以便它消失在背景中。但是线程中的repaint()调用似乎不起作用 这是我的密码: public class Run { public static void main(String[] args) { new Circle(); } } import java.awt.Color; import java.awt.
public class Run {
public static void main(String[] args) {
new Circle();
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Circle extends JFrame implements Runnable {
private boolean running = false;
private Thread t;
Color currentColor;
int currentRed, currentGreen, currentBlue;
public Circle() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(150,150));
start();
repaint();
pack();
setVisible(true);
}
@Override
public void paint(Graphics g) {
//super.paint(g);
//currentColor = g.getColor();
//currentRed = currentColor.getRed();
//currentGreen = currentColor.getGreen();
//currentBlue = currentColor.getBlue();
g.setColor(new Color(currentRed++,currentGreen++,currentBlue++));
g.fillOval(7,30,100,100);
}
public synchronized void stop() {
running = false;
t.interrupt();
}
public synchronized void start() {
System.out.println("start");
if (!running) {
running = true;
t = new Thread(this);
t.start();
}
}
@Override
public void run() {
while(running) {
try {
Thread.sleep(5);
System.out.println("run");
repaint();
System.out.println("repaint");
} catch(InterruptedException e) {
}
}
}
}
我还尝试调用paint(),但我知道我不应该这样做,而且无论如何都不起作用。这对我来说有点道理。但我不清楚,为什么重新绘制不起作用…以下是代码的工作版本。请注意以下评论:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Circle extends JFrame implements Runnable {
private boolean running = false;
private Thread t;
int currentRed = 0, currentGreen = 0, currentBlue = 0;
public Circle() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(150,150));
//repaint(); //not really needed here
pack();
setVisible(true);
start();
}
@Override
public void paint(Graphics g) {
g.setColor(new Color(currentRed,currentGreen,currentBlue));
g.fillOval(7,30,100,100);
//modify color and make sure it is valid
currentRed = ++currentRed % 256;
currentGreen = ++currentGreen % 256; //Equivalent to: currentGreen = currentGreen == 255 ? 0 : currentGreen+1;
currentBlue = ++currentBlue % 256;
}
public synchronized void stop() {
running = false;
t.interrupt();
}
public synchronized void start() {
if (!running) {
running = true;
t = new Thread(this);
t.start();
}
}
@Override
public void run() {
while(running) {
try {
Thread.sleep(5);
repaint();
} catch(InterruptedException e) { //do not mute exceptions
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Circle());
}
}
结合您在评论中获得的许多技巧的更好的实现是:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Circle extends JPanel { //do custom painting on a JPanel
private boolean running = false;
private int currentRed = 0, currentGreen = 0, currentBlue = 0;
private final Timer timer; //use swing tools to update gui
private static final int DELAY = 5, W = 100, H = 100, OFFSET = 25;
public Circle() {
timer = new Timer(DELAY, e -> repaint());
setPreferredSize(new Dimension(W + 2*OFFSET,H + 2*OFFSET));
}
public static void createAndShowGui(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Circle circle = new Circle();
frame.add(circle);
frame.pack();
frame.setVisible(true);
circle.start();
}
@Override
public void paintComponent(Graphics g) { //for custom painting override paintComponent
super.paintComponent(g); //always call super
g.setColor(new Color(currentRed,currentGreen,currentBlue));
g.fillOval(OFFSET,OFFSET,W,H);
//modify color and make sure it is valid
currentRed = ++currentRed % 256;
currentGreen = ++currentGreen % 256;
currentBlue = ++currentBlue % 256;
}
public void stop() {
running = false;
timer.stop();
}
public void start() {
if (!running) {
running = true;
timer.start();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
你睡了5毫秒,你一定很快就会发现圆圈消失了Repoint()不见了??告诉我们你有什么例外吗?在画完椭圆形后,你正在设置图形的颜色。先做吧。用
g.setColor(新颜色(currentRed++、currentGreen++、currentBlue++)替换绘制方法的主体;g、 圆形(7,30,100,100)代码>是的。我刚刚测试过。发布一个完整的最小示例(我的意思是完整的:我必须能够复制和粘贴代码并执行它,而不必做任何修改),准确地告诉您预期会发生什么以及会发生什么。“不行”这个词太含糊了。