Java画图(直线,矩形,椭圆),并显示其周长面积

Shape类 父类

package com.lovoinfo.shape;

import java.awt.Color;
import java.awt.Graphics;
/**
 * 图形(抽象类,不能实例化)
 * @author Administrator
 *
 */
public abstract class Shape {
    protected int startX,startY;//起点横纵坐标
    protected int endX,endY;//终点横纵坐标

    protected Color color;

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

    /**
     * 绘图(抽象方法留给子类重写)
     * @param g 画笔
     */
    public abstract void draw(Graphics g);

    public abstract void calculate(Graphics g);

    public int getStartX() {
        return startX;
    }
    public void setStartX(int startX) {
        this.startX = startX;
    }
    public int getStartY() {
        return startY;
    }
    public void setStartY(int startY) {
        this.startY = startY;
    }
    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;
    }

}

定义子类
Rectangle
Oval
Line

package com.lovoinfo.shape;

import java.awt.Graphics;

public class Rectangle extends Shape {
    private int width;
    private int height;
    private String s;
    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        width= Math.abs(endX-startX);
        height=Math.abs(endY-startY);

        int x1=startXint y1=startY@Override
    public void calculate(Graphics g) {
        s="此图为矩形"+"矩形周长为:"+2*(width+height)+"面积为:"+width*height;

        g.drawString(s, 100,100 );

    }

}
package com.lovoinfo.shape;

import java.awt.Graphics;

public class Oval extends Shape{
    private static final double PI = 3.14;
    private String s;
    private int width;
    private int height;
    @Override
    public void draw(Graphics g) {

        g.setColor(color);

        width= Math.abs(endX-startX);
        height=Math.abs(endY-startY);

        int x1=startXint y1=startY@Override
    public void calculate(Graphics g) {
        double a=width/2;
        double b=height/2;
        s="此图为椭圆:"+"面积为:"+PI *a*b+"周长为:"+2*PI*b+4*(Math.abs(a-b));
        g.drawString(s, 100, 100);

    }


}
package com.lovoinfo.shape;

import java.awt.Graphics;

public class Line extends Shape {
    private String s;
    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawLine(startX,startY, endX, endY);

    }

    @Override
    public void calculate(Graphics g) {
        s="此图为直线"+"长度为:"+Math.sqrt((startX-endX)*(startX-endX)+
                (startY-endY)*(startY-endY));
        g.drawString(s, 100, 100);

    }

}

工具类
产生直线椭圆矩形坐标随机数

package com.lovoinfo.util;

import java.awt.Color;
/**
 * 工具类:第一:加上final,让这个类不能被继承
 *      第二:构造器私有,让别人只能以对象加点调用方法
 * @author Administrator
 *
 */
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);
    }

    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);
    }

}

UI

package com.lovoinfo.ui;

import java.awt.Color;
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.lovoinfo.shape.Line;
import com.lovoinfo.shape.Oval;
import com.lovoinfo.shape.Rectangle;
import com.lovoinfo.shape.Shape;
import com.lovoinfo.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.equals("Oval")){
                shape=new Oval();
            }else{
                shape=new Rectangle();
            }
            shape.setStartX(MyUtil.random(0, 1000));
            shape.setStartY(MyUtil.random(0, 600));
            shape.setEndX(MyUtil.random(0, 1000));
            shape.setEndY(MyUtil.random(0, 600));
           // shape.setColor(MyUtil.randomColor());
            shape.setColor(Color.black);
            repaint();
        }

    }

    private JButton lineButton;
    private JButton ovalButton;
    private JButton recButton;


//  private Line line = null;
//  private Rectangle rec = null;
//  private Oval oval = null;
    private Shape shape=null;
    public MyFrame() {
        this.setSize(1000, 600);
        this.setTitle("绘图窗口");
        // super.setTitle("我的第一个窗口");
        this.setResizable(false);
        this.setLocationRelativeTo(null);// 窗口居中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 设置默认的关闭操作

        lineButton = new JButton("Line");
        ovalButton = new JButton("Oval");
        recButton = new JButton("Rectangle");
//      recButton.addActionListener(new ActionListener() {
//
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              rec = new Rectangle();
//              rec.setStartX(MyUtil.random(0, 1000));
//              rec.setStartY(MyUtil.random(0, 600));
//              rec.setEndX(MyUtil.random(0, 1000));
//              rec.setEndY(MyUtil.random(0, 600));
//              repaint();
//
//          }
//      });

//      ovalButton.addActionListener(new ActionListener() {
//
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              oval = new Oval();
//              oval.setStartX(MyUtil.random(0, 1000));
//              oval.setStartY(MyUtil.random(0, 600));
//              oval.setEndX(MyUtil.random(0, 1000));
//              oval.setEndY(MyUtil.random(0, 600));
//              repaint();
//
//          }
//      });
//      lineButton.addActionListener(new ActionListener() {
//          /**
//           * 点击按钮后要执行的方法
//           */
//          @Override
//          public void actionPerformed(ActionEvent e) {
//
//              line = new Line();
//              line.setStartX(MyUtil.random(0, 1000));
//              line.setStartY(MyUtil.random(0, 600));
//              line.setEndX(MyUtil.random(0, 1000));
//              line.setEndY(MyUtil.random(0, 600));
//
//              repaint();
//          }
//      });// 需要放一个Action监听器
        ActionListener handler=new ButtonHandler();
        lineButton.addActionListener(handler);
        ovalButton.addActionListener(handler);
        recButton.addActionListener(handler);
        this.setLayout(new FlowLayout());// 设置流式布局管理器
        this.add(lineButton);
        this.add(ovalButton);
        this.add(recButton);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(shape!=null){
        shape.draw(g);
        shape.calculate(g);
        }
//      if (line != null) {
//          line.draw(g);
//      }
//      if (rec != null) {
//          rec.draw(g);
//      }
//      if (oval != null) {
//          oval.draw(g);
//      }

    }
}

测试类

package com.lovoinfo.ui;

import java.awt.Color;
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.lovoinfo.shape.Line;
import com.lovoinfo.shape.Oval;
import com.lovoinfo.shape.Rectangle;
import com.lovoinfo.shape.Shape;
import com.lovoinfo.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.equals("Oval")){
                shape=new Oval();
            }else{
                shape=new Rectangle();
            }
            shape.setStartX(MyUtil.random(0, 1000));
            shape.setStartY(MyUtil.random(0, 600));
            shape.setEndX(MyUtil.random(0, 1000));
            shape.setEndY(MyUtil.random(0, 600));
           // shape.setColor(MyUtil.randomColor());
            shape.setColor(Color.black);
            repaint();
        }

    }

    private JButton lineButton;
    private JButton ovalButton;
    private JButton recButton;


//  private Line line = null;
//  private Rectangle rec = null;
//  private Oval oval = null;
    private Shape shape=null;
    public MyFrame() {
        this.setSize(1000, 600);
        this.setTitle("绘图窗口");
        // super.setTitle("我的第一个窗口");
        this.setResizable(false);
        this.setLocationRelativeTo(null);// 窗口居中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 设置默认的关闭操作

        lineButton = new JButton("Line");
        ovalButton = new JButton("Oval");
        recButton = new JButton("Rectangle");
//      recButton.addActionListener(new ActionListener() {
//
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              rec = new Rectangle();
//              rec.setStartX(MyUtil.random(0, 1000));
//              rec.setStartY(MyUtil.random(0, 600));
//              rec.setEndX(MyUtil.random(0, 1000));
//              rec.setEndY(MyUtil.random(0, 600));
//              repaint();
//
//          }
//      });

//      ovalButton.addActionListener(new ActionListener() {
//
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              oval = new Oval();
//              oval.setStartX(MyUtil.random(0, 1000));
//              oval.setStartY(MyUtil.random(0, 600));
//              oval.setEndX(MyUtil.random(0, 1000));
//              oval.setEndY(MyUtil.random(0, 600));
//              repaint();
//
//          }
//      });
//      lineButton.addActionListener(new ActionListener() {
//          /**
//           * 点击按钮后要执行的方法
//           */
//          @Override
//          public void actionPerformed(ActionEvent e) {
//
//              line = new Line();
//              line.setStartX(MyUtil.random(0, 1000));
//              line.setStartY(MyUtil.random(0, 600));
//              line.setEndX(MyUtil.random(0, 1000));
//              line.setEndY(MyUtil.random(0, 600));
//
//              repaint();
//          }
//      });// 需要放一个Action监听器
        ActionListener handler=new ButtonHandler();
        lineButton.addActionListener(handler);
        ovalButton.addActionListener(handler);
        recButton.addActionListener(handler);
        this.setLayout(new FlowLayout());// 设置流式布局管理器
        this.add(lineButton);
        this.add(ovalButton);
        this.add(recButton);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(shape!=null){
        shape.draw(g);
        shape.calculate(g);
        }
//      if (line != null) {
//          line.draw(g);
//      }
//      if (rec != null) {
//          rec.draw(g);
//      }
//      if (oval != null) {
//          oval.draw(g);
//      }

    }
}

你可能感兴趣的:(Java)