Java第九天
package day 0507;
import java.awt.Color;
import java.awt.Graphics;
/**
* 图形(抽象的不能实例化)
* @author A
*
*/
public abstract class Shape {
protected int x1, y1; //开始坐标
protected int x2, 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;
}
***********************
package day0507;
/*
*
*重写画圆的方法
*/
import java.awt.Graphics;
public class Oval extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
int x = x1 < x2? x1 : x2;
int y = y1 < y2? y1 : y2;
g.setColor(color);
g.drawOval(x, y, width, height);
}
}
*************************
package day0507;
/*
*
*重写画线条的方法
*/
import java.awt.Graphics;
public class Line extends Shape {
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
***********************
package day 0507;
/*
*
*重写画矩型的方法
*/
import java.awt.Graphics;
public class Rectangle extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
int x = x1 < x2? x1 : x2;
int y = y1 < y2? y1 : y2;
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
**********************
package day 0507;
import java.awt.Color;
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);
}
}
}
*********************
package day 0507;
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 javax.swing.JOptionPane;
import mt.shape.Rectangle;
import mt.shape.Line;
import mt.shape.Oval;
import mt.shape.Shape;
import mt.util.MyUtil;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String commond =e.getActionCommand();
if (commond.equals("Line")) {
shape = line;
}
else if (commond.equals("Oval")) {
shape =oval ;
}
else {
shape = rect;
}
shape.setX1(MyUtil.random(0, 800));
shape.setY1(MyUtil.random(0, 600));
shape.setX2(MyUtil.random(0, 800));
shape.setY2(MyUtil.random(0, 600));
shape.setColor(MyUtil.randomColor());
repaint();
}
}
private JButton ovalButton, rectButton, lineButton;
private Shape shape=null;
private Line line = new Line();
private Oval oval= new Oval();
private Rectangle rect = new Rectangle();
public MyFrame() {
this.setTitle("青春之歌"); // 标题
this.setSize(800, 600); // 大小
this.setResizable(false); // 是否能调整大小
this.setLocationRelativeTo(null); //
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
lineButton = new JButton("Line");
ovalButton = new JButton("Oval");
rectButton = new JButton("Rect");
ActionListener handler = new ButtonHandler();
lineButton.addActionListener(handler);
ovalButton.addActionListener(handler);
rectButton.addActionListener(handler);
this.setLayout(new FlowLayout());//设置流水按钮
this.add(lineButton);
this.add(ovalButton);
this.add(rectButton);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (shape != null) {
shape.draw(g);
int a=Math.abs(shape.getX1()-shape.getX2());//宽
int b=Math.abs(shape.getY1()-shape.getY2());//高
if (shape==line) {
int L=(int) Math.sqrt((a+b)*(a+b));
g.drawString("长度"+L, 100, 100);//划字符串
}
if (shape==oval) {
int L =(int) ((a+b)/2*Math.PI);
int S=(int) (Math.PI*a*b);
g.drawString("周长="+L,100,100);
g.drawString("面积="+S,100,120);
}
if (shape==rect) {
int L=(a+b)*2;
int S=a*b;
g.drawString("周长="+L,100,100);
g.drawString("面积="+S,100,120);
}
}}
}
**************************
package day 0507;
import mt.iu.MyFrame;
public class Test {
public static void main(String[] args) {
new MyFrame().setVisible(true); //匿名对象
}
}