Java学习第13天

2016-12-15  本文已影响0人  _Raye

多态练习:

package org.mobiletrain;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 图形(抽象类)
 * @author apple
 *
 */
public abstract class Shape{

    protected Color color;
    protected int centerX;
    protected int centerY;
    
    /**
     * 构造器
     * @param centerX 中心点的横坐标
     * @param centerY 中心点的纵坐标
     * @param color   画笔颜色
     */
    public Shape(int centerX,int centerY,Color color) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.color = color;
    }

    /**
     * 计算周长
     * @return 图形的周长
     */
    public abstract double perimeter();
    
    /**
     * 计算面积
     * @return 图形的面积
     */
    public abstract double area();
    
    /**
     * 绘图
     * @param g 画笔(绘图的上下文环境)
     */
    public  void draw(Graphics g){
        g.setColor(color);
        g.drawString(String.format("周长:%.2f", perimeter()), centerX, centerY);
        g.drawString(String.format("面积:%.2f", area()), centerX, centerY + 20);
    }
    
    
}
package org.mobiletrain;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 矩形
 * @author apple
 *
 */
public class Rectangle extends Shape{

    protected int height;
    protected int width;
    
    public Rectangle(int centerX,int centerY,Color color,int width,int height) {
        super(centerX, centerY, color);
        this.width = width;
        this.height = height;
    }

    @Override
    public double perimeter() {
        
        return (height + width) * 2;
    }

    @Override
    public double area() {
        
        return height * width;
    }


    @Override
    public void draw(Graphics g) {
        super.draw(g);
    g.drawRect(centerX  - width / 2 - 40, centerY - height / 2 - 40, width, height);        
    }
}
package org.mobiletrain;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 圆
 */
public class Circle extends Shape{

    private int radius;
    
    public Circle(int centerX,int centerY,Color color,int radius) {
        super(centerX, centerY, color);
        this.radius = radius;
    }

    @Override
    public double perimeter() {

        return 2 * Math.PI * radius;
    }

    @Override
    public double area() {

        return Math.PI * radius * radius;
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        g.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
    
    }   
}
package org.mobiletrain;

import java.awt.Color;
import java.awt.Graphics;

public class Triangle extends Shape{

    private int length;
    
    public Triangle(int centerX,int centerY,Color color,int length) {
        super(centerX, centerY, color);
        this.length = length;
    }

    @Override
    public double perimeter() {
        
        return length * 3;
    }

    @Override
    public double area() {
        
        return Math.sqrt(perimeter() / 2 * (perimeter() - length) *
                             (perimeter() - length) * (perimeter() - length));
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        int x1 = centerX;
        int y1 = (int) (centerY - length / 2 / Math.cos(Math.PI / 6));
        int x2 = centerX - length / 2;
        int y2 = (int) (centerY + Math.tan(Math.PI / 6) * length / 2);
        int x3 = centerX + length / 2;
        int y3 = y2;
        g.setColor(color);
        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x2, y2, x3, y3);
        g.drawLine(x3, y3, x1, y1);
        
    }

}
package org.mobiletrain.ui;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import org.mobiletrain.Circle;
import org.mobiletrain.Rectangle;
import org.mobiletrain.Triangle;;

@SuppressWarnings("serial")
public class ShapeFrame extends JFrame {

    
    private Circle circle = new Circle(600, 300, Color.green, 100);
    private Rectangle rectangle = new Rectangle(300, 450, Color.red, 300, 200);
    private Triangle triangle = new Triangle(300, 200, Color.blue, 200);
    
    /**
     * 构造器,设置窗口
     */
    public ShapeFrame(){
        this.setTitle("我的绘图应用");
        this.setSize(800, 600);
        //窗口大小是否可以改变
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }

    //系统自动调用的方法,对窗口进行渲染
    @Override
    public void paint(Graphics g) {
        super.paint(g);//相当于先画出了窗口,如果删除,窗口会出现残影
        circle.draw(g);
        rectangle.draw(g);
        triangle.draw(g);
    }
    
    public static void main(String[] args) {
        new ShapeFrame().setVisible(true);
    }
}
package org.mobiletrain;

/**
 * 车类(抽象类)
 * @author apple
 *
 */
public abstract class Vehicle {

    protected String brand;//车的品牌
    
    public Vehicle(String brand){
        this.brand = brand;
    }
    
    /**
     * 获得车的品牌
     * @return 品牌
     */
    public String getBrand() {
        return brand;
    }
    
    //模板方法模式(GoF设计模式 - 23种)
    /**
     * 获得日租金(抽象方法)
     * @return 日租金
     */
    public abstract int rentPerDay();
    
    /**
     * 计算总租金
     * @param days 天数
     * @return 总租金
     */
    public int calcRent(int days){
        return days * rentPerDay();
    }
}
package org.mobiletrain;

/**
 * 汽车类
 * @author apple
 *
 */
public class Car extends Vehicle {

    private boolean nomal;//判断是普通车还是商务车
    
    /**
     * 构造器
     * @param brand 品牌
     * @param normal 判断是普通车还是商务车
     */
    public Car(String brand,boolean normal) {
        super(brand);   
        this.nomal = normal;
    }

    @Override
    public int rentPerDay() {
        return nomal ? 400 : 600;
    }
}
package org.mobiletrain;

/**
 * 卡车类
 * @author apple
 *
 */
public class Truck extends Vehicle {

    private double load;//载重
    
    /**
     * 构造器
     * @param brand 品牌
     * @param load 载重
     */
    public Truck(String brand,double load) {
        super(brand);
        this.load = load;
    }

    @Override
    public int rentPerDay() {
        if (load < 10) {
            return 1200;
        }
        else if (load < 20) {
            return 2000;
        }
        else {
            return 2500;
        }
    }

}

package org.mobiletrain.ui;

import org.mobiletrain.Car;
import org.mobiletrain.Truck;
import org.mobiletrain.Vehicle;

public class CarRentTest {

    public static void main(String[] args) {
        Vehicle v1 = new Car("奔驰", false);
        System.out.println(v1.calcRent(5));
        
        Vehicle v2 = new Truck("东风", 8);
        System.out.println(v2.calcRent(1));
        
    }
}
package org.mobiletrain.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Chess extends JFrame{

    private Image knightImage;//骑士
    private Image rookImage;//城堡

    public Chess() {
        this.setTitle("国际象棋");
        this.setSize(800, 800);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        try {
            knightImage = ImageIO.read(this.getClass().getClassLoader().getResource("22.png"));
            rookImage = ImageIO.read(this.getClass().getClassLoader().getResource("21.png"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paint(Graphics g) {     
        super.paint(g);
        g.setColor(Color.gray);
        g.fillRect(0, 0, 800, 800);
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                g.setColor((i + j) % 2 == 0 ? new Color(210, 220, 162) : new Color(179, 56, 89));
                g.fillRect(40 + j * 90, 60 + i * 90, 90, 90);
            } 
        }
        g.drawImage(knightImage, 130, 60, 90, 90,null);
        g.drawImage(rookImage, 40, 60, 90, 90,null);
    }

}
package org.mobiletrain;

import org.mobiletrain.ui.Chess;

public class ChessTest{

    public static void main(String[] args) {
        new Chess().setVisible(true);
    }
}
上一篇下一篇

猜你喜欢

热点阅读