关于swing的问题

逛青楼的道长 2016-10-22 11:58:09
import javax.swing.JPanel;
import java.awt.*;
public class FigurePanel extends JPanel{
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int ROUND_RECTANGLE = 3;
public static final int OVAL = 4;
private int type = 1;
private boolean filled = false;
public FigurePanel(){

}
public FigurePanel(int type){
this.type = type;
}
public FigurePanel(int type,boolean filled){
this.type = type;
this.filled = filled;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
switch(type){
case LINE:
g.setColor(Color.BLACK);
g.drawLine(10, 10, width-10, height-10);
g.drawLine(width-1, 10, 10, height-10);
break;
case RECTANGLE:
g.setColor(Color.BLUE);
if(filled)
g.fillRect((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
else
g.drawRect((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
break;
case ROUND_RECTANGLE:
g.setColor(Color.RED);
if(filled)
g.fillRoundRect((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height),20,20);
else
g.drawRoundRect((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height),20,20);
case OVAL:
if(filled)
g.fillOval((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
else
g.drawOval((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
}
}
public void seyType(int type){
this.type = type;
repaint();
}
public int getType(){
return type;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public boolean getFilled(){
return filled;
}
public Dimension getPreferredSize(){
return new Dimension(80,80);
}


}
这个类的width和height都没有初始化,为什么能画出图形呢?,还有那个Dimension这个东西用来做什么的,注释掉也能运行,结果都一样
...全文
75 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧