面向对象练习

package com.lovoinfo.shape;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class DrawingFrame extends JFrame {
    private BufferedImage offImage = new BufferedImage(900, 600, 1);
    private String temp = "Circle";
    private Shape shape = null;
    private List<Shape> shapelist = new ArrayList<Shape>();
    private JButton circleButton = new JButton();
    private JButton rectButton = new JButton();
    private JButton trigonButton = new JButton();

    public DrawingFrame() {
        this.setSize(800, 600);
        this.setTitle("绘图");
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        circleButton = new JButton("Circle");
        rectButton = new JButton("Rect");
        trigonButton = new JButton("Trigon");
        this.setLayout(null);
        this.add(circleButton);
        circleButton.setBounds(200, 20, 100, 30);
        this.add(rectButton);
        rectButton.setBounds(400, 20, 100, 30);
        this.add(trigonButton);
        trigonButton.setBounds(600, 20, 100, 30);

        ActionListener cilck = new CilckButton();
        circleButton.addActionListener(cilck);
        rectButton.addActionListener(cilck);
        trigonButton.addActionListener(cilck);
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if(temp.equals("Circle")){
                    shape = new Circle(MyUtil.random(10, 250)); 
                }
                else if(temp.equals("Rect")){
                    shape = new Rect(MyUtil.random(10, 300),MyUtil.random(20, 400));
                }
                else {
                    shape = new Trigon(MyUtil.random(100, 300));
                } 


                shape.setX(e.getX());
                shape.setY(e.getY());
                shape.setColor(MyUtil.randomColor());
                shapelist.add(shape);

                repaint();
            }
        });
    }

        private class CilckButton implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent e) {
                temp = e.getActionCommand();

            }

        }




    @Override
    public void paint(Graphics g) {
        Graphics g2 = offImage.getGraphics();
        super.paint(g2);
        for(Shape temp : shapelist){
            temp.draw(g2);
        }
        if(shape != null) {
            shape.draw(g2);
            g2.drawString(
                String.format("周长: %.2f", shape.perimeter()), 
                shape.getX(), shape.getY());
            g2.drawString(
                String.format("面积: %.2f", shape.area()), 
                shape.getX(), shape.getY() + 50);
        }

    g.drawImage(offImage, 0, 0, null);
    }
    public static void main(String[] args) {
        new DrawingFrame().setVisible(true);
    }
}
package com.lovoinfo.shape;

import java.awt.Graphics;

/**
 * 能否在窗口上绘图的接口
 * @author Carry
 *
 */
public interface Drawable {

    /**
     * 绘图
     * @param g 画笔对象
     */
    public abstract void draw(Graphics g);
}
package com.lovoinfo.shape;

import java.awt.Color;

/**
 * 自定义工具类
 * @author Carry
 *
 */
public final class MyUtil {

    private MyUtil() {
    }

    /**
     * 产生指定范围的随机整数
     * @param min 最小值(闭区间)
     * @param max 最大值(闭区间)
     * @return 指定范围的随机整数
     */
    public static int random(int min, int max) {
        return (int) (Math.random() * (max - min + 1) + min);
    }

    /**
     * 生成随机颜色
     * @return Color对象
     */
    public static Color randomColor() {
        int r = random(0, 255);
        int g = random(0, 255);
        int b = random(0, 255);
        return new Color(r, g, b);
    }
}
package com.lovoinfo.shape;

import java.awt.Color;

/**
 * 图形(抽象类)
 * @author Carry
 *
 */
public abstract class Shape implements Drawable {
    protected int x;            // 中心的横坐标
    protected int y;            // 中心的纵坐标
    protected Color color;      // 颜色

    /**
     * 计算面积
     * @return 图形的面积
     */
    public abstract double area();

    /**
     * 计算周长
     * @return 图形的周长
     */
    public abstract double perimeter();

    public void setColor(Color color) {
        this.color = color;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
package com.lovoinfo.shape;

import java.awt.Graphics;

/**
 * 圆
 * @author Carry
 *
 */
public class Circle extends Shape {
    private int radius;     // 半径

    /**
     * 构造器
     * @param radius 半径
     */
    public Circle(int radius) {
        this.radius = radius;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }

}
package com.lovoinfo.shape;

import java.awt.Graphics;

public class Rect extends Shape {
    private int width;      // 宽
    private int height;     // 高

    /**
     * 构造器
     * @param width 宽
     * @param height 高
     */
    public Rect(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawRect(x - width / 2, y - height / 2, width, height);
    }

    @Override
    public double area() {
        return width * height;
    }

    @Override
    public double perimeter() {
        return (width + height) << 1;
    }

}
package com.lovoinfo.shape;

import java.awt.Graphics;

public class Trigon extends Shape {
    private int length;// 边长

    public Trigon(int length) {
        super();
        this.length = length;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawLine(x, (int) (y - length / 2 / 0.866), x - length / 2,
                (int) (y + length / 2 * 0.577));
        g.drawLine(x, (int) (y - length / 2 / 0.866), x + length / 2,
                (int) (y + length / 2 * 0.577));
        g.drawLine(x - length / 2, (int) (y + length / 2 * 0.577), x + length
                / 2, (int) (y + length / 2 * 0.577));
    }

    @Override
    public double area() {
        return (length / 2 * Math.sqrt(3)) * length / 2;
    }

    @Override
    public double perimeter() {
        return length * 3;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值