Javaプロット(直線、長方形、楕円)を描画し、その周長面積を表示します.

27871 ワード

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

    }
}