Java Paint Eraser

时间:2015-11-19 09:50:39

标签: java swing

我想在程序中添加一个橡皮擦,但我知道如何在mouseDragged部分内输入橡皮擦代码..任何人都可以帮忙吗? thx我还是java程序的新手〜

// dummypost有人可以帮助我理解导致问题的确切原因。由于我无法猜测发生了什么,我想我还没有正确理解桥接方法的概念。如果有人可以解释一下桥接方法,那将会很有帮助.// dummypost

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import java.util.*;

 public class DrawingArea extends JPanel
{
private ArrayList<Drawing> drawing = new ArrayList<Drawing>();
private Point startPoint = null;
private Point endPoint = null;
private static int eraser = 0;rivate ArrayList<Drawing> drawing = new ArrayList<Drawing>();
private Point startPoint = null;
private Point endPoint = null;
private static int eraser = 0;

private Color fgColor = Color.BLACK;
private Color bgColor = Color.WHITE;

public DrawingArea()
{
    MyMouseListener ml = new MyMouseListener();
    addMouseListener(ml);
    addMouseMotionListener(ml);

}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    setBackground(bgColor);

        //Custom code to paint all the graphics from the List
    for (DrawingArea.Drawing drw : drawing) 
    {
        g.setColor(drw.getFGColor());
        Point startPoint = drw.getStartPoint();
        Point endPoint = drw.getEndPoint();
        g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
    }
        //Paint the graphics as the mouse is being dragged
    g.setColor(fgColor);

    if (startPoint != null && endPoint != null && eraser == 0) 
    {
        g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
    }

    if(startPoint != null && endPoint != null && eraser == 1)
    {
        g.clearRect(startPoint.x, startPoint.y,40,40);
    }

}

public void eraser()
{
    eraser = 1;
}


public void clear()
{
    drawing.clear();
    repaint();
}

public void setFGColor(Color fgColor)
{
    this.fgColor = fgColor;
}

public void setBGColor(Color bgColor)
{
    this.bgColor = bgColor;
}

class MyMouseListener extends MouseInputAdapter
{
    public void mousePressed(MouseEvent e)
    {
        startPoint = e.getPoint();
        endPoint = startPoint;
    }

    public void mouseDragged(MouseEvent e)
    {
    // Repaint only the area affected by the mouse dragging
        endPoint = e.getPoint();

        if (startPoint != null && endPoint != null && eraser ==0) 
        {
            Drawing drw = new Drawing(fgColor, startPoint, endPoint);
            drawing.add(drw);   
        }

        if (startPoint != null && endPoint != null && eraser ==1) 
        {

        }
        startPoint = endPoint;
        repaint();      
    }

    public void mouseReleased(MouseEvent e)
    {
        eraser = 0;
    }
}

class Drawing
{
    private Color fgColor;
    private Point startPoint;
    private Point endPoint;

    public Drawing(Color fgColor, Point startPoint, Point endPoint)
    {
        this.fgColor = fgColor;
        this.startPoint = startPoint;
        this.endPoint = endPoint;
    }

    public Color getFGColor()
    {
        return fgColor;
    }

    public void setFGColor(Color fgColor)
    {
        this.fgColor = fgColor;
    }

    public Point getStartPoint()
    {
        return startPoint;
    }

    public Point getEndPoint()
    {
        return endPoint;
    }
}

}

0 个答案:

没有答案