Day12_13

一.继承、多态、抽象类与方法和画图的综合运用

package HomeWork12_12;

import javax.swing.JFrame;

public class DrawTest
{

    public static void main(String[] args)
    {
        
            JFrame jFrame=new ShapeFrame();
            jFrame.setVisible(true);
    }

}
package HomeWork12_12;

import java.awt.Color;
import java.awt.Graphics;
/*
 * 画图系统:
 * 图形:
 * 坐标、颜色
 * 周长,面积,画图
 * 支持画圆,矩形,等边三角形
 * 
 */
/**
 * 图形类
 * @author YY
 *
 */
public abstract class Shape 
{
    protected int centerx;
    protected int centery;
    protected Color color;
/**
 * 构造器  
 * @param x 中心x坐标
 * @param y 中心y坐标
 * @param color 要画的颜色
 */
    protected Shape(int x,int y,Color color)
    {
        this.centerx=x;
        this.centery=y;
        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("周长:"+perimeter(), centerx, centery);
        g.drawString("面积:"+area(), centerx, centery+20);
    }
}
package HomeWork12_12;

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

import javax.swing.JFrame;

@SuppressWarnings("serial")
public  class ShapeFrame extends JFrame
{
    private Circle circle=new Circle(300, 150, Color.RED, 100);
    
    private Rectangle rectangle=new Rectangle(400, 350, Color.BLUE,200,300);
    private Triangle triangle=new Triangle(500, 150, Color.GREEN, 200);
    
    public ShapeFrame()
    {
        this.setTitle("绘图");
        this.setSize(800, 600);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        circle.draw(g);
        rectangle.draw(g);
        triangle.draw(g);
    }

}
package HomeWork12_12;

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

public class Circle extends Shape
{
    private int radius;

    public Circle(int x, int y, Color color,int radius)
    {
        super(x, y, 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 HomeWork12_12;

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

public class Rectangle extends Shape
{
    private int height;
    private int width;
    
    protected Rectangle(int x, int y, Color color,int height,int width)
    {
        super(x, y, color);
        this.height=height;
        this.width=width;
    }
    
    @Override
    public double perimeter()
    {
        
        return 2*(height+width);
    }
    @Override
    public double area()
    {
        return height*width;
    }

    @Override
    public void draw(Graphics g)
    {
        super.draw(g);
        g.drawRect(centerx-width/2, centery-height/2, width, height);
        
    }
    
    

    
}
package HomeWork12_12;

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

public class Triangle extends Shape
{

    private int side;
    
    public Triangle(int x, int y, Color color,int side)
    {
        super(x, y, color);
        this.side=side;
    }

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

    @Override
    public double area()
    {
        return side*side*Math.sqrt(3)/4;
    }

    @Override
    public void draw(Graphics g)
    {
        super.draw(g);
        int x1=centerx;
        int y1=(int)(centery-side/2.0/Math.cos(Math.PI/6));
        int x2=centerx-side/2;
        int y2=(int)(centery+Math.tan(Math.PI/6)*side/2);
        int x3=centerx+side/2;
        int y3=y2;
        
        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x2, y2, x3, y3);
        g.drawLine(x3, y3, x1, y1);
        
    }

}

二.国际象棋棋盘

package Day12_13_02;

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

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Chess extends JFrame
{
    public Chess()
    {
        this.setTitle("国际象棋");
        this.setSize(600, 620);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawRect(15, 35, 570, 570);
        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 8; i++)
            {
                g.setColor((i + j) % 2 == 0 ? new Color(210, 220, 162) : new Color(179, 56, 89));
                g.fillRect(20+j*70, 40+i*70, 70, 70);
            }
        }
    }

}
package Day12_13_02;

public class ChessTest
{
    public static void main(String[] args)
    {
        Chess chess=new Chess();
        chess.setVisible(true);
    }
}

你可能感兴趣的:(Day12_13)