Java - Moving selected object from JFrame












0















I am working on a given project which is similar to Paint program on Windows.
I have created buttons, which helped me to draw circles, rectangles, helped to clean the frame and etc.
But i have faced the main problem. I want to select with mouse some region, which I would be able to move around the frame. Here is the code:



    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)
{
}



}


I want actually put all eventprocess in given snippet in code:



    if (e.getActionCommand().equals("Pick up image")){

}


Can anyone help me pls?










share|improve this question

























  • Did you have a question?

    – mypetlion
    Nov 20 '18 at 17:48











  • I want to implement movement of the object, which i will select with mouse

    – Икмет Пирмамедов
    Nov 20 '18 at 18:00






  • 1





    hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:14











  • You r right, I am new with awt/swing, and thanks for advice)

    – Икмет Пирмамедов
    Nov 20 '18 at 18:37











  • are you using j8 or higher?

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:38
















0















I am working on a given project which is similar to Paint program on Windows.
I have created buttons, which helped me to draw circles, rectangles, helped to clean the frame and etc.
But i have faced the main problem. I want to select with mouse some region, which I would be able to move around the frame. Here is the code:



    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)
{
}



}


I want actually put all eventprocess in given snippet in code:



    if (e.getActionCommand().equals("Pick up image")){

}


Can anyone help me pls?










share|improve this question

























  • Did you have a question?

    – mypetlion
    Nov 20 '18 at 17:48











  • I want to implement movement of the object, which i will select with mouse

    – Икмет Пирмамедов
    Nov 20 '18 at 18:00






  • 1





    hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:14











  • You r right, I am new with awt/swing, and thanks for advice)

    – Икмет Пирмамедов
    Nov 20 '18 at 18:37











  • are you using j8 or higher?

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:38














0












0








0








I am working on a given project which is similar to Paint program on Windows.
I have created buttons, which helped me to draw circles, rectangles, helped to clean the frame and etc.
But i have faced the main problem. I want to select with mouse some region, which I would be able to move around the frame. Here is the code:



    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)
{
}



}


I want actually put all eventprocess in given snippet in code:



    if (e.getActionCommand().equals("Pick up image")){

}


Can anyone help me pls?










share|improve this question
















I am working on a given project which is similar to Paint program on Windows.
I have created buttons, which helped me to draw circles, rectangles, helped to clean the frame and etc.
But i have faced the main problem. I want to select with mouse some region, which I would be able to move around the frame. Here is the code:



    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)
{
}



}


I want actually put all eventprocess in given snippet in code:



    if (e.getActionCommand().equals("Pick up image")){

}


Can anyone help me pls?







java swing jframe awt java-2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:42









Andrew Thompson

153k27163341




153k27163341










asked Nov 20 '18 at 17:42









Икмет ПирмамедовИкмет Пирмамедов

1




1













  • Did you have a question?

    – mypetlion
    Nov 20 '18 at 17:48











  • I want to implement movement of the object, which i will select with mouse

    – Икмет Пирмамедов
    Nov 20 '18 at 18:00






  • 1





    hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:14











  • You r right, I am new with awt/swing, and thanks for advice)

    – Икмет Пирмамедов
    Nov 20 '18 at 18:37











  • are you using j8 or higher?

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:38



















  • Did you have a question?

    – mypetlion
    Nov 20 '18 at 17:48











  • I want to implement movement of the object, which i will select with mouse

    – Икмет Пирмамедов
    Nov 20 '18 at 18:00






  • 1





    hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:14











  • You r right, I am new with awt/swing, and thanks for advice)

    – Икмет Пирмамедов
    Nov 20 '18 at 18:37











  • are you using j8 or higher?

    – Arnault Le Prévost-Corvellec
    Nov 20 '18 at 18:38

















Did you have a question?

– mypetlion
Nov 20 '18 at 17:48





Did you have a question?

– mypetlion
Nov 20 '18 at 17:48













I want to implement movement of the object, which i will select with mouse

– Икмет Пирмамедов
Nov 20 '18 at 18:00





I want to implement movement of the object, which i will select with mouse

– Икмет Пирмамедов
Nov 20 '18 at 18:00




1




1





hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:14





hi, You are assurely new with awt/swing, regarding the structure of your code. First, you will to remove all interfaces implemented and use dedicated classes you code is too long now and will not going shorter regarding what you want to do. so you need a little more organisation basically the behaviour that you search is not available with prebuild feature in swing or awt so you will have to do it your self ...

– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:14













You r right, I am new with awt/swing, and thanks for advice)

– Икмет Пирмамедов
Nov 20 '18 at 18:37





You r right, I am new with awt/swing, and thanks for advice)

– Икмет Пирмамедов
Nov 20 '18 at 18:37













are you using j8 or higher?

– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:38





are you using j8 or higher?

– Arnault Le Prévost-Corvellec
Nov 20 '18 at 18:38












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398625%2fjava-moving-selected-object-from-jframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398625%2fjava-moving-selected-object-from-jframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory