Java 1030 接口

package com.lovo;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MyFrame extends JFrame {
	private JButton but1 = new JButton("圆形");
	private JButton but2 = new JButton("线条");
	private JButton but3 = new JButton("矩形");

	private boolean temp = true;
	private Drawable c = null;

	//译名内部类实现接口
	private class MyListener implements ActionListener {
        
		@Override
		public void actionPerformed(ActionEvent e) {
			Object obj = e.getSource();
			if (obj == but1) {
				c = new Circle();
			} else if (obj == but2) {
				c = new Line();
			} else if (obj == but3) {
				c = new Rectangle();
			}

		}

	}
    //缺省适配器
	private class MouseHandler extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent e) {
			if (c != null) {
				c.setStartX(e.getX());
				c.setStartY(e.getY());
				c.setEndX(e.getX());
				c.setEndY(e.getY());
			}

		}

		@Override
		public void mouseDragged(MouseEvent e) {
			if (c != null) {
				c.setEndX(e.getX());
				c.setEndY(e.getY());
				repaint();
			}
		}

	}

	public MyFrame() {
		this.setSize(800, 800);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.add(but1);
		this.add(but2);
		this.add(but3);

		MyListener a = new MyListener();
		but1.addActionListener(a);
		but2.addActionListener(a);
		but3.addActionListener(a);

		MouseHandler handler = new MouseHandler();
		this.addMouseListener(handler);
		this.addMouseMotionListener(handler);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (temp != true) {
			g.clearRect(0, 0, 800, 800);
			temp = true;
		}
		if (c != null) {
			c.draw(g);
		}
	}

	public static void main(String[] args) {
		new MyFrame().setVisible(true);
	}
}

package com.lovo;
/**
 * 父类,图形抽象类。(给予子类其它图形继承)
 */

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

public abstract class Drawable {
	protected int startX, startY;
	protected int endX, endY;
	protected Color color;

	/**
	 * 构造器
	 */
	public Drawable() {

		this.color = this.radomColor();
	}

	public Color radomColor() {
		int r = (int) (Math.random() * 256);  //随机颜色值
		int g = (int) (Math.random() * 256);
		int b = (int) (Math.random() * 256);
		Color color = new Color(r, g, b);
		return color;
	}

	public int getStartX() {
		return startX;
	}

	public void setStartX(int startX) {
		this.startX = startX;
	}

	public int getStartY() {
		return startY;
	}

	public void setStartY(int startY) {
		this.startY = startY;
	}

	public int getEndX() {
		return endX;
	}

	public void setEndX(int endX) {
		this.endX = endX;
	}

	public int getEndY() {
		return endY;
	}

	public void setEndY(int endY) {
		this.endY = endY;
	}

	public Color getColor() {
		return color;
	}

	public abstract void draw(Graphics g);  //抽象方法
}

package com.lovo;
/**
 * 子类 
 */

import java.awt.Graphics;

public class Rectangle extends Drawable {
    //继承了抽象类就需要重写抽象类里面的抽象方法
	@Override
	public void draw(Graphics g) {
		g.setColor(this.radomColor());
		int x = Math.min(startX, endX);
		int y = Math.min(startY, endY);
		g.drawRect(x, y, Math.abs(endX - startX), Math.abs(endY - startY));
	}
}

package com.lovo;
/**
 * 子类,圆形
 */

import java.awt.Graphics;

public class Circle extends Drawable {

	@Override
	public void draw(Graphics g) {
		g.setColor(this.radomColor()); 
		int x = startX < endX ? startX : endX;
		int y = startY < endY ? startY : endY;
		g.drawOval(x, y, Math.abs(endX - startX), Math.abs(endY - startY));
	}

}

package com.lovo;

import java.awt.Graphics;

public class Line extends Drawable {

	@Override
	public void draw(Graphics g) {
		g.setColor(this.radomColor()); 
		g.drawLine(startX, startY, endX, endY);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值