Java 如何在jframe中清除
我有一个移动球的情况。然而,在与队列搏斗之后,repaint()将paintComponent(图形g)放入队列中。我立即求助于使用油漆。现在的问题是,没有访问super.paintComponent(g)的权限,我不知道如何在每次绘制之前清除画布。因此,我的问题的一个可能的答案是一种自己清理画布的方法。我还发现线程可能是一个可行的解决方案,但在多次尝试实现该想法后,我仍然不明白,因此如果有人能够为此展示正确的实现,我将非常感激 这是我的密码:Java 如何在jframe中清除,java,swing,jframe,Java,Swing,Jframe,我有一个移动球的情况。然而,在与队列搏斗之后,repaint()将paintComponent(图形g)放入队列中。我立即求助于使用油漆。现在的问题是,没有访问super.paintComponent(g)的权限,我不知道如何在每次绘制之前清除画布。因此,我的问题的一个可能的答案是一种自己清理画布的方法。我还发现线程可能是一个可行的解决方案,但在多次尝试实现该想法后,我仍然不明白,因此如果有人能够为此展示正确的实现,我将非常感激 这是我的密码: import java.awt.Graphic
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);
}
}
}
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.*;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入javax.swing.JButton;
公共类PhysicsEngine扩展了JPanel{
双x,y;
潘杰潘;
按钮b1;
按钮b2;
JButton b3;
JButton b4;
JButton b5;
公共静态void main(字符串[]args){
JFrame=新的JFrame(“球形发动机”);
PhysicsEngine gui=新PhysicsEngine();
frame.add(gui、BorderLayout.CENTER);
框架。设置尺寸(600600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
公共物理引擎(){
b1=新的按钮(“1”);
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件){
试一试{
startFirst();
}捕获(中断异常异常){
}
}
});
本条增补(b1);
b2=新的按钮(“2”);
b2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
试一试{
startSecond();
}捕获(中断异常异常){
}
}
});
本条增补(b2);
b3=新的按钮(“3”);
b3.addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
试一试{
startThird();
}捕获(中断异常异常){
}
}
});
本条增补(b3);
b4=新的按钮(“4”);
b4.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
试一试{
startFourth();
}捕获(中断异常异常){
}
}
});
本条增补(b4);
b5=新的按钮(“5”);
b5.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
试一试{
开始第五次();
}捕获(中断异常异常){
}
}
});
本条增补(b5);
}
公共组件(图形g){
超级组件(g);
系统输出打印项次(“+y”);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.RED);
g2d.椭圆形((int)x,(int)y,30,30);
}
public void startFirst()引发InterruptedException{
x=300;
y=80;
//xPos=0*t*t+0*t+300这是300时的常数
对于(int t=1;t您的问题不是“绘制队列”,而是您在EDT上下文中调用Thread.sleep
违反了API的单线程特性
不要将for loop
s与Thread.sleep
一起使用,而应该使用Swing计时器
,它更新变量的状态paintComponent
所依赖的(并调用repaint
)
SwingTimer
可以看作是一个伪循环,它的ActionListener
在EDT的上下文中被调用,从而可以安全地从
首先看一看和了解更多细节
作为一个概念性的例子
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();
}
}
}
您的问题不是“绘制队列”,而是您在EDT@MadProgrammer那我该怎么解决呢?