第七天、第八天 深入案例:绘图系统
实例1:绘图系统(继承、多态、抽象类、监听接口、)
package com.lovoinfo.shape;
import java.awt.Color;
import java.awt.Graphics;
/**
* 创建图形类(抽象类, 不能实例化)
*
* @author jackfrued
*
*/
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;
}
/**
/*标记
public abstract void mark(Graphics g);
}
package com.lovoinfo.shape;
import java.awt.Graphics;
import com.lovoinfo.shape.Shape;
public class Line extends Shape {
/**
* Line将Shape的抽象方法实例化
*/
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
/**
* Line将Shape的标记抽象方法实例化
*/
@Override
public void mark(Graphics g) {
g.drawString(
"线条长度:"+ (double)(Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)
* (y1 - y2))), x2, y2);
}
}
package com.lovoinfo.shape;
import com.lovoinfo.shape.Shape;
import java.awt.Graphics;
public class Oval extends Shape {
int width;
/**
* Oval将Shape的抽象方法实例化
*/
@Override
public void draw(Graphics g) {
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);
}
@Override
public void mark(Graphics g) {
g.drawString("半径:" + width / 2 + "周长:" + 2 * Math.PI * width / 2
+ "面积:" + Math.PI * width / 2 * width / 2, x2, y2);
}
}
package com.lovoinfo.shape;
import java.awt.Graphics;
public class Rectangle extends Shape {
int width;
int height;
/**
* Rectangle将Shape的抽象方法实例化
*/
@Override
public void draw(Graphics g) {
width = Math.abs(x2 - x1);
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);
}
public void mark(Graphics g) {
g.drawString("宽:" + width + "高:" + height + "周长:" + 2
* (width + height)+"面积:"+width*height, x2, y2);
}
}
package com.lovoinfo.util;
import java.awt.Color;
/**
* 创建新的工具类MyUtil
* @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);
}
/**
* 随机颜色方法
* @return 生成一个随机颜色
*/
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.ui;
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;
/**
* 创建窗口框体类
* @author Lengendary
* 继承已有的swing的JFrame类
*/
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
/**
* 继承事件监听接口ActionListenner
*/
private class ButtonHandler implements ActionListener {
/**
* 重写actionPerformed
*/
@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.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();
}
}
/**
* 创建按钮
*/
private JButton lineButton, ovalButton, rectButton;
private Shape shape;
public MainFrame() {
this.setTitle("绘图窗口");
this.setSize(1000, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
lineButton = new JButton("Line");
ovalButton = new JButton("Oval");
rectButton = new JButton("Rectangle");
ActionListener handler = new ButtonHandler();
lineButton.addActionListener(handler);
ovalButton.addActionListener(handler);
rectButton.addActionListener(handler);
/**
* 单独添加按钮实现随机画图
*/
// lineButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// line = new Line();
// line.setX1(MyUtil.random(30, 1000));
// line.setY1(MyUtil.random(0, 600));
// line.setX2(MyUtil.random(30, 1000));
// line.setY2(MyUtil.random(0, 600));
// line.setColor(MyUtil.randomColor());
// repaint();
// }
// });
// ovalButton.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
// oval = new Oval();
// oval.setX1(MyUtil.random(30, 1000));
// oval.setY2(MyUtil.random(0, 600));
// oval.setX2(MyUtil.random(30, 1000));
// oval.setY2(MyUtil.random(0, 600));
// oval.setColor(MyUtil.randomColor());
// repaint();
// }
// });
// rectButton.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent e) {
// rec = new Rectangle();
// rec.setX1(MyUtil.random(30, 1000));
// rec.setY1(MyUtil.random(0, 600));
// rec.setX2(MyUtil.random(30, 1000));
// rec.setY2(MyUtil.random(0, 600));
// rec.setColor(MyUtil.randomColor());
// repaint();
// }
// });
/**
* 窗口添加按钮并居中
*/
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);
shape.mark(g);
}
}
}
package com.lovoinfo.test;
import com.lovoinfo.ui.MainFrame;
public class MainFrameTest {
public static void main(String[] args) {
//匿名对象可见 true
new MainFrame().setVisible(true);
}
}
1.作业:使用抽、封、继、态完成一个工资结算的程序
package economy.paysquare;
/**
* 创建员工抽象类
* @author Lengendary
*
*/
public abstract class Employee {
protected String name;//名字
protected double salary;//底薪
protected String position;//职位
public Employee(String name, double minSalary) {
this.name = name;
this.salary = minSalary;
}
/**
* 创建抽象职位方法
* @return
*/
public abstract String getPosition();
public abstract void setPosition(String position);
/**
* 重写引用函数输出
*/
@Override
public String toString() {
return "员工 :" + name + "| 工资=" + salary + ", 职位="
+ position ;
}
public abstract void calculate();
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package economy.paysquare;
/**
* 经理类
* @author Lengendary
*
*/
public class Manager extends Employee {
private final double mana = 8000;
public Manager(String name, double salary) {
super(name, salary);
}
@Override
public void calculate() {
salary = mana;
}
@Override
public void setPosition(String position) {
this.position = position;
}
@Override
public String getPosition() {
return null;
}
}
package economy.paysquare;
public class Programmer extends Employee {
private double workHour;
private final double hourOfMoney = 100;
public Programmer(String name, double minSalary,double workHour) {
super(name, minSalary);
this.workHour = workHour;
}
public double getWorkHour() {
return workHour;
}
public void setWorkHour(double workHour) {
this.workHour = workHour;
}
@Override
public void calculate() {
salary += workHour*hourOfMoney;
}
@Override
public void setPosition(String position) {
this.position = position;
}
@Override
public String getPosition() {
return position;
}
}
package economy.paysquare;
public class Salesman extends Employee {
private double sales;
private final double d = 0.05;
public Salesman(String name, double minSalary,double sales) {
super(name, minSalary);
this.sales = sales;
}
@Override
public void calculate() {
salary +=sales*d;
}
@Override
public void setPosition(String position) {
this.position = position;
}
@Override
public String getPosition() {
return position;
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
}
package economy.paysquare;
import java.util.Scanner;
public class MainPrograme {
public static void main(String[] args) {
final int personNum = 6;
Scanner sc = new Scanner(System.in);
System.out.print("请按员工工号1~" + personNum + "输入名字:");
sc.hasNextLine();
String[] names = new String[personNum];
for (int i = 0; i < names.length; i++) {
names[i] = sc.nextLine();
}
System.out.print("请按员工工号输入岗位:m、经理 p、程序员 s、销售员");
sc.hasNextLine();
Employee[] myCompany = new Employee[personNum];
String[] positions = new String[personNum];
for (int j = 0; j < names.length; j++) {
positions[j] = sc.nextLine();
}
for (int i = 0; i < myCompany.length; i++) {
Employee index = null;
switch (positions[i]) {
case "m":
index = new Manager(names[i], 8000);
index.calculate();
break;
case "p":
index = new Programmer(names[i], 0, 8);
index.calculate();
break;
case "s":
index = new Salesman(names[i], 1200, 10000);
index.calculate();
break;
}
if (index instanceof Programmer) {
System.out.print("请输入"+names[i]+"本月的工作时间:");
((Programmer) index).setWorkHour(sc.nextDouble());
index.calculate();
}
else if(index instanceof Salesman){
System.out.print("请输入"+names[i]+"本月的销售额:");
((Salesman) index).setSales(sc.nextDouble());
index.calculate();
}
System.out.println(index);
}
sc.close();
}
}