Java面向对象深入学习:制作一个画图工具并计算那些图形的面积和周长。
一.将以下的每一个Java程序都放到同一个包下。
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 MainFrame extends JFrame {
private JButton lineButton;
private JButton ovilButton;
private JButton rectangleButton;
private Shape shape = null;
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("ovil")) {
shape = new Oval();
} else {
shape = new Rectangle();
}
shape.setX1(MyUtil.random(0, 1000));
shape.setY1(MyUtil.random(0, 600));
shape.setX2(MyUtil.random(0, 1000));
shape.setY2(MyUtil.random(0, 600));
shape.setColor(MyUtil.randomColor());
repaint();
}
}
public MainFrame() {
this.setTitle("绘图窗口");
this.setSize(1000, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
ovilButton = new JButton("ovil");
lineButton = new JButton("line");
rectangleButton = new JButton("rect");
ActionListener buttt = new ButtonHandler();
ovilButton.addActionListener(buttt);
lineButton.addActionListener(buttt);
rectangleButton.addActionListener(buttt);
this.setLayout(new FlowLayout());
this.add(lineButton);
this.add(ovilButton);
this.add(rectangleButton);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (shape != null) {
shape.draw(g);
if (shape instanceof Line ){
String lineLength = "线条长度 = ";
lineLength += Math.sqrt(Math.pow(shape.getX1() - shape.getX2(), 2)
+ Math.pow(shape.getY1() - shape.getY2(), 2));
g.drawString(lineLength, 30, 40);
}else if (shape instanceof Oval){
String ovalArea = "椭圆的面积";
ovalArea += Math.abs(Math.PI * ((shape.getX1() - shape.getX2()) * (shape.getY1() - shape.getY2())/4));
g.drawString(ovalArea, 30, 40);
String ovalGirth = "椭圆的周长";
ovalGirth += 2 * Math.PI + 4 *Math.abs((shape.getX1() - shape.getX2()) - (shape.getY1() - shape.getY2()));
g.drawString(ovalGirth, 30, 70);
}else{
String rectangleArea = "矩形的面积";
rectangleArea += Math.abs((shape.getX1() - shape.getX2()) * (shape.getY1() - shape.getY2()));
g.drawString(rectangleArea, 30, 40);
String rectangleGirth = "矩形的周长";
rectangleGirth += 2 * Math.abs((shape.getX1() - shape.getX2()) + (shape.getY1() - shape.getY2()));
g.drawString(rectangleGirth, 30, 70);
}
}
}
}
import java.awt.Graphics;
public class Rectangle extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x2 - x1);
int height = Math.abs(y2 - y1);
int x = x1 < x2 ? x1 : x2;
int y = y1 < y2 ? y1 : y2;
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
import java.awt.Graphics;
public class Oval extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x2 - x1);
int height = Math.abs(y2 - y1);
int x = x1 < x2 ? x1 : x2;
int y = y1 < y2 ? y1 : y2;
g.setColor(color);
g.drawOval(x, y, width, height);
}
}
import java.awt.Color;
import java.awt.Graphics;
/**
* 图形(抽象类, 不能实例化)
*
* @author 谷振东
*
*/
public abstract class Shape {
protected int x1; // 起点的横坐标
protected int y1; // 起点的纵坐标
protected int x2; // 终点的横坐标
protected int y2; // 终点的纵坐标
protected Color color; // 颜色
/**
* 绘图(抽象方法留给子类重写)
*
* @param g
* 画笔
*/
public abstract void draw(Graphics g);
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public void setColor(Color color) {
this.color = color;
}
}
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);
}
}
import java.awt.Graphics;
public class Line extends Shape {
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}