简体   繁体   English

Java重绘不会更新

[英]Java repaint doesn't update

I encountered a problem when I was coding, When I call repaint anywhere, it doesn't update the image but the program still keeps running: 我在编码时遇到问题,当我在任何地方调用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) {}
}

I tested lots of times, and the problem is with the repaint, the program works, but the image stays the same when it shouldn't. 我测试了很多次,问题出在重绘上,程序可以正常工作,但是在不需要的时候图像保持不变。 It prints out: 0 0 0 a (when I click on the button) and doesn't print out "a" again even if I click on the button, which it shouldn't since part is set to 1. 它会打印出:0 0 0 a(当我单击按钮时),即使我单击按钮也不会再次打印出“ a”,这是不应该的,因为part设置为1。

Does the image load? 图像加载了吗? This looks suspicious: Image playButton = Toolkit.getDefaultToolkit().getImage("game sprites/play_button.png"); 这看起来很可疑: Image playButton = Toolkit.getDefaultToolkit().getImage("game sprites/play_button.png"); . Where are the images stored? 图像存储在哪里?

This looks suspicious... 这看起来可疑...

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

And is probably meant to be... 而且可能是...

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

You've broken the paint chain, you MUST call super.paintComponent before you do any custom painting... 您已经中断了绘画链,必须super.paintComponent任何自定义绘画之前调用super.paintComponent

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

And finally, you have two instances of mainMenu ... 最后,您有两个mainMenu实例...

The first is the one you have on the screen... 第一个是屏幕上显示的那个...

f.add(new MainMenu());

The second is the one that is responding to the mouse events and updating the state of part 第二个是响应鼠标事件并更新part状态的控件

f.addMouseListener(this);

Which has nothing to do with the first. 与第一个无关。

You should avoid making a windows within components (like JPanel ) and instead create an instance of MainMenu and add it to an instance of JFrame , for example... 您应该避免在组件(如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) {
    }
}

In Java an if statement only affects one statement after the head of the statement. 在Java中,if语句仅影响语句开头之后的一个语句。 So you need to put the entire body in {}. 因此,您需要将整个正文放在{}中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM