Я работаю над заданным проектом, который похож на программу Paint в Windows.Я создал кнопки, которые помогли мне нарисовать круги, прямоугольники, помогли очистить рамку и т. Д. Но я столкнулся с главной проблемой.Я хочу выделить мышью какой-нибудь регион, который я мог бы перемещать по рамке.Вот код:
package StreamProject;
import java.awt.*;
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 java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class Paint extends JPanel implements MouseListener, ActionListener, MouseMotionListener
{
BufferedImage grid;
Graphics2D gc;
private static final long serialVersionUID = 1L;
public static int stroke, eraser = 0;
private int xX1, yY1, xX2, yY2, choice; // choice
private static final Color BACKGROUND_COLOR = Color.WHITE; // set background as white
private int eraserW = 50; // the eraser width is set to 50
private int eraserH = 50; // the eraser height is set to 50
Paint()
{
JFrame frame = new JFrame("PaintProgram");
frame.setSize(1200, 800);
frame.setBackground(BACKGROUND_COLOR);
frame.getContentPane().add(this);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); // this wll create a menu bar
JMenu opt = new JMenu("Options");
menuBar.add(opt);
JMenuItem item = new JMenuItem ("New");
item = new JMenuItem ("Open new window"); opt.add(item);
item.addActionListener(this);
item = new JMenuItem("Save"); opt.add(item);
item.addActionListener(this);
item = new JMenuItem("Quit"); opt.add(item);
item.addActionListener(this);
JButton pickUp = new JButton("Pick up image");
pickUp.addActionListener(this);
JButton b1 = new JButton("Clear Drawing");
b1.addActionListener(this); // button 1 passed to every ActionListener object that registered using addActionListener
JButton color = new JButton("Color");
color.addActionListener(this);
JButton erase = new JButton("Erase"); // this button is set as Eraser
erase.addActionListener(this);
JButton b2 = new JButton("Rect"); // this button is set as Rect
b2.addActionListener(this); // button 2 passed to every ActionListener object that registered using addActionListener
JButton b3 = new JButton("oval"); // this button is set as Oval
b3.addActionListener(this); // button 3 passed to every ActionListener object that registered using addActionListener
JButton b4 = new JButton("Line"); // this button is set as Line
b4.addActionListener(this); // button 4 passed to every ActionListener object that registered using addActionListener
JRadioButton medium = new JRadioButton("Medium Line"); // there are radio button to choose Medium Line
medium.addActionListener(this);
JRadioButton thick = new JRadioButton("Thick Line"); // there are radio button to choose Thick Line
thick.addActionListener(this);
ButtonGroup lineOption = new ButtonGroup();
lineOption.add(medium); // line for medium
lineOption.add(thick); // line for thick
this.add(pickUp);
this.add(b1);
this.add(color);
this.add(erase);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(medium);
this.add(thick);
addMouseListener(this); // receiving an event from the mouse
frame.setVisible(true); // this frame is set visible to user
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // the operation will automatically off when user clicked exit button
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (grid == null)
{
int w = this.getWidth(); // width
int h = this.getHeight(); // height
grid = (BufferedImage) (this.createImage(w, h));
gc = grid.createGraphics();
gc.setColor(Color.BLUE);
}
g2.drawImage(grid, null, 0, 0);
check();
}
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);
if(choice == 1)
{
check();
gc.drawRect(xX1, yY1, w, h);
repaint();
}
else if(choice == 2)
{
check();
gc.drawOval(xX1, yY1, w, h);
repaint();
}
else if(choice == 3)
{
if (stroke == 0)
gc.setStroke(new BasicStroke(3));
if (stroke == 1)
gc.setStroke(new BasicStroke(6));
gc.drawLine(xX1, yY1, xX2, yY2);
repaint();
}
else if(choice == 4)
{
repaint();
Color temp = gc.getColor();
gc.setColor(BACKGROUND_COLOR);
gc.fillRect(0, 0, getWidth(), getHeight());
gc.setColor(temp);
repaint();
}
else
{
if (eraser == 1)
gc.clearRect(xX1, yY1, w, h);
}
}
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 saveMap() {
// String sb = "TEST CONTENT";
// JFileChooser chooser = new JFileChooser();
// chooser.setCurrentDirectory(new File("/home/hikmet/Desktop"));
// int retrival = chooser.showSaveDialog(null);
// if (retrival == JFileChooser.APPROVE_OPTION) {
// try(FileWriter fw = new FileWriter(chooser.getSelectedFile()+".png")) {
// fw.write(sb.toString());
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
public void actionPerformed(ActionEvent e)
{
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("Quit")){
System.exit(0);
}
if (e.getActionCommand().equals("Save")){
// saveMap();
}
if (e.getActionCommand().equals("Open new window")){
new Paint();
}
if (e.getActionCommand().equals("Pick up image")){
}
if (e.getActionCommand().equals("Rect")) { choice = 1; }
if (e.getActionCommand().equals("oval")) { choice = 2; }
if (e.getActionCommand().equals("Line")) { choice = 3; }
if (e.getActionCommand().equals("Medium Line")) { stroke = 0; }
if (e.getActionCommand().equals("Thick Line")) { stroke = 1; }
if (e.getActionCommand().equals("Erase")) { eraser = 1; choice = 5; super.addMouseMotionListener(this); }
if (e.getActionCommand().equals("Clear Drawing")) { choice = 4;draw(); }
}
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) { xX1 = e.getX(); yY1 = e.getY(); }
public void mouseReleased(MouseEvent e) { xX2 = e.getX(); yY2 = e.getY(); draw(); eraser = 0; }
public void mouseDragged(MouseEvent re)
{
Color c = gc.getColor();
gc.setColor(BACKGROUND_COLOR);
gc.drawRect(re.getX(), re.getY(), eraserW, eraserH);
gc.setColor(c);
repaint();
}
public void mouseMoved(MouseEvent arg0)
{
}
}
Я хочу на самом деле поместить весь обработчик событий в данный фрагмент кода:
if (e.getActionCommand().equals("Pick up image")){
}
Кто-нибудь может мне помочь, пожалуйста?