作业:窗口绘图

来源:互联网 发布:淘宝卖家支付宝限额 编辑:程序博客网 时间:2024/05/19 14:51

自定义工具

package com.lovoinfo.zeng;

import java.awt.Color;

/**
* 自定义工具类
* @author jackfrued
*
*/
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.zeng;import java.awt.Graphics;/** * 能否在窗口上绘图的接口 * @author jackfrued * */public interface Drawable {    /**     * 绘图     * @param g 画笔对象     */    public abstract void draw(Graphics g);}

父类

package com.lovoinfo.zeng;import java.awt.Color;/** * 图形(抽象类) * @author jackfrued * */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.zeng;import java.awt.Graphics;/** * 圆 * @author jackfrued * */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.zeng;import java.awt.Graphics;/** * 矩形 * @author Administrator * */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.zeng;/** * 三角形 */import java.awt.Graphics;public class Trigon extends Shape {    private int length;//边长/** * 构造器 * @param length */    public Trigon(int length) {        super();        this.length = length;    }    public void setLength(int length) {        this.length = length;    }    @Override    public void draw(Graphics g) {        g.setColor(color);        g.drawLine(x, y, x + length, y);        g.drawLine(x + length, y, x + length / 2, y - (int) (Math.sqrt(3) * length / 2));        g.drawLine(x + length / 2, y - (int) (Math.sqrt(3) * length / 2), x, y);    }    /**     * 面积     */    @Override    public double area() {        int p = (length * 3) / 2;        return Math                .sqrt(p * (length - p) * p * (length - p) * p * (length - p));    }    /**     * 周长     */    @Override    public double perimeter() {        return length * 3;    }}

窗口运行

package com.lovoinfo.zeng;import java.awt.Color;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.util.ArrayList;import java.util.List;import javax.swing.JButton;import javax.swing.JFrame;@SuppressWarnings("serial")public class DrawingFrame extends JFrame {    private class Pushbutton implements ActionListener {        @Override        public void actionPerformed(ActionEvent e) {            type = e.getActionCommand();        }    }    private List<Shape> shapes = new ArrayList<Shape>();    private JButton CircleButton, RectButton, TrigonButton;    private Shape shape = null;    private String type = "Circ";    private boolean isMouseLeftButtonDown = false;    public DrawingFrame() {        this.setTitle("");        this.setSize(800, 600);        this.setResizable(false);        this.setLocationRelativeTo(null);        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        CircleButton = new JButton("Circle");        RectButton = new JButton("Rect");        TrigonButton = new JButton("Trigon");        ActionListener handler = new Pushbutton();        CircleButton.addActionListener(handler);        RectButton.addActionListener(handler);        TrigonButton.addActionListener(handler);        this.setLayout(null);        CircleButton.setBounds(100, 10, 60, 30);        this.add(CircleButton);        RectButton.setBounds(170, 10, 60, 30);        this.add(RectButton);        TrigonButton.setBounds(240, 10, 60, 30);        this.add(TrigonButton);        this.addMouseListener(new MouseAdapter() {            @Override            public void mousePressed(MouseEvent e) {                isMouseLeftButtonDown = e.getButton() == MouseEvent.BUTTON1;                if (isMouseLeftButtonDown) {                    if (type.equals("Circle")) {                        shape = new Circle(MyUtil.random(10, 500));                    } else if (type.equals("Rect")) {                        shape = new Rect(MyUtil.random(20, 500), MyUtil.random(                                20, 550));                    }else{                        shape = new Trigon(MyUtil.random(10, 550));                    }                }                shape.setX(e.getX());                shape.setY(e.getY());                Color color = MyUtil.randomColor();                shape.setColor(color);                repaint();            }        });    }    @Override    public void paint(Graphics g) {        super.paint(g);        if (shape != null) {            shape.draw(g);            g.drawString(String.format("周长: %.2f", shape.perimeter()),                    shape.getX(), shape.getY());            g.drawString(String.format("面积: %.2f", shape.area()), shape.getX(),                    shape.getY() + 15);        }    }    public static void main(String[] g3) {        new DrawingFrame().setVisible(true);    }}
0 0