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