构造图像,线段,矩形,椭圆,圆角矩形

package thinking;

import java.awt.*;
import javax.swing.*;

public class My extends JFrame{
public My(){
setLayout(new GridLayout(2,3,5,5));
add(new FigurePanel(FigurePanel.LINE));//FigurePanel是自己定义的类,调用FigurePanel中的构造方法
add(new FigurePanel(FigurePanel.RECTANGLE));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
add(new FigurePanel(FigurePanel.OVAL,true));
add(new FigurePanel(FigurePanel.RECTANGLE,true));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));
}
public static void main(String[] args){
My frame = new My();
frame.setTitle("xx");
frame.setSize(600,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}


  

class FigurePanel extends JPanel { //构建FigurePanel类
      
    // 定义常量
    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;  
    }  
      
    public void paintComponent(Graphics g)  //方法
    {  
        super.paintComponents(g);  
          
        int width = getWidth();
        int height = getHeight();
          
        System.out.println("width = " + width + " heigth = "+ height);  
        System.out.println("width = " + ((int)width*0.1) + " heigth = "+ height);
        
        
        switch (type)  //类型
        {  
        case LINE:  //LINE类型时的行为
        {  
            g.setColor(Color.YELLOW);  
            g.drawLine(10, 10, width - 10, height - 10);  
            g.drawLine(width - 10, 10, 10, height - 10); 
            break;
        }  
        case RECTANGLE:  //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), 40, 40);  
            }  
            else  
            {  
                g.drawRoundRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height), 40, 40);  
            }
            break;
        }
        case OVAL://椭圆
        {
            g.setColor(Color.BLACK);  
            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 setType(int type)
    {
        this.type = type;
        repaint();//repaint方法调用会引起paintComponent方法被调用。repaint方法被调用以刷新视图区域
    }
    public int getType()
    {
        return type;
    }
    
    public void setFilled(boolean filled)
    {
        this.filled = filled; 
        repaint();//repaint方法调用会引起paintComponent方法被调用。repaint方法被调用以刷新视图区域
    }
    
    public boolean isFilled()  
    {  
        return filled;  
    }  
    
    public Dimension getPreferredSize()
    {
        return new Dimension(10, 10);  
    }  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值