Java Painting not working

陌路散爱 提交于 2019-12-02 10:24:27

Had you actually created an instance of TicTacToe, instead of JFrame, your DrawingPane would likely be painting over/covering what ever your frame is painting via it's paint method.

// Create an instance of TicTacToe instead of JFrame...
//JFrame masterFrame = new JFrame("TicTacToe");
TicTacToe masterFrame = new TicTacToe();

A child component can be painted without notifying or requiring it's parent container to be painted, meaning that the child components will actually cover up what has previously been painted by the frame, this can produce some very weird results, as different parts get updated

A JFrame contains a number of child components, including the JRootPane and the contentPane, onto which you place your own components.

A better solution in your case is probably to use your DrawingPane and customise it's paintComponent to render the grid

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

For example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TicTacToe {

    public static void main(String[] args) {
        new TicTacToe();
    } //End of Main method

    public TicTacToe() {
        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("Tic Tac Toe");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TicTacToePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TicTacToePane extends JPanel {

        public TicTacToePane() {
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            //Drawing the GridLines
            g2d.drawLine(168, 0, 168, 500);
            g2d.drawLine(336, 0, 336, 500);
            g2d.drawLine(0, 168, 500, 168);
            g2d.drawLine(0, 336, 500, 336);
            g2d.dispose();
        }

    }

} //End of TicTacToe Class    

As a general rule, it's recommended not to extend directly from JFrame, apart from the custom painting issues you're having, you're not adding any functionality to the class and you're locking yourself into a single use case (it'd be hard to re-use the class again)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!