java.awt.Graphics

线

package com.even.shape;

import java.awt.Graphics;

public class Line extends Shape {

    @Override
    public void draw(Graphics g) {
        g.setColor(Color);
        g.drawLine(starx, stary, endx, endy);

    }
     public int getLength(){
            return   (int) Math.sqrt((getStarx() - getEndx())
                    * (getStarx() - getEndx())
                    + (getStary() - getEndy())
                    * (getStary() - getEndy()));
        }

}

椭圆

package com.even.shape;

import java.awt.Graphics;

public class Oval extends Shape {

    @Override
    public void draw(Graphics g) {
        g.setColor(Color);
        int width = Math.abs(endx - starx);    //横坐标的绝对值
        int height = Math.abs(endy - stary);   //纵坐标的绝对值

        int x = starx < endx? starx : endx;    
        int y = stary < endy? stary : endy;

        g.drawOval(x, y, width, height);

    }
     public int getArea() {
            int width = Math.abs(endx - starx);
            int height = Math.abs(endy - stary);
            return (int) (Math.PI * width / 2 * height / 2);
        }
     public int getPerimeter() {
            int width = Math.abs(endx - starx);
            int height = Math.abs(endy - stary);
            width = (width > height ? width : height);
            height = (width < height ? width : height);
            return (int) (2 * Math.PI * height / 2 + 4 * (width / 2 - height / 2));
        }

}

长方形

package com.even.shape;

import java.awt.Graphics;

public class Rectangle extends Shape {

    @Override
    public void draw(Graphics g) {
        g.setColor(Color);
        int width = Math.abs(endx - starx);    //横坐标的绝对值
        int height = Math.abs(endy - stary);   //纵坐标的绝对值

        int x = starx < endx? starx : endx;    
        int y = stary < endy? stary : endy;

        g.drawRect(x, y, width, height);

    }
     public int getArea() {
            int width = Math.abs(endx - starx);
            int height = Math.abs(endy - stary);
            return width * height;
        }
     public int getPerimeter() {
            int width = Math.abs(endx - starx);
            int height = Math.abs(endy - stary);
            return 2 * (width + height);
        }

}

超类图形


package com.even.shape;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 图形  (抽象类,不能实例化)
 */
public abstract class Shape {
    protected int starx;    //起点横坐标
    protected int stary;    //类推
    protected int endx;
    protected int endy;

    protected Color Color;  //颜色

    public Color getColor() {
        return Color;
    }

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

    /**
     * 抽象方法(留给子类去实现)
     * @param g 画笔
     */
    public abstract void draw(Graphics g);

    public int getStarx() {
        return starx;
    }

    public void setStarx(int starx) {
        this.starx = starx;
    }

    public int getStary() {
        return stary;
    }

    public void setStary(int stary) {
        this.stary = stary;
    }

    public int getEndx() {
        return endx;
    }

    public void setEndx(int endx) {
        this.endx = endx;
    }

    public int getEndy() {
        return endy;
    }

    public void setEndy(int endy) {
        this.endy = endy;
    }
}


我的窗口设计

package com.even.ui;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import com.even.shape.Line;
import com.even.shape.Oval;
import com.even.shape.Rectangle;
import com.even.shape.Shape;
import com.even.util.MyUtil;

@SuppressWarnings("serial")
public class Myframe extends JFrame {
    private class ButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if(command.equals("Line")){
                shape = new Line();
            }
            else if(command.equalsIgnoreCase("Oval")) {
                shape = new Oval();
            }
            else{
                shape = new Rectangle();
            }

            shape.setStarx(MyUtil.random(1, 600));
            shape.setStary(MyUtil.random(1, 600));
            shape.setEndx(MyUtil.random(1, 600));
            shape.setEndy(MyUtil.random(1, 600));
            shape.setColor(MyUtil.randomColor());

            repaint();
             if (shape instanceof Line) {
                    str = "直线的长度" + ((Line) shape).getLength();

                } else if (shape instanceof Oval) {
                    str = "  椭圆的面积:" + ((Oval) shape).getArea()

                            + "  椭圆的周长为:" + ((Oval) shape).getPerimeter();

                } else {
                    str = "  矩形的面积:" + ((Rectangle) shape).getArea()

                            + "  矩形的周长为:" + ((Rectangle) shape).getPerimeter();
                }
        }

    }
    private JButton lineButton, ovalButton, rectButton;

    private Shape shape = null;
    private String str;

    public Myframe() {
        this.setTitle("绘制窗口"); // 窗口题目
        this.setSize(1000, 800); // 窗口尺寸
        this.setResizable(false); // 设置窗口大小不可改变
        this.setLocationRelativeTo(null); // 设置窗口的相对位置
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 关掉窗口即结束程序

        lineButton = new JButton("Line");
        ovalButton = new JButton("Oval");
        rectButton = new JButton("Rectangle");

        ActionListener handler = new ButtonHandler();


        lineButton.addActionListener(handler);
        ovalButton.addActionListener(handler);
        rectButton.addActionListener(handler);



        this.setLayout(new FlowLayout());
        this.add(lineButton);
        this.add(ovalButton);
        this.add(rectButton);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        if (shape != null) {
            shape.draw(g);

            g.drawString(str, 300, 300);
        }

    }

}

我的工具包

package com.even.util;

/**
 * 我的工具空间
 */
import java.awt.Color;

public final class MyUtil { // 加个final,不让继承,成为终结类(断子绝孙类)

    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.even.test;

import com.even.ui.Myframe;

public class MyFrameTest {

    public static void main(String[] args) {
        new Myframe().setVisible(true);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值