gpt4 book ai didi

java - 修复后覆盖未调用的绘制方法

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

我的问题本质上是创建一个具有几种不同场景的物理引擎。我想通过单独运行每个场景的按钮将不同的场景合并到一个窗口中。框架工作正常,按钮显示并可以按下;然而, pain 方法中的打印行永远不会发生,从那里我得出结论,即使在重新绘制之后,也不会调用绘制。我知道它们不一样,但我不明白为什么在这种情况下与其他情况相比,油漆没有被访问。

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

public class PhysicsEngine extends JPanel{
double x ,y;

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

public static void main(String[] args){
PhysicsEngine gui = new PhysicsEngine();
}

public PhysicsEngine(){
frame = new JFrame("Ball Engine");

pan = new JPanel();
frame.add(pan);

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

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


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

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

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

frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g) {
super.paint(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;

for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615) break; //stops the loop when the ball is off the screen

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

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

for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615) break; //stops the loop when the ball is off the screen

y = .1*t*t - 10*t + 550; //parametric equation for y
repaint();
Thread.sleep(10);
}
}

public void startThird() throws InterruptedException{
for(int t = 1;;t++){
if(y>=615||x>=615) break; //stops the loop when the ball is off the screen

y = .1*t*t - 10*t + 550; //parametric equation for y
x = 0*t*t + 5*t + 50; //parametric equation for x
repaint();
Thread.sleep(10);
}
}

public void startFourth() throws InterruptedException{
for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615||x>=615) break; //stops the loop when the ball is off the screen

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

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

最佳答案

基本问题是您正在重写PhysicsEngine 类的paint() 方法。但您永远不会将此类的实例添加到框架中。

但是,更大的问题是类(class)的结构。您的主类不应该只是为了创建 JFrame 而扩展 JPanel。创建框架的逻辑应位于 main() 方法中,然后您的 PysicsEngine 面板应包含您想要为框架构建的所有组件。另外,自定义绘制应该通过重写 paintComponent(...) 方法而不是 Paint() 方法来完成。

阅读 Swing 教程中关于 Custom Painting 的部分了解基本绘画信息和演示。

然后您可以查看教程中的其他部分,以获得更好的方法来构建代码。例如,如何使用按钮教程中的 ButtonDemo 代码将向您展示如何扩展 JPanel 并向其添加按钮。

关于java - 修复后覆盖未调用的绘制方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47186207/

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