gpt4 book ai didi

java - jframe中如何清除

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

我遇到了一种情况,我正在移动一个球。然而,在与 repaint() 放置 PaintComponent(Graphics g) 的队列作斗争之后。我求助于使用 PaintImmediately。现在的问题是,如果无法访问 super.paintComponent(g),我不知道如何在每次绘画之前清除 Canvas 。因此,我的问题的一个可能答案是一种自行清除 Canvas 的方法。我还发现线程可能是一个可能的解决方案,但在多次尝试实现该想法之后,我仍然不明白这一点,因此如果有人能够展示正确的实现,我将非常感激。

这是我的代码:

  import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;

public class PhysicsEngine extends JPanel {

double x, y;

JPanel pan;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;

public static void main(String[] args) {

JFrame frame = new JFrame("Ball Engine");
PhysicsEngine gui = new PhysicsEngine();
frame.add(gui, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public PhysicsEngine() {

b1 = new JButton("1");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
startFirst();
} catch (InterruptedException exception) {
}
}
});
this.add(b1);

b2 = new JButton("2");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startSecond();
} catch (InterruptedException exception) {
}
}
});
this.add(b2);

b3 = new JButton("3");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startThird();
} catch (InterruptedException exception) {
}
}
});
this.add(b3);

b4 = new JButton("4");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startFourth();
} catch (InterruptedException exception) {
}
}
});
this.add(b4);

b5 = new JButton("5");
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startFifth();
} catch (InterruptedException exception) {
}
}
});
this.add(b5);

}

public void paintComponent(Graphics g) {
super.paintComponent(g);

System.out.println("" + y);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillOval((int) x, (int) y, 30, 30);
}

public void startFirst() throws InterruptedException {
x = 300;
y = 80;

// xPos= 0*t*t + 0*t + 300 this is constant at 300
for(int t = 1; t<70;t++){

y = .1 * t * t + 0 * t + 80; // parametric equation for y
repaint();
paintImmediately((int)x, (int)y, 30, 30);
Thread.sleep(10);
}
}

public void startSecond() throws InterruptedException {
x = 300;
y = 550;

for (int t = 1;t<150; t++) {
// xPos= 0*t*t + 0*t + 300 this is constant at 300

y = .1 * t * t - 15 * t + 550; // parametric equation for y
repaint();
paintImmediately((int)x, (int)y, 30, 30);
Thread.sleep(10);
}
}

public void startThird() throws InterruptedException {
y = 550;
x = 50;

for (int t = 1;t<150; t++) {

y = .1 * t * t - 15 * t + 550; // parametric equation for y
x = 0 * t * t + 3 * t + 50; // parametric equation for x
repaint();
paintImmediately((int)x, (int)y, 30, 30);
Thread.sleep(10);
}
}

public void startFourth() throws InterruptedException {
y = 50;
x = -4;

for (int t = 1;t<110; t++) {
// xPos= 0*t*t + 0*t + 300 this is constant at 300

y = .001*t * t * t + 50; // given parametric equation for y
x = t - 4; // given parametric equation for x
repaint();
paintImmediately((int)x, (int)y, 30, 30);
Thread.sleep(10);
}
}

public void startFifth() throws InterruptedException {
for (int t = 1; t < 130 /* goes for 1.5 seconds */; t++) {
y = 200 * Math.sin(.05*t) + 300; // given parametric equation for y
x = 200 * Math.cos(.05*t) + 300; // given parametric equation for x
repaint();
paintImmediately((int)x, (int)y, 30, 30);
Thread.sleep(10);
}
}
}

最佳答案

您的问题不是“绘制队列”,而是您通过在 EDT 上下文中调用 Thread.sleep 违反了 API 的单线程性质。

您应该使用更新状态的 Swing Timer,而不是使用带有 Thread.sleepfor-loop paintComponent 依赖的变量的数量(并调用 repaint)

Swing Timer 可以被认为是一个伪循环,其 ActionListener 在 EDT 的上下文中调用,从而可以安全地更新 UI

首先查看 Concurrency in SwingHow to Use Swing Timers了解更多详情

作为一个概念示例...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
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();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private PhysicsPane physicsPane;

public TestPane() {
physicsPane = new PhysicsPane();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
JButton b1 = new JButton("1");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
startFirst();
}
});
this.add(b1, gbc);
this.add(physicsPane, gbc);
}

protected void startFirst() {
physicsPane.startFirst();
}

}

public class PhysicsPane extends JPanel {

private Timer timer;
private double xPos, yPos;
private int tick;

public PhysicsPane() {
setBackground(Color.BLUE);
}

protected void stopTimer() {
if (timer == null) {
return;
}
timer.stop();
timer = null;
}

public void startFirst() {
stopTimer();

xPos = 300;
yPos = 500;
tick = 0;

timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (tick >= 150) {
stopTimer();
return;
}

yPos = .1 * tick * tick - 15 * tick + 550;

tick++;
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fillOval((int) xPos, (int) yPos, 30, 30);
g2d.dispose();
}
}

}

关于java - jframe中如何清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209315/

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