gpt4 book ai didi

Java:当从另一个类引用时,为我的框架定义的绘制方法不会调用 repaint()

转载 作者:行者123 更新时间:2023-12-01 22:28:12 25 4
gpt4 key购买 nike

我试图从另一个类调用这个构造函数,但是当我创建新线程时,它永远不会调用重绘。如果我只是从类中的 main 方法调用 run() 方法,它就会完全按照预期工作。似乎我的 repaint() 方法没有从其他类调用。我担心我的其他类(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
  • 请勿在绘制方法中调用任何可能导致重绘的方法,这将导致无限更新循环,最终消耗您的 CPU
  • 避免覆盖顶级容器(例如 JFrame)的 paint,除了没有双缓冲之外,大多数窗口还包含其他组件(JRootPanecontentPaneglassPane)所有这些都会干扰您尝试绘制的内容
  • Swing 是一个单线程框架,并且不是线程安全的。这意味着您永远不应该在事件调度线程的上下文中执行任何长时间运行或阻塞的操作,并且永远不应该从事件调度线程外部创建或修改任何 UI 组件。
  • 只有一个 EDT,所有 UI 代码都将在 in 中运行。诸如绘画之类的事情始终在 EDT 中自动执行。

看一下:

有关如何在框架要求内工作的更多详细信息和想法

Fade

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();
}

}

}

关于Java:当从另一个类引用时,为我的框架定义的绘制方法不会调用 repaint(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28313302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com