首先...
瞭解在Swing繪畫作品如何,看Painting in AWT and Swing和Performing Custom Painting。
建議您覆蓋的paintComponent
代替paint
並始終確保你打電話super.paintComponent
public class MyCanvas extends JComponent {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fill3DRect(20, 20, 200, 200,true);
}
}
其次...
有許多方法你可能做到這一點,我個人'd只是利用[Graphics2D
和Shape
的API](2D Graphics),但有些事告訴我對你來說不夠好,所以...
而不是試圖用基於組件的形狀(和要爭取佈局管理器),創建它描述了「繪製」元素的接口,這東西可畫...
public interface Drawable {
public Rectangle getBounds();
public void setBounds(Rectangle bounds);
public Color getColor();
public void setColor(Color color);
public void draw(Graphics2D g2d);
}
這描述的東西,可以繪製,地點和什麼顏色
接下來,保持Drawables
的List
一個S和使用paintComponent
方法來畫他們......
public class MyCanvas extends JComponent {
private List<Drawable> drawables;
public MyCanvas() {
drawables = new ArrayList<>(25);
}
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void add(Drawable drawable) {
drawables.add(drawable);
repaint();
}
public void remove(Drawable drawable) {
drawables.remove(drawable);
repaint();
}
public int getDrawableCount() {
return drawables.size();
}
public Drawable getDrawableAt(int index) {
return drawables.get(index);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
for (Drawable d : drawables) {
d.draw(g2d);
}
g2d.dispose();
}
}
現在你有一些畫和someth可以畫出他們...
第三...
做些事情來繪製。我會用一個抽象Drawable
涵蓋了所有常見的東西,使生活更輕鬆的開始......
public abstract class AbstractDrawable {
private Rectangle bounds;
private Color color
public AbstractDrawable(Rectangle bounds, Color color) {
setBounds(bounds);
setColor(color);
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Dimension bounds) {
this.bounds = bounds;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public abstract void draw(Graphics2D g2d);
}
的,你可以去堅果...
public class DrawableRectangle {
public AbstractDrawable(Rectangle bounds, Color color) {
super(bounds, color);
}
public abstract void draw(Graphics2D g2d) {
g2d.setColor(getColor());
g2d.fill(getBounds());
}
}
和/或...
public class Drawable3DRectangle {
public AbstractDrawable(Rectangle bounds, Color color) {
super(bounds, color);
}
public abstract void draw(Graphics2D g2d) {
g2d.setColor(getColor());
Rectangle bounds = getBounds();
g2d.fill3DRect(bounds.x, bounds.y, bounds.width, bounds.height);
}
}
第四...
把它放在一起......
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 DrawableShapes {
public static void main(String[] args) {
new DrawableShapes();
}
public DrawableShapes() {
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 TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private MyCanvas canvas;
public TestPane() {
setLayout(new BorderLayout());
canvas = new MyCanvas();
add(canvas);
canvas.add(new Drawable3DRectangle(new Rectangle(20, 20, 100, 100), Color.RED));
canvas.add(new DrawableRectangle(new Rectangle(20, 140, 100, 100), Color.RED));
}
}
}
好了,所以這個油漆2個繪圖資源,但如何將他們?
基本上,你搶你要移動,有點像Drawable
參考...
Drawable drawable = canvas.getDrawableAt(0);
然後修改它的位置...
drawable.getBounds().x += 5;
drawable.getBounds().y += 5;
然後你重繪MyCanvas
實例...
canvas.repaint();
首先,瞭解繪畫在Swing中的工作原理,參見[繪畫AWT和Swing](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/ )。 – MadProgrammer 2014-11-22 08:48:33
我正在閱讀並仍在尋找解決方案 – 2014-11-22 09:10:03
*「我嘗試了一些方法..」*向我們展示您在[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)中嘗試的內容。 – 2014-11-22 09:10:10