你是在找這個嗎?
您可以在單獨的.Java文件中自由聲明類MyCircle
和DrawingPane
。
我相信這會回答「我希望能夠將顏色部署爲參數並從另一個類的另一個方法調用該方法。」
public class TestingX12 {
public static void main(String args[]) {
new TestingX12();
}
public TestingX12() {
//create list of circles
List<MyCircle> circList = new ArrayList<MyCircle>();
circList.add(new MyCircle(new Rectangle(100, 20, 120, 30), Color.red));
circList.add(new MyCircle(new Rectangle(150, 50, 80, 50), Color.yellow));
circList.add(new MyCircle(new Rectangle(30, 90, 30, 110), Color.blue));
DrawingPane dp = new DrawingPane(circList);
JFrame frame = new JFrame("JToolTip Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(dp);
frame.setSize(400, 450);
frame.setVisible(true);
}
class MyCircle {
Rectangle rectangle;
Color color;
public MyCircle(Rectangle r, Color c) {
this.rectangle = r;
this.color = c;
}
}
public class DrawingPane extends JPanel {
List<MyCircle> circles;
public DrawingPane(List<MyCircle> circles) {
this.circles = circles;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.get(i).rectangle;
g.setColor(circles.get(i).color);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
System.out.println("Drawing...");
}
}
}
}
看到我的回答http://stackoverflow.com/questions/7809838/jpanel-drawing-with-a-specific-color/7810287#7810287 – gtiwari333