“如何”将归结为“什么”。
一般来说,如果你自己动手绘制东西,你会发现将东西绘制到GridPane
上更容易,而不是依赖布局管理器之类的东西。
例如。这创建了一个简单的interface
,使用单个方法将事物绘制到网格上。
public interface GridShape {
public void draw(Graphics2D g2d, JComponent parent);
}
然后,这是由什么都想要画到网格
public class WaveShape implements GridShape {
@Override
public void draw(Graphics2D g2d, JComponent parent) {
g2d.setColor(Color.RED);
int xDiff = parent.getWidth()/4;
int height = parent.getHeight() - 1;
int xPos = 0;
GeneralPath path = new GeneralPath();
path.moveTo(0, 0);
path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
xPos += xDiff;
path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
xPos += xDiff;
path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
xPos += xDiff;
path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
g2d.draw(path);
}
}
的GridPane
然后绘制本身,然后画什么都“形”它(这个例子是非常基本的实现,但你可以有一个setter从而改变其如果需要涂或,“形”,有一个List
它允许你同时绘制多个形状)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestGrid {
public static void main(String[] args) {
new TestGrid();
}
public TestGrid() {
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 GridPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GridPane extends JPanel {
private WaveShape waveShape;
public GridPane() {
waveShape = new WaveShape();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());
g2d.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);
g2d.dispose();
// I don't trust you
g2d = (Graphics2D) g.create();
waveShape.draw(g2d, this);
g2d.dispose();
}
}
public interface GridShape {
public void draw(Graphics2D g2d, JComponent parent);
}
public class WaveShape implements GridShape {
@Override
public void draw(Graphics2D g2d, JComponent parent) {
g2d.setColor(Color.RED);
int xDiff = parent.getWidth()/4;
int height = parent.getHeight() - 1;
int xPos = 0;
GeneralPath path = new GeneralPath();
path.moveTo(0, 0);
path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
xPos += xDiff;
path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
xPos += xDiff;
path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
xPos += xDiff;
path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
g2d.draw(path);
}
}
}
让你'Shape'类某种真棒“绘制”类的,你的'GridPane'可以画画,这将是简单 – MadProgrammer
@MadProgrammer我如何才能控制到GridPanel中的paintComponent()方法中借鉴? – chaoqunli
通常你会对你想要绘制的东西有一些参考,可能在'List'中,这取决于你的需要 – MadProgrammer