简体   繁体   中英

Why isn't the paint() method being called at any point in this program?

This is my code for a puzzle game that I'm starting to work on. I'm trying to get it to display "Paused" in the middle of the screen, but couldn't manage to get that to work. It turns out, even with various repaints(), nothing is happening. Why? Is it because the class itself doesn't actually extend JPanel or JFrame?

package main;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PuzzleSquares implements ActionListener, KeyListener, Runnable {
boolean running = false;
boolean inGame = false;
boolean paused = false;

JFrame frame = new JFrame();
JPanel menu = new JPanel();
JPanel game = new JPanel();

Thread thread;

public PuzzleSquares() {    
    frame.requestFocus();
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Just another puzzle game");

    addMenu();

    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
}

public void addMenu() {
    menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
    menu.setFocusable(true);
    menu.setBackground(Color.GRAY);

    createButton("START", menu);
    createButton("HIGH SCORES", menu);
    createButton("EXIT", menu);

    frame.revalidate();
    frame.add(menu);
}

public void addGame() {
    game.setBackground(Color.GREEN);
    game.addKeyListener(this);
    game.setFocusable(true);
    game.setPreferredSize(frame.getMaximumSize());

    frame.add(game);
    frame.revalidate();
    game.requestFocus();
}

public void createButton(String title, Container c) {
    JButton button = new JButton(title) {
        {
            setSize(200, 30);
            setMaximumSize(getSize());
        }
    };
    button.setAlignmentX(JPanel.CENTER_ALIGNMENT);
    button.addActionListener(this);
    button.setActionCommand(title);

    c.add(button);
}

public synchronized void start() {
    if (running) { return; }

    running = true;

    thread = new Thread(this);
    thread.start();
}

public synchronized void stop() {
    if (!running) { return; }

    running = false;

    try { thread.join(); } catch (Exception e) {}
}

@Override
public void run() {
    while (running) {

    }
}

public void paint(Graphics g) {
    System.out.println("Hi");
    if (paused) {
        g.setColor(Color.BLACK);
        for (int i=0;i<100;i++) {
            g.drawLine(i, i+1, i, i+1);
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("START")) {
        System.out.println("Start");
        frame.remove(menu);
        addGame();
    }
    if (e.getActionCommand().equals("HIGH SCORES")) {
        System.out.println("High Scores");
    }
    if (e.getActionCommand().equals("EXIT")) {
        System.out.println("Exit");
    }
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        if (!paused) {
            System.out.println("Pause");
            paused = true;
        } else {
            paused = false;
        }
        game.repaint();
    }

}

@Override
public void keyReleased(KeyEvent e) {

}

public static void main(String[] args) {
    PuzzleSquares ps = new PuzzleSquares();
    ps.start();
}
}

Basically, your class isn't paintable, it does not extent from anything that could be painted by the framework (like JComponent or JPanel )

If you tried doing something like...

@Override
public void paint(Graphics g) {
    super.paint(g);
    System.out.println("Hi");
    if (paused) {
        g.setColor(Color.BLACK);
        for (int i=0;i<100;i++) {
            g.drawLine(i, i+1, i, i+1);
        }
    }
}

It wouldn't compile, because the method doesn't override any methods from it's parent class or interfaces.

See Painting in AWT and Swing and Performing Custom Painting for more details

I'd also discourage the use of KeyListener and would recommend the the use of the key bindings API instead, see How to Use Key Bindings . It will help solve your focus issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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