骆老师的时钟

package com.lovoinfo.shape;


import java.awt.Color;
import java.awt.FlowLayout;
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 implements ActionListener {
private List<Shape> shapeList = new ArrayList<Shape>();
private List<Shape> redoList = new ArrayList<Shape>();


private JButton circleButton, rectButton, triButton;
private JButton undoButton, redoButton, clearButton;


private Shape shape = null;
private String shapeType = "Circle";


public DrawingFrame() {
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);


circleButton = new JButton("Circle");
circleButton.addActionListener(this);
rectButton = new JButton("Rectangle");
rectButton.addActionListener(this);
triButton = new JButton("Triangle");
triButton.addActionListener(this);


undoButton = new JButton("撤销");
undoButton.addActionListener(this);
redoButton = new JButton("恢复");
redoButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);


this.setLayout(new FlowLayout());
this.add(circleButton);
this.add(rectButton);
this.add(triButton);
this.add(undoButton);
this.add(redoButton);
this.add(clearButton);


this.addMouseListener(new MouseAdapter() {


@Override
public void mousePressed(MouseEvent e) {
if(shapeType.equals("Circle")) {
int radius = MyUtil.random(10, 280);
shape = new Circle(radius);
}
else if(shapeType.equals("Rectangle")) {
int width = MyUtil.random(10, 550);
int height = MyUtil.random(10, 550);
shape = new Rect(width, height);
}
else if(shapeType.equals("Triangle")) {
int edge = MyUtil.random(50, 300);
shape = new Triangle(edge);
}


if(shape != null) {
shape.setX(e.getX());
shape.setY(e.getY());
Color color = MyUtil.randomColor();
shape.setColor(color);
shapeList.add(shape);


repaint();
}
}
});
}


@Override
public void paint(Graphics g) {
super.paint(g);
for(Shape tempShape : shapeList) {
tempShape.draw(g);
g.drawString(
String.format("周长: %.2f", tempShape.perimeter()), 
tempShape.getX(), tempShape.getY());
g.drawString(
String.format("面积: %.2f", tempShape.area()), 
tempShape.getX(), tempShape.getY() + 15);
}
}


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


@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == circleButton) {
shapeType = "Circle";
}
else if(e.getSource() == rectButton) {
shapeType = "Rectangle";
}
else if(e.getSource() == triButton) {
shapeType = "Triangle";
}
else if(e.getSource() == undoButton) {
if(shapeList.size() > 0) {
Shape temp = shapeList.remove(shapeList.size() - 1);
redoList.add(temp);
repaint();
}
}
else if(e.getSource() == redoButton) {
if(redoList.size() > 0) {
Shape temp = redoList.remove(redoList.size() - 1);
shapeList.add(temp);
repaint();
}
}
else if(e.getSource() == clearButton) {
shapeList.clear();
repaint();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值