Java绘画不起作用

时间:2015-06-25 01:15:37

标签: java swing jframe jpanel custom-painting

所以我有这段代码:

package tictactoe;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TicTacToe extends JFrame {

public static void main(String[] args) {
    JFrame masterFrame = new JFrame("TicTacToe");
    JPanel drawingPanel = new JPanel();
    GameBoard theGame = new GameBoard();

    masterFrame.add(drawingPanel);
    masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    masterFrame.setSize(504, 504);
    masterFrame.setResizable(false);
    masterFrame.setLocationRelativeTo(null);
    masterFrame.setVisible(true);
} //End of Main method


@Override
public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    //Drawing the GridLines
    g2.drawLine(168, 0, 168, 500);
    g2.drawLine(336, 0, 336, 500);
    g2.drawLine(0, 168, 500, 168);
    g2.drawLine(0, 336, 500, 336);
} //End of Paint Method
} //End of TicTacToe Class    

我希望它能以TicTacToe方式在我的JFrame上绘制4行,但JFrame仍为空白。为什么是这样?我的代码有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

如果您实际创建了TicTacToe的实例,而不是JFrame,那么您的DrawingPane可能会通过它paint绘制/覆盖您的框架正在绘制的内容方法

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

子组件可以在不通知或要求绘制父容器的情况下进行绘制,这意味着子组件实际上将覆盖先前由框架绘制的内容,这可能会产生一些非常奇怪的结果,因为不同的部件得到更新

JFrame包含许多子组件,包括您放置自己组件的JRootPanecontentPane

RootPane

在您的情况下,更好的解决方案可能是使用您的DrawingPane并自定义paintComponent来呈现网格

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

例如......

TicTacToe

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    

作为一般规则,建议不要直接从JFrame扩展,除了您所拥有的自定义绘画问题之外,您不会在课程中添加任何功能,而是将自己锁定在单个用例(再次使用该类很难)