創造新的class稱為DrawOval:
import
java.awt.*;
import
javax.swing.*;
public
class
DrawOval
extends
JPanel{
private
int
d = 10;
public
void
paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval(10,
10, d, d);
→ 寬度和高度是變數d
}
public
void
setD(int
newD){
d
= (newD >= 0 ? newD : 10); →
如果newD大於0,等於這個數字,如果不是就是10
repaint();
→ 重繪這個橢圓形(內建的method)
}
public
Dimension getPreferredSize(){
return
new
Dimension(200,200);
}
public
Dimension getMinimumSize(){
return
getPreferredSize();
}
}
沒有留言:
張貼留言