gpt4 book ai didi

java - 对象类未绘制到屏幕上

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

我目前正在开发一个小型编码项目,但遇到了一个问题。我回顾了我过去的工作,我似乎无法弄清楚为什么这个程序不会调用paint方法。目前我只是想在框架上画一个圆圈。

以下内容为我尝试绘制的简单圆圈创建窗口和对象类。

public class Main {

public static void main(String[] args) {

final int WIDTH = 700, HEIGHT = 900;

JFrame frame = new JFrame("Physics Demo");
JPanel content = new JPanel();

content.setLayout(new GridLayout(1, 0, 0, 0));

Character ball = new Character(WIDTH, HEIGHT);

Timer changeFrame = new Timer (100, ball);

frameSetup(frame, content, WIDTH, HEIGHT, ball, changeFrame);

}

public static void frameSetup(JFrame frame, JPanel content, int WIDTH, int HEIGHT, Character ball, Timer changeFrame){

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(content);

content.add(ball);

frame.addKeyListener(ball);

frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));

frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);

frame.setVisible(true);
changeFrame.start();

}

}

下面的类是对象类,当我运行程序时,我从控制台得到响应。角色触发一次(理应如此),并且 actionPreformed 方法与计时器一起循环运行。由于某种原因它不运行绘图类。

public class Character extends JPanel implements ActionListener, KeyListener{
/* Identify the Objects values and physics,
* Characters weight, size and properties are below.
*
*/
private static final long serialVersionUID = 1L;

final int characterRadius = 30;

final double characterWeight = 0.5;

int characterY, characterX;

boolean bouncy;

public Character(int WIDTH, int HEIGHT){

System.out.println("Character called upon... " + WIDTH);

}

public void characterObject(Graphics g, int WIDTH, int HEIGHT){

super.paint(g);

System.out.println("characterObject graphics called upon... " + WIDTH);

g.setColor(Color.BLUE);
g.fillOval(350, 450, characterRadius, characterRadius);

}

/*
* Ball does not have any player interactions
*/

@Override
public void keyPressed(KeyEvent buttonPressed) {


}

@Override
public void keyReleased(KeyEvent arg0) {

}

@Override
public void keyTyped(KeyEvent arg0) {


}

//******************************************

@Override
public void actionPerformed(ActionEvent arg0) {

System.out.println("actionPreformed called upon...");

repaint();

}

}

我已经进行了一段时间的试验和错误,但我似乎无法弄清楚,所以我将此作为最后的手段。

如果需要,我可以提供更多信息。

最佳答案

为什么要从 characterObject 调用 super.paint?这不是定制绘画的工作原理。您无法控制绘制过程,API 可以控制

您需要重写当 API 希望重新绘制组件时调用的方法之一。作为一般建议,例如,这将是 paintComponent 方法

public class Character extends JPanel implements ActionListener, KeyListener {

/* Identify the Objects values and physics,
* Characters weight, size and properties are below.
*
*/
private static final long serialVersionUID = 1L;

final int characterRadius = 30;

final double characterWeight = 0.5;

int characterY, characterX;

boolean bouncy;

public Character(int WIDTH, int HEIGHT) {

System.out.println("Character called upon... " + WIDTH);

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
System.out.println("characterObject graphics called upon... " + WIDTH);

g.setColor(Color.BLUE);
g.fillOval(350, 450, characterRadius, characterRadius);

}

/*
* Ball does not have any player interactions
*/
@Override
public void keyPressed(KeyEvent buttonPressed) {

}

@Override
public void keyReleased(KeyEvent arg0) {

}

@Override
public void keyTyped(KeyEvent arg0) {

}

//******************************************
@Override
public void actionPerformed(ActionEvent arg0) {

System.out.println("actionPreformed called upon...");

repaint();

}
}

我建议阅读 Performing Custom PaintingPainting in Swing有关 Swing 中绘画实际工作原理的更多详细信息。

我还建议您查看 How to use Key Bindings作为 KeyListener 的替代品,这将解决您下一个明显的问题

您可能还想阅读Java Coding Conventions ,这会让其他人更容易阅读你的代码,也让你更容易阅读别人的代码。

您将宽度高度传递给Character构造函数,但忽略它们,我建议您去需要将这些值分配给实例字段并在 paintComponent 方法中使用它们

关于java - 对象类未绘制到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46535138/

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