Java绘画不起作用 [英] Java Painting not working
问题描述
所以我有这段代码:
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保持空白.为什么是这样?我的代码有什么问题,我该如何解决?
What I would like it to do is draw 4 lines onto my JFrame in a TicTacToe fashion, but the JFrame remains blank. Why is this? What is wrong with my code and how should I fix it?
推荐答案
如果您实际上创建了TicTacToe
的实例,而不是JFrame
,则您的DrawingPane
可能会绘制/覆盖框架的内容通过paint
方法绘画.
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
JFrame
包含许多子组件,包括JRootPane
和contentPane
,您可以在这些子组件上放置自己的组件.
A JFrame
contains a number of child components, including the JRootPane
and the contentPane
, onto which you place your own components.
针对您的情况,更好的解决方案可能是使用DrawingPane
并自定义paintComponent
来渲染网格
A better solution in your case is probably to use your DrawingPane
and customise it's paintComponent
to render the grid
请参见 AWT和Swing中的绘画和执行自定义绘画以获取更多详细信息
See Painting in AWT and Swing and Performing Custom Painting for more details
例如...
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
扩展,除了遇到的自定义绘画问题之外,不要在类中添加任何功能,并且要将自己锁定为一次性使用情况(很难再次使用该类)
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)
这篇关于Java绘画不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!