「如何」將歸結爲「什麼」。
一般來說,如果你自己動手繪製東西,你會發現將東西繪製到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