Java 涂鸦

涂鸦原理:
鼠标一点击产生一个实心圆,拖拽产生实心圆

定义Shape类

package com.lovoinfo.shape;

import java.awt.Color;
import java.awt.Graphics;
/**
 * 图形(抽象类,不能实例化)
 * @author Administrator
 *
 */
public abstract class Shape {
    protected int startX,startY;//起点横纵坐标
    protected int endX,endY;//终点横纵坐标

    protected Color color;

    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * 绘图(抽象方法留给子类重写)
     * @param g 画笔
     */
    public abstract void draw(Graphics g);

    //public abstract void calculate(Graphics g);

    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;
    }

}

定义Oval类继承Shape

package com.lovoinfo.shape;

import java.awt.Graphics;

public class Oval extends Shape{

    private int x;
    private int y;

    public Oval(int x,int y){
        this.x=x;
        this.y=y;
    }
    @Override
    public void draw(Graphics g) {
        g.fillOval(x-2, y-2, 4, 4);
    }

}

定义窗口类

package com.lovoinfo;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import com.lovoinfo.shape.Oval;
import com.lovoinfo.shape.Shape;

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

    private List shapeList=new ArrayList();
    private Shape shape;
    private boolean isMouseButtonDown;// 判断是否鼠标左键被按下
    public MyFrame(){
        this.setSize(500, 500);
        this.setTitle("我的涂鸦窗口");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //监听鼠标按下
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                isMouseButtonDown = e.getButton() == MouseEvent.BUTTON1;
                if(isMouseButtonDown){
                int x = e.getX();//鼠标按下时记录坐标
                int y = e.getY();
                shape=new Oval(x,y);
                shape.setStartX(x);
                shape.setStartY(y);
                }

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //保存图形
                if(isMouseButtonDown && shape!=null){
                shapeList.add(shape);

            }

            }
        });
        //监听鼠标拖拽
        this.addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {

                int x = e.getX();//鼠标拖拽时记录坐标
                int y = e.getY();
                shape=new Oval(x,y);
                shape.setEndX(x);
                shape.setEndY(y);
                shapeList.add(shape);
                repaint();


            }
        });

    }



    @Override
    public void paint(Graphics g) {
        for(Shape shape:shapeList){
            shape.draw(g);
        }
        if(shape != null){
            shape.draw(g);
        }
    }



}

你可能感兴趣的:(Java,Java编程基础)