Java重绘不会更新

时间:2014-12-08 02:02:00

标签: java awt 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) {}
}

我测试了很多次,问题在于重绘,程序是否有效,但是图像保持不变。打印出来: 0 0 0 a(当我点击按钮时) 即使单击按钮也不会再打印出“a”,因为部分设置为1,所以不应该打印出来。

2 个答案:

答案 0 :(得分:2)

图像是否加载?这看起来很可疑: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());

第二个是响应鼠标事件并更新part

状态的事件
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) {
    }
}

答案 1 :(得分:0)

在Java中,if语句只影响语句头部之后的一个语句。所以你需要将整个身体放在{}。

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