Java:为我的框架定义的绘制方法在从另一个 class 引用时不会调用 repaint()
Java: A defined paint method for my frame won't call repaint() when referenced from another class
我试图从另一个 class 调用此构造函数,但是当我创建新线程时,它从来没有调用重绘。如果我只是从 class 中的 main 方法调用 运行() 方法,它会完全按预期工作。似乎我的 repaint() 方法没有被另一个 class 调用。我担心我的另一个class还在计算,但即使把它放在一个线程中,我也没有运气。
代码:
public class MakeTransparent implements Runnable{
private int i;
private static int dollars;
private float transparency;
public MakeTransparent(int dollars){
this.setDollars(dollars);
this.transparency = 1;
this.i = 300;
}
@Override
public void run() {
JFrame frame = new JFrame("Tiddlybiscuits"){
private static final long serialVersionUID = 1L;
public void paint(Graphics arg0) {
Graphics2D g2 = (Graphics2D)arg0;
super.paint(g2);
g2.setColor(getBackground());
g2.fillRect(0, 0, this.getWidth(),this.getHeight());
g2.setColor(Color.CYAN);
g2.setFont(g2.getFont().deriveFont(50, 200F));
//System.out.println(arg0.getFontMetrics());
String string = "+$";
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparency());
g2.setComposite(c);
g2.drawString(string + getDollars(), 30, getI());
try {Thread.sleep(40);} catch (InterruptedException e) {}
setI(getI()-5);
setTransparency(getTransparency()-.03f);
repaint();
}
};
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setBackground(new Color(1,55,134,0));
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try { Thread.sleep(1400); } catch (InterruptedException e) { }
frame.dispose();
}
- 永远不要从事件调度线程的上下文中调用
Thread.sleep
- 不要在 paint 方法中调用任何可能导致
repaint
发生的方法,这将导致无限更新循环,最终会消耗你的 CPU
- 避免覆盖
paint
顶级容器,如 JFrame
,除了没有双缓冲,大多数 windows 包含其他组件(JRootPane
、contentPane
, glassPane
) 所有这些都会干扰您尝试绘制的内容
- Swing 是单线程框架,不是线程安全的。这意味着你不应该在事件调度线程的上下文中执行任何长时间的 运行 或阻塞操作,你也不应该在事件调度线程之外创建或修改任何 UI 组件。
- 只有一个 EDT,所有 UI 代码都将 运行 在其中。像绘画这样的事情总是在 EDT 中自动执行。
看看:
- Painting in AWT and Swing
- Performing Custom Painting
- Concurrency in Swing
- Worker Threads and SwingWorker
- How to use Swing Timers
有关如何在框架要求内工作的更多详细信息和想法
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane tp = new TestPane();
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float t = tp.getTransparancy() - 0.03f;
if (t < 0) {
t = 0;
((Timer)e.getSource()).stop();
}
int i = tp.getI();
tp.setI(i - 1);
System.out.println(t);
tp.setTransparancy(t);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer.start();
}
});
}
public class TestPane extends JPanel {
private float transparancy = 1f;
private int i = 200;
private int dollars;
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
public void setTransparancy(float value) {
if (transparancy != value) {
float old = transparancy;
this.transparancy = value;
firePropertyChange("transparancy", old, transparancy);
repaint();
}
}
public float getTransparancy() {
return transparancy;
}
public int getDollars() {
return dollars;
}
public int getI() {
return i;
}
public void setI(int value) {
if (i != value) {
float old = i;
this.i = value;
firePropertyChange("i", old, i);
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getBackground());
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.CYAN);
g2d.setFont(g2d.getFont().deriveFont(50, 200F));
//System.out.println(arg0.getFontMetrics());
String string = "+$";
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparancy());
g2d.setComposite(c);
g2d.drawString(string + getDollars(), 30, getI());
g2d.dispose();
}
}
}
我试图从另一个 class 调用此构造函数,但是当我创建新线程时,它从来没有调用重绘。如果我只是从 class 中的 main 方法调用 运行() 方法,它会完全按预期工作。似乎我的 repaint() 方法没有被另一个 class 调用。我担心我的另一个class还在计算,但即使把它放在一个线程中,我也没有运气。
代码:
public class MakeTransparent implements Runnable{
private int i;
private static int dollars;
private float transparency;
public MakeTransparent(int dollars){
this.setDollars(dollars);
this.transparency = 1;
this.i = 300;
}
@Override
public void run() {
JFrame frame = new JFrame("Tiddlybiscuits"){
private static final long serialVersionUID = 1L;
public void paint(Graphics arg0) {
Graphics2D g2 = (Graphics2D)arg0;
super.paint(g2);
g2.setColor(getBackground());
g2.fillRect(0, 0, this.getWidth(),this.getHeight());
g2.setColor(Color.CYAN);
g2.setFont(g2.getFont().deriveFont(50, 200F));
//System.out.println(arg0.getFontMetrics());
String string = "+$";
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparency());
g2.setComposite(c);
g2.drawString(string + getDollars(), 30, getI());
try {Thread.sleep(40);} catch (InterruptedException e) {}
setI(getI()-5);
setTransparency(getTransparency()-.03f);
repaint();
}
};
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setBackground(new Color(1,55,134,0));
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try { Thread.sleep(1400); } catch (InterruptedException e) { }
frame.dispose();
}
- 永远不要从事件调度线程的上下文中调用
Thread.sleep
- 不要在 paint 方法中调用任何可能导致
repaint
发生的方法,这将导致无限更新循环,最终会消耗你的 CPU - 避免覆盖
paint
顶级容器,如JFrame
,除了没有双缓冲,大多数 windows 包含其他组件(JRootPane
、contentPane
,glassPane
) 所有这些都会干扰您尝试绘制的内容 - Swing 是单线程框架,不是线程安全的。这意味着你不应该在事件调度线程的上下文中执行任何长时间的 运行 或阻塞操作,你也不应该在事件调度线程之外创建或修改任何 UI 组件。
- 只有一个 EDT,所有 UI 代码都将 运行 在其中。像绘画这样的事情总是在 EDT 中自动执行。
看看:
- Painting in AWT and Swing
- Performing Custom Painting
- Concurrency in Swing
- Worker Threads and SwingWorker
- How to use Swing Timers
有关如何在框架要求内工作的更多详细信息和想法
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane tp = new TestPane();
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float t = tp.getTransparancy() - 0.03f;
if (t < 0) {
t = 0;
((Timer)e.getSource()).stop();
}
int i = tp.getI();
tp.setI(i - 1);
System.out.println(t);
tp.setTransparancy(t);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer.start();
}
});
}
public class TestPane extends JPanel {
private float transparancy = 1f;
private int i = 200;
private int dollars;
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
public void setTransparancy(float value) {
if (transparancy != value) {
float old = transparancy;
this.transparancy = value;
firePropertyChange("transparancy", old, transparancy);
repaint();
}
}
public float getTransparancy() {
return transparancy;
}
public int getDollars() {
return dollars;
}
public int getI() {
return i;
}
public void setI(int value) {
if (i != value) {
float old = i;
this.i = value;
firePropertyChange("i", old, i);
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getBackground());
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.CYAN);
g2d.setFont(g2d.getFont().deriveFont(50, 200F));
//System.out.println(arg0.getFontMetrics());
String string = "+$";
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparancy());
g2d.setComposite(c);
g2d.drawString(string + getDollars(), 30, getI());
g2d.dispose();
}
}
}