TestFigurePanel.java + FigurePanel

本文介绍了一个图形绘制面板FigurePanel的实现,该面板可以根据不同类型的参数绘制线、矩形、圆角矩形和椭圆等基本图形,并支持填充效果。同时提供了一个示例应用TestFigurePanel来展示这些图形。
摘要由CSDN通过智能技术生成

在原书中 TestFigurePanel是在 FigurePanel 前面的 如果之编写TestFigurePanel.java  是会报错的   所以  小心哦

package graphics;

//放在包 graphics中

import javax.swing.*;
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;


    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.paintComponent(g);
        int width = getSize().width;
        int height = getSize().height;


        switch (type) {
        case LINE:
            g.setColor(Color.BLACK);
            g.drawLine(10, 10, width - 10, height - 10);
            g.drawLine(width - 10, 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);
            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)(height*0.8));
                break;
        }
    }
    public void setType(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 getPreferredSise(){
        return new Dimension(80, 80);
    }

}

///

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


public class TestFigurePanel extends JFrame {
public TestFigurePanel(){
setLayout(new GridLayout(2, 3, 5, 5));
add(new FigurePanel(FigurePanel.LINE));//调用上头的FigurePanel     
add(new FigurePanel(FigurePanel.RECTANGLE));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
add(new FigurePanel(FigurePanel.OVAL));
add(new FigurePanel(FigurePanel.RECTANGLE, true));
add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));
}

public static void main(String[]args){
TestFigurePanel frame = new TestFigurePanel();
frame.setSize(400, 200);
frame.setTitle("TestFigurePanel");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值