gpt4 book ai didi

Java重绘不更新

转载 作者:行者123 更新时间:2023-11-30 03:38:08 25 4
gpt4 key购买 nike

我在编码时遇到一个问题,当我在任何地方调用 repaint 时,它不会更新图像,但程序仍然继续运行:

import java.applet.*; 
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class mainMenu extends JPanel implements MouseListener{
Graphics g;
Image playButton = Toolkit.getDefaultToolkit().getImage("game sprites/play_button.png");
private int part = 0;

public static void main(String[] a) {
mainMenu mm = new mainMenu();

mm.makeJFrame();
}
public void makeJFrame(){
JFrame f = new JFrame();
f.setSize(1000, 600);
f.add(new mainMenu());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.addMouseListener(this);
f.setTitle("MAIN MENU");
}

public void paintComponent(Graphics g) {
if(part == 0)
System.out.println("0");
g.drawImage( playButton, 330, 200, 294, 102, this);
if(part == 1)
{
System.out.println("1");
g.setColor(Color.RED);
g.fillRect(0, 0, 40, 40);
}
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();

if(part == 0)
{
if(x > 330 && x < 624 && y > 232 && y < 334)
{
System.out.println("a");
part = 1;
repaint();
}
}
}
public void mouseReleased(MouseEvent e) {}
}

我测试了很多次,问题出在重绘上,程序可以工作,但图像在不应该的情况下保持不变。它打印出:000a(当我点击按钮时)即使我单击按钮,也不会再次打印出“a”,因为部分设置为 1,所以不应该打印出“a”。

最佳答案

图片加载了吗?这看起来很可疑:Image playButton = Toolkit.getDefaultToolkit().getImage("game sprites/play_button.png");。图像存储在哪里?

这看起来很可疑......

if (part == 0)
System.out.println("0");
g.drawImage(playButton, 330, 200, 294, 102, this);

并且可能意味着......

if (part == 0) {
System.out.println("0");
g.drawImage(playButton, 330, 200, 294, 102, this);
}

您已经破坏了绘制链,您必须在进行任何自定义绘制之前调用 super.paintComponent...

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

最后,您有两个 mainMenu 实例...

第一个是屏幕上显示的......

f.add(new MainMenu());

第二个是响应鼠标事件并更新部分的状态

f.addMouseListener(this);

这与第一个无关。

您应该避免在组件中创建窗口(例如 JPanel),而是创建 MainMenu 的实例并将其添加到 JFrame 的实例中>,例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainMenu extends JPanel implements MouseListener {

Image playButton = Toolkit.getDefaultToolkit().getImage("...");
private int part = 0;

public static void main(String[] a) {
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 MainMenu());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public MainMenu() {
addMouseListener(this);
}

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

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(part);
if (part == 0) {
System.out.println("0");
g.drawImage(playButton, 0, 0, this);
} else if (part == 1) {
System.out.println("1");
g.setColor(Color.RED);
g.fillRect(0, 0, 40, 40);
}
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();

if (part == 0) {
System.out.println("a");
part = 1;
repaint();
}
}

public void mouseReleased(MouseEvent e) {
}
}

关于Java重绘不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350304/

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