帮忙用java做一个画图程序的橡皮擦

发布于 2024-12-07 05:18:59 字数 6834 浏览 1 评论 0原文

为一个班级制作一个非常基本的绘画程序,其中一个要求是我们必须制作一个橡皮擦。不知道如何做到这一点,我们正在使用一个缓冲图像,然后将其粘贴到我们的 jframe 中。关于如何做到这一点的任何想法/任何代码示例?有人告诉我尝试clearRect,但也许我只是用错了它,但我无法让它接受它。任何帮助/代码示例都会很棒,这是我的代码,都是一类。

     import javax.imageio.ImageIO;
     import javax.swing.*;
     import javax.swing.border.Border;
     import javax.swing.border.LineBorder;

     import java.awt.*;
     import java.awt.event.*;
     import java.awt.image.BufferedImage;
     import java.io.File;
     import java.io.IOException;
     import java.util.*;


     public class PaintProgram extends JPanel implements MouseListener,ActionListener
     {
public static int stroke, eraser = 0;
private int xX1, yY1 , xX2, yY2, choice ;

public static void main(String [] args)
{
    new PaintProgram();
}

PaintProgram()
{


    JFrame frame = new JFrame("Paint Program");
    frame.setSize(1200, 800);
    frame.setBackground(Color.WHITE);
    frame.getContentPane().add(this);


    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu help = new JMenu("Help");
    menuBar.add(help);
    JMenuItem about = new JMenuItem("About");
    help.add(about);
    about.addActionListener(this);



    JButton button1 = new JButton("Clear");
    button1.addActionListener(this);
    JButton color = new JButton("Color");
    color.addActionListener(this);
    JButton erase = new JButton("Erase?");
    erase.addActionListener(this);
    JButton button2 = new JButton("Empty Rect");
    button2.addActionListener(this);
    JButton button3 = new JButton("Filled oval");
    button3.addActionListener(this);
    JButton button4 = new JButton("Filled Rect");
    button4.addActionListener(this);
    JButton button5 = new JButton("Empty oval");
    button5.addActionListener(this);
    JButton button6 = new JButton("Line");
    button6.addActionListener(this);
    JRadioButton thin = new JRadioButton("Thin Line");
    thin.addActionListener(this);
    JRadioButton medium = new JRadioButton("Medium Line");
    medium.addActionListener(this);
    JRadioButton thick = new JRadioButton("Thick Line");
    thick.addActionListener(this);


    ButtonGroup lineOption = new ButtonGroup( );
    lineOption.add(thin);
    lineOption.add(medium);
    lineOption.add(thick);

   this.add(button1); 
   this.add(color);
   this.add(erase);
   this.add(button2);
   this.add(button3);
   this.add(button4);
   this.add(button5);
   this.add(button6);
   this.add(thin);
   this.add(medium);
   this.add(thick);
   addMouseListener(this);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



}

public void paintComponent(Graphics g)
{ 
     super.paintComponent(g);  
     Graphics2D g2 = (Graphics2D)g;
     if(grid == null){
        int w = this.getWidth();
        int h = this.getHeight();
        grid = (BufferedImage)(this.createImage(w,h));
        gc = grid.createGraphics();
        gc.setColor(Color.BLUE);
     }

     g2.drawImage(grid, null, 0, 0);
     check();
}
BufferedImage grid;
Graphics2D gc;


public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
     int w = xX2 - xX1;
        if (w<0)
        w = w *(-1);

   int h = yY2-yY1;
        if (h<0)
        h=  h*(-1);

     switch(choice)
    {
        case 1:
            check();
            gc.drawRect(xX1, yY1, w, h);
            repaint();
            break;

        case 2:
            check();
            gc.drawOval(xX1, yY1, w, h);
            repaint();
            break;

        case 3:
            check();
            gc.drawRect(xX1, yY1, w, h);
            gc.fillRect(xX1, yY1, w, h);
            repaint();
            break;

        case 4:
            check();
            gc.drawOval(xX1, yY1, w, h);
            gc.fillOval(xX1, yY1, w, h);
            repaint();
            break;  

        case 5:

            if (stroke == 0)
            gc.setStroke(new BasicStroke (1));
            if (stroke == 1)
            gc.setStroke(new BasicStroke (3));
            if (stroke == 2)
            gc.setStroke(new BasicStroke (6));
            gc.drawLine(xX1, yY1, xX2, yY2);
            repaint();
            break;

        case 6:
            repaint();
            Color temp = gc.getColor();
            gc.setColor(Color.WHITE);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;   

        case 7:

            if (eraser == 1)
            {
                gc.clearRect(xX1, yY1, w, h);
            }
            else
            {

            }
            break;
    }
}

public void check()
{
    if (xX1 > xX2)
    {
        int z = 0;
        z = xX1;
        xX1 = xX2;
        xX2 =z;
    }
    if (yY1 > yY2)
    {
        int z = 0;
        z = yY1;
        yY1 = yY2;
        yY2 = z;
    }
}




public void actionPerformed(ActionEvent e)
{

if (e.getActionCommand().equals("Color"))
{
     Color bgColor
     = JColorChooser.showDialog(this,"Choose Background Color", getBackground());
   if (bgColor != null)
     gc.setColor(bgColor);
}


if (e.getActionCommand().equals("About"))
{
    System.out.println("About Has Been Pressed");
    JFrame about = new JFrame("About");
    about.setSize(300, 300);
    JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png"));
    about.add(picture);
    about.setVisible(true);
}

if (e.getActionCommand().equals("Empty Rect")) 
{         
  System.out.println("Empty Rectangle Has Been Selected~");
   choice = 1;

  }

if (e.getActionCommand().equals("Empty oval")) 
{         
 System.out.println("Empty Oval Has Been Selected!");
   choice = 2;
  }


if (e.getActionCommand().equals("Filled Rect"))
{         
  System.out.println("Filled Rectangle Has Been Selected");
   choice = 3;
  }


if (e.getActionCommand().equals("Filled oval")) 
{         
 System.out.println("Filled Oval Has Been Selected");
   choice = 4;
  }


if (e.getActionCommand().equals("Line"))
{
    System.out.println("Draw Line Has Been Selected");
    choice = 5;
}

if (e.getActionCommand().equals("Thin Line"))
{
    stroke = 0;
}

if (e.getActionCommand().equals("Medium Line"))
{
    stroke = 1;
}

if (e.getActionCommand().equals("Thick Line"))
{
    stroke = 2;
}

if(e.getActionCommand().equals("Erase?"))
{
    eraser = 1;
    choice = 7;
}

if (e.getActionCommand().equals("Clear"))
{         
    System.out.println("Clear All The Things!!!");
    choice = 6;
    draw();
}


 }

 public void mouseExited(MouseEvent evt){}
 public void mouseEntered(MouseEvent evt){}
 public void mouseClicked(MouseEvent evt){}
 public void mousePressed(MouseEvent evt)
 {

     xX1 = evt.getX();
     yY1= evt.getY();



   }
 public void mouseReleased(MouseEvent evt)
 {
     xX2 =evt.getX();
     yY2=evt.getY();
     draw();
     eraser = 0;
   }

}

Making a very basic paint program for a class and one of the requirements is that we must make an eraser. Not sure how to do that, We are using a bufferedimage that is then getting pasted to our jframe. Any ideas on how this could be done/ any code examples? Someone told me to try clearRect but maybe I was just using it wrong but i could not get it to accept it. Any help / code examples would be great, here is my code, its all one class.

     import javax.imageio.ImageIO;
     import javax.swing.*;
     import javax.swing.border.Border;
     import javax.swing.border.LineBorder;

     import java.awt.*;
     import java.awt.event.*;
     import java.awt.image.BufferedImage;
     import java.io.File;
     import java.io.IOException;
     import java.util.*;


     public class PaintProgram extends JPanel implements MouseListener,ActionListener
     {
public static int stroke, eraser = 0;
private int xX1, yY1 , xX2, yY2, choice ;

public static void main(String [] args)
{
    new PaintProgram();
}

PaintProgram()
{


    JFrame frame = new JFrame("Paint Program");
    frame.setSize(1200, 800);
    frame.setBackground(Color.WHITE);
    frame.getContentPane().add(this);


    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu help = new JMenu("Help");
    menuBar.add(help);
    JMenuItem about = new JMenuItem("About");
    help.add(about);
    about.addActionListener(this);



    JButton button1 = new JButton("Clear");
    button1.addActionListener(this);
    JButton color = new JButton("Color");
    color.addActionListener(this);
    JButton erase = new JButton("Erase?");
    erase.addActionListener(this);
    JButton button2 = new JButton("Empty Rect");
    button2.addActionListener(this);
    JButton button3 = new JButton("Filled oval");
    button3.addActionListener(this);
    JButton button4 = new JButton("Filled Rect");
    button4.addActionListener(this);
    JButton button5 = new JButton("Empty oval");
    button5.addActionListener(this);
    JButton button6 = new JButton("Line");
    button6.addActionListener(this);
    JRadioButton thin = new JRadioButton("Thin Line");
    thin.addActionListener(this);
    JRadioButton medium = new JRadioButton("Medium Line");
    medium.addActionListener(this);
    JRadioButton thick = new JRadioButton("Thick Line");
    thick.addActionListener(this);


    ButtonGroup lineOption = new ButtonGroup( );
    lineOption.add(thin);
    lineOption.add(medium);
    lineOption.add(thick);

   this.add(button1); 
   this.add(color);
   this.add(erase);
   this.add(button2);
   this.add(button3);
   this.add(button4);
   this.add(button5);
   this.add(button6);
   this.add(thin);
   this.add(medium);
   this.add(thick);
   addMouseListener(this);
   frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



}

public void paintComponent(Graphics g)
{ 
     super.paintComponent(g);  
     Graphics2D g2 = (Graphics2D)g;
     if(grid == null){
        int w = this.getWidth();
        int h = this.getHeight();
        grid = (BufferedImage)(this.createImage(w,h));
        gc = grid.createGraphics();
        gc.setColor(Color.BLUE);
     }

     g2.drawImage(grid, null, 0, 0);
     check();
}
BufferedImage grid;
Graphics2D gc;


public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
     int w = xX2 - xX1;
        if (w<0)
        w = w *(-1);

   int h = yY2-yY1;
        if (h<0)
        h=  h*(-1);

     switch(choice)
    {
        case 1:
            check();
            gc.drawRect(xX1, yY1, w, h);
            repaint();
            break;

        case 2:
            check();
            gc.drawOval(xX1, yY1, w, h);
            repaint();
            break;

        case 3:
            check();
            gc.drawRect(xX1, yY1, w, h);
            gc.fillRect(xX1, yY1, w, h);
            repaint();
            break;

        case 4:
            check();
            gc.drawOval(xX1, yY1, w, h);
            gc.fillOval(xX1, yY1, w, h);
            repaint();
            break;  

        case 5:

            if (stroke == 0)
            gc.setStroke(new BasicStroke (1));
            if (stroke == 1)
            gc.setStroke(new BasicStroke (3));
            if (stroke == 2)
            gc.setStroke(new BasicStroke (6));
            gc.drawLine(xX1, yY1, xX2, yY2);
            repaint();
            break;

        case 6:
            repaint();
            Color temp = gc.getColor();
            gc.setColor(Color.WHITE);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;   

        case 7:

            if (eraser == 1)
            {
                gc.clearRect(xX1, yY1, w, h);
            }
            else
            {

            }
            break;
    }
}

public void check()
{
    if (xX1 > xX2)
    {
        int z = 0;
        z = xX1;
        xX1 = xX2;
        xX2 =z;
    }
    if (yY1 > yY2)
    {
        int z = 0;
        z = yY1;
        yY1 = yY2;
        yY2 = z;
    }
}




public void actionPerformed(ActionEvent e)
{

if (e.getActionCommand().equals("Color"))
{
     Color bgColor
     = JColorChooser.showDialog(this,"Choose Background Color", getBackground());
   if (bgColor != null)
     gc.setColor(bgColor);
}


if (e.getActionCommand().equals("About"))
{
    System.out.println("About Has Been Pressed");
    JFrame about = new JFrame("About");
    about.setSize(300, 300);
    JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png"));
    about.add(picture);
    about.setVisible(true);
}

if (e.getActionCommand().equals("Empty Rect")) 
{         
  System.out.println("Empty Rectangle Has Been Selected~");
   choice = 1;

  }

if (e.getActionCommand().equals("Empty oval")) 
{         
 System.out.println("Empty Oval Has Been Selected!");
   choice = 2;
  }


if (e.getActionCommand().equals("Filled Rect"))
{         
  System.out.println("Filled Rectangle Has Been Selected");
   choice = 3;
  }


if (e.getActionCommand().equals("Filled oval")) 
{         
 System.out.println("Filled Oval Has Been Selected");
   choice = 4;
  }


if (e.getActionCommand().equals("Line"))
{
    System.out.println("Draw Line Has Been Selected");
    choice = 5;
}

if (e.getActionCommand().equals("Thin Line"))
{
    stroke = 0;
}

if (e.getActionCommand().equals("Medium Line"))
{
    stroke = 1;
}

if (e.getActionCommand().equals("Thick Line"))
{
    stroke = 2;
}

if(e.getActionCommand().equals("Erase?"))
{
    eraser = 1;
    choice = 7;
}

if (e.getActionCommand().equals("Clear"))
{         
    System.out.println("Clear All The Things!!!");
    choice = 6;
    draw();
}


 }

 public void mouseExited(MouseEvent evt){}
 public void mouseEntered(MouseEvent evt){}
 public void mouseClicked(MouseEvent evt){}
 public void mousePressed(MouseEvent evt)
 {

     xX1 = evt.getX();
     yY1= evt.getY();



   }
 public void mouseReleased(MouseEvent evt)
 {
     xX2 =evt.getX();
     yY2=evt.getY();
     draw();
     eraser = 0;
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-12-14 05:18:59

我在添加/更改代码的地方添加了注释。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class PaintProgram extends JPanel implements MouseListener, ActionListener,
/**
 * CHANGE BY S AQEEL : implement mouse motion listener
 */
MouseMotionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static int stroke, eraser = 0;
    private int xX1, yY1, xX2, yY2, choice;

    /**
     * CHANGE BY S AQEEL : Define a constant for background color, because we are using it at a lot of places
     */
    private static final Color BACKGROUND_COLOR = Color.WHITE;

    /**
     * CHANGE BY S AQEEL : Also define variables for eraser width and height. In a more usable implementation you can allow user to change eraser size
     */
    private int eraserWidth = 40;
    private int eraserHeight = 40;

    public static void main(String[] args)
    {
        new PaintProgram();
    }

    PaintProgram()
    {

        JFrame frame = new JFrame("Paint Program");
        frame.setSize(1200, 800);

        /**
         * CHANGE BY S AQEEL : Use constant instead of hardcoding
         */
        frame.setBackground(BACKGROUND_COLOR);
        frame.getContentPane().add(this);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu help = new JMenu("Help");
        menuBar.add(help);
        JMenuItem about = new JMenuItem("About");
        help.add(about);
        about.addActionListener(this);

        JButton button1 = new JButton("Clear");
        button1.addActionListener(this);
        JButton color = new JButton("Color");
        color.addActionListener(this);
        JButton erase = new JButton("Erase?");
        erase.addActionListener(this);
        JButton button2 = new JButton("Empty Rect");
        button2.addActionListener(this);
        JButton button3 = new JButton("Filled oval");
        button3.addActionListener(this);
        JButton button4 = new JButton("Filled Rect");
        button4.addActionListener(this);
        JButton button5 = new JButton("Empty oval");
        button5.addActionListener(this);
        JButton button6 = new JButton("Line");
        button6.addActionListener(this);
        JRadioButton thin = new JRadioButton("Thin Line");
        thin.addActionListener(this);
        JRadioButton medium = new JRadioButton("Medium Line");
        medium.addActionListener(this);
        JRadioButton thick = new JRadioButton("Thick Line");
        thick.addActionListener(this);

        ButtonGroup lineOption = new ButtonGroup();
        lineOption.add(thin);
        lineOption.add(medium);
        lineOption.add(thick);

        this.add(button1);
        this.add(color);
        this.add(erase);
        this.add(button2);
        this.add(button3);
        this.add(button4);
        this.add(button5);
        this.add(button6);
        this.add(thin);
        this.add(medium);
        this.add(thick);
        addMouseListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        if (grid == null)
        {
            int w = this.getWidth();
            int h = this.getHeight();
            grid = (BufferedImage) (this.createImage(w, h));
            gc = grid.createGraphics();
            gc.setColor(Color.BLUE);
        }

        g2.drawImage(grid, null, 0, 0);
        check();
    }

    BufferedImage grid;
    Graphics2D gc;

    public void draw()
    {
        Graphics2D g = (Graphics2D) getGraphics();
        int w = xX2 - xX1;
        if (w < 0)
            w = w * (-1);

        int h = yY2 - yY1;
        if (h < 0)
            h = h * (-1);

        switch (choice)
        {
        case 1:
            check();
            gc.drawRect(xX1, yY1, w, h);
            repaint();
            break;

        case 2:
            check();
            gc.drawOval(xX1, yY1, w, h);
            repaint();
            break;

        case 3:
            check();
            gc.drawRect(xX1, yY1, w, h);
            gc.fillRect(xX1, yY1, w, h);
            repaint();
            break;

        case 4:
            check();
            gc.drawOval(xX1, yY1, w, h);
            gc.fillOval(xX1, yY1, w, h);
            repaint();
            break;

        case 5:

            if (stroke == 0)
                gc.setStroke(new BasicStroke(1));
            if (stroke == 1)
                gc.setStroke(new BasicStroke(3));
            if (stroke == 2)
                gc.setStroke(new BasicStroke(6));
            gc.drawLine(xX1, yY1, xX2, yY2);
            repaint();
            break;

        case 6:
            repaint();
            Color temp = gc.getColor();

            /**
             * CHANGE BY S AQEEL : Use constant instead of hardcoding
             */
            gc.setColor(BACKGROUND_COLOR);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;

        case 7:

            if (eraser == 1)
            {
                gc.clearRect(xX1, yY1, w, h);
            }
            else
            {

            }
            break;
        }
    }

    public void check()
    {
        if (xX1 > xX2)
        {
            int z = 0;
            z = xX1;
            xX1 = xX2;
            xX2 = z;
        }
        if (yY1 > yY2)
        {
            int z = 0;
            z = yY1;
            yY1 = yY2;
            yY2 = z;
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        /**
         * CHANGE BY S AQEEL : Remove mousemotionlistener(which is added when eraser is selected) So that if another control is pressed, the user does
         * not accidentally erases
         */
        super.removeMouseMotionListener(this);

        if (e.getActionCommand().equals("Color"))
        {
            Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", getBackground());
            if (bgColor != null)
                gc.setColor(bgColor);
        }

        if (e.getActionCommand().equals("About"))
        {
            System.out.println("About Has Been Pressed");
            JFrame about = new JFrame("About");
            about.setSize(300, 300);
            JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png"));
            about.add(picture);
            about.setVisible(true);
        }

        if (e.getActionCommand().equals("Empty Rect"))
        {
            System.out.println("Empty Rectangle Has Been Selected~");
            choice = 1;

        }

        if (e.getActionCommand().equals("Empty oval"))
        {
            System.out.println("Empty Oval Has Been Selected!");
            choice = 2;
        }

        if (e.getActionCommand().equals("Filled Rect"))
        {
            System.out.println("Filled Rectangle Has Been Selected");
            choice = 3;
        }

        if (e.getActionCommand().equals("Filled oval"))
        {
            System.out.println("Filled Oval Has Been Selected");
            choice = 4;
        }

        if (e.getActionCommand().equals("Line"))
        {
            System.out.println("Draw Line Has Been Selected");
            choice = 5;
        }

        if (e.getActionCommand().equals("Thin Line"))
        {
            stroke = 0;
        }

        if (e.getActionCommand().equals("Medium Line"))
        {
            stroke = 1;
        }

        if (e.getActionCommand().equals("Thick Line"))
        {
            stroke = 2;
        }

        if (e.getActionCommand().equals("Erase?"))
        {
            eraser = 1;
            choice = 7;

            /**
             * CHANGE BY S AQEEL : Add mousemotionlistener here.
             */
            super.addMouseMotionListener(this);
        }

        if (e.getActionCommand().equals("Clear"))
        {
            System.out.println("Clear All The Things!!!");
            choice = 6;
            draw();
        }

    }

    public void mouseExited(MouseEvent evt)
    {
    }

    public void mouseEntered(MouseEvent evt)
    {
    }

    public void mouseClicked(MouseEvent evt)
    {
    }

    public void mousePressed(MouseEvent evt)
    {

        xX1 = evt.getX();
        yY1 = evt.getY();

    }

    public void mouseReleased(MouseEvent evt)
    {
        xX2 = evt.getX();
        yY2 = evt.getY();
        draw();
        eraser = 0;
    }

    /**
     * CHANGE BY S AQEEL : MouseMotionListener functions implemented. Note this listener is only registered when eraser is selected
     */
    public void mouseDragged(MouseEvent me)
    {
        Color c = gc.getColor();
        gc.setColor(BACKGROUND_COLOR);
        gc.drawRect(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.fillRect(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.setColor(c);
        repaint();
    }

    public void mouseMoved(MouseEvent arg0)
    {
    }
}

既然您的问题已经解决,您应该尝试添加一些少量但可用的质量功能,例如:使用不同的光标来绘制/擦除不同的形状。允许用户更改橡皮擦尺寸。另外,在当前的实现中,用户必须拖动橡皮擦才能擦除,您应该添加一个功能,以便用户只需单击(除了拖动之外)即可擦除点。

I have added comments where I added/changed the code.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class PaintProgram extends JPanel implements MouseListener, ActionListener,
/**
 * CHANGE BY S AQEEL : implement mouse motion listener
 */
MouseMotionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static int stroke, eraser = 0;
    private int xX1, yY1, xX2, yY2, choice;

    /**
     * CHANGE BY S AQEEL : Define a constant for background color, because we are using it at a lot of places
     */
    private static final Color BACKGROUND_COLOR = Color.WHITE;

    /**
     * CHANGE BY S AQEEL : Also define variables for eraser width and height. In a more usable implementation you can allow user to change eraser size
     */
    private int eraserWidth = 40;
    private int eraserHeight = 40;

    public static void main(String[] args)
    {
        new PaintProgram();
    }

    PaintProgram()
    {

        JFrame frame = new JFrame("Paint Program");
        frame.setSize(1200, 800);

        /**
         * CHANGE BY S AQEEL : Use constant instead of hardcoding
         */
        frame.setBackground(BACKGROUND_COLOR);
        frame.getContentPane().add(this);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu help = new JMenu("Help");
        menuBar.add(help);
        JMenuItem about = new JMenuItem("About");
        help.add(about);
        about.addActionListener(this);

        JButton button1 = new JButton("Clear");
        button1.addActionListener(this);
        JButton color = new JButton("Color");
        color.addActionListener(this);
        JButton erase = new JButton("Erase?");
        erase.addActionListener(this);
        JButton button2 = new JButton("Empty Rect");
        button2.addActionListener(this);
        JButton button3 = new JButton("Filled oval");
        button3.addActionListener(this);
        JButton button4 = new JButton("Filled Rect");
        button4.addActionListener(this);
        JButton button5 = new JButton("Empty oval");
        button5.addActionListener(this);
        JButton button6 = new JButton("Line");
        button6.addActionListener(this);
        JRadioButton thin = new JRadioButton("Thin Line");
        thin.addActionListener(this);
        JRadioButton medium = new JRadioButton("Medium Line");
        medium.addActionListener(this);
        JRadioButton thick = new JRadioButton("Thick Line");
        thick.addActionListener(this);

        ButtonGroup lineOption = new ButtonGroup();
        lineOption.add(thin);
        lineOption.add(medium);
        lineOption.add(thick);

        this.add(button1);
        this.add(color);
        this.add(erase);
        this.add(button2);
        this.add(button3);
        this.add(button4);
        this.add(button5);
        this.add(button6);
        this.add(thin);
        this.add(medium);
        this.add(thick);
        addMouseListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        if (grid == null)
        {
            int w = this.getWidth();
            int h = this.getHeight();
            grid = (BufferedImage) (this.createImage(w, h));
            gc = grid.createGraphics();
            gc.setColor(Color.BLUE);
        }

        g2.drawImage(grid, null, 0, 0);
        check();
    }

    BufferedImage grid;
    Graphics2D gc;

    public void draw()
    {
        Graphics2D g = (Graphics2D) getGraphics();
        int w = xX2 - xX1;
        if (w < 0)
            w = w * (-1);

        int h = yY2 - yY1;
        if (h < 0)
            h = h * (-1);

        switch (choice)
        {
        case 1:
            check();
            gc.drawRect(xX1, yY1, w, h);
            repaint();
            break;

        case 2:
            check();
            gc.drawOval(xX1, yY1, w, h);
            repaint();
            break;

        case 3:
            check();
            gc.drawRect(xX1, yY1, w, h);
            gc.fillRect(xX1, yY1, w, h);
            repaint();
            break;

        case 4:
            check();
            gc.drawOval(xX1, yY1, w, h);
            gc.fillOval(xX1, yY1, w, h);
            repaint();
            break;

        case 5:

            if (stroke == 0)
                gc.setStroke(new BasicStroke(1));
            if (stroke == 1)
                gc.setStroke(new BasicStroke(3));
            if (stroke == 2)
                gc.setStroke(new BasicStroke(6));
            gc.drawLine(xX1, yY1, xX2, yY2);
            repaint();
            break;

        case 6:
            repaint();
            Color temp = gc.getColor();

            /**
             * CHANGE BY S AQEEL : Use constant instead of hardcoding
             */
            gc.setColor(BACKGROUND_COLOR);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;

        case 7:

            if (eraser == 1)
            {
                gc.clearRect(xX1, yY1, w, h);
            }
            else
            {

            }
            break;
        }
    }

    public void check()
    {
        if (xX1 > xX2)
        {
            int z = 0;
            z = xX1;
            xX1 = xX2;
            xX2 = z;
        }
        if (yY1 > yY2)
        {
            int z = 0;
            z = yY1;
            yY1 = yY2;
            yY2 = z;
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        /**
         * CHANGE BY S AQEEL : Remove mousemotionlistener(which is added when eraser is selected) So that if another control is pressed, the user does
         * not accidentally erases
         */
        super.removeMouseMotionListener(this);

        if (e.getActionCommand().equals("Color"))
        {
            Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", getBackground());
            if (bgColor != null)
                gc.setColor(bgColor);
        }

        if (e.getActionCommand().equals("About"))
        {
            System.out.println("About Has Been Pressed");
            JFrame about = new JFrame("About");
            about.setSize(300, 300);
            JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png"));
            about.add(picture);
            about.setVisible(true);
        }

        if (e.getActionCommand().equals("Empty Rect"))
        {
            System.out.println("Empty Rectangle Has Been Selected~");
            choice = 1;

        }

        if (e.getActionCommand().equals("Empty oval"))
        {
            System.out.println("Empty Oval Has Been Selected!");
            choice = 2;
        }

        if (e.getActionCommand().equals("Filled Rect"))
        {
            System.out.println("Filled Rectangle Has Been Selected");
            choice = 3;
        }

        if (e.getActionCommand().equals("Filled oval"))
        {
            System.out.println("Filled Oval Has Been Selected");
            choice = 4;
        }

        if (e.getActionCommand().equals("Line"))
        {
            System.out.println("Draw Line Has Been Selected");
            choice = 5;
        }

        if (e.getActionCommand().equals("Thin Line"))
        {
            stroke = 0;
        }

        if (e.getActionCommand().equals("Medium Line"))
        {
            stroke = 1;
        }

        if (e.getActionCommand().equals("Thick Line"))
        {
            stroke = 2;
        }

        if (e.getActionCommand().equals("Erase?"))
        {
            eraser = 1;
            choice = 7;

            /**
             * CHANGE BY S AQEEL : Add mousemotionlistener here.
             */
            super.addMouseMotionListener(this);
        }

        if (e.getActionCommand().equals("Clear"))
        {
            System.out.println("Clear All The Things!!!");
            choice = 6;
            draw();
        }

    }

    public void mouseExited(MouseEvent evt)
    {
    }

    public void mouseEntered(MouseEvent evt)
    {
    }

    public void mouseClicked(MouseEvent evt)
    {
    }

    public void mousePressed(MouseEvent evt)
    {

        xX1 = evt.getX();
        yY1 = evt.getY();

    }

    public void mouseReleased(MouseEvent evt)
    {
        xX2 = evt.getX();
        yY2 = evt.getY();
        draw();
        eraser = 0;
    }

    /**
     * CHANGE BY S AQEEL : MouseMotionListener functions implemented. Note this listener is only registered when eraser is selected
     */
    public void mouseDragged(MouseEvent me)
    {
        Color c = gc.getColor();
        gc.setColor(BACKGROUND_COLOR);
        gc.drawRect(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.fillRect(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.setColor(c);
        repaint();
    }

    public void mouseMoved(MouseEvent arg0)
    {
    }
}

Now that your problem is solved, you should try to add little but usable quality features, like: Using different cursors for drawing/erasing different shapes. Allow user to change eraser size. Also in the current implementation, the user have to drag the eraser to erase, you should add a functionality so that user can erase points by just clicking (besides dragging).

浊酒尽余欢 2024-12-14 05:18:59

橡皮擦就像笔一样,只不过它将颜色设置为背景颜色。当您擦除时,您会将笔下的当前颜色替换为“背景”颜色,这使其看起来像背景,从而有效地消除了先前在屏幕上绘制的感知颜色。

如果您的应用程序处理透明度,那么您的橡皮擦可能会将颜色设置为透明;但这是相同的想法,因为“透明度”只是颜色成分之一,如红色、绿色和蓝色。

如果您想要一个按钮来擦除整个绘图,则只需以背景色绘制一个与屏幕一样大的矩形即可。

An eraser is just like the pen, except that it sets the color to the background color. When you erase you replace the current color under the pen with the "background" color, which makes it look like the background, effectively removing the perceived color that was previously drawn on the screen.

If your application handles transparencies, then perhaps your eraser will set the color to transparent; but it's the same idea, as "transparency" is just one of the color components, like red, green, and blue.

In the event that you want a button to erase the entire drawing, then simply draw a rectangle as large as the screen in the background color.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文