Drag rectangle on JFrame in Java - java

I want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates.
i have the following code.
in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)
but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen
plz tell me how to solve this.

What you need to do, is save the previously drawn rectangle in some sort of data structure, so you can draw it again later.
This code (sorry about the length, will do something similar to what you are describing.
To use it, just slap the JPanel inside of a JFrame.
public class DrawPane extends JPanel {
private List<DrawnShape> drawings;
private DrawnShape curShape;
public DrawPane() {
drawings = new ArrayList<DrawnShape>();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(300, 300));
addMouseListener(clickListener);
addMouseMotionListener(moveListener);
}
#Override
protected void paintComponent(Graphics g2) {
super.paintComponent(g2);
Graphics2D g = (Graphics2D) g2;
for (DrawnShape s : drawings) {
s.draw(g);
}
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(2));
if (curShape == null)
return;
curShape.draw(g);
}
private MouseListener clickListener = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
curShape = new DrawnShape(e.getPoint(), e.getPoint());
}
#Override
public void mouseReleased(MouseEvent e) {
drawings.add(new DrawnShape(curShape.getClickP(), e.getPoint()));
curShape = null;
}
};
private MouseMotionListener moveListener = new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
curShape = new DrawnShape(curShape.getClickP(), e.getPoint());
repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
}
};
}
class DrawnShape {
private Point p1, p2;
public DrawnShape(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public Point getClickP() {
return p1;
}
public void draw(Graphics2D g) {
g.drawLine(p1.x, p1.y, p2.x, p1.y);
g.drawLine(p1.x, p1.y, p1.x, p2.y);
g.drawLine(p2.x, p2.y, p2.x, p1.y);
g.drawLine(p2.x, p2.y, p1.x, p2.y);
}
}

Custom Painting Approaches shows two techniques for doing this.

Related

drag object using mouse Listener

I'm trying to create a program that able the user to drag and drop the oval around in the space. I was able to drag and drop but after I tried do it again on the second run, the oval jump all over the places. I was wondering if anyone know why this happen? Am i missing something? Thank you
public class MoveOval extends JFrame {
private Ellipse2D node = new Ellipse2D.Float(200,200,80,120);
private Point offset;
private int preX,preY;
private Image dbImage;
private Graphics dbg;
Adapter ma = new Adapter();
public static void main(String args[]){
JFrame frame = new MoveOval();
frame.setSize(600,600);
frame.setVisible(true);
}
public MoveOval(){
super("Move Oval");
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(ma);
addMouseMotionListener(ma);
}
private class Adapter extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(node.contains(e.getPoint())){
preX = node.getBounds().x-e.getX();
preY = node.getBounds().y-e.getX();
offset = new Point(preX, preY);
}
}
public void mouseDragged(MouseEvent e){
if(node.contains(e.getPoint())){
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e) {
offset=null;
}
}
public void updateLocation(MouseEvent e){
Point to = e.getPoint();
to.x += offset.x;
to.y += offset.y;
Rectangle bounds = node.getBounds();
bounds.setLocation(to);
node.setFrame(bounds);
repaint();
}
public void paint(Graphics g){
dbImage=createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
Graphics2D gd = (Graphics2D)g.create();
gd.setColor(Color.blue);
gd.fill(node);
}
}
Actually a very simple mistake and easy to fix.
public void mousePressed(MouseEvent e){
if(node.contains(e.getPoint())){
preX = node.getBounds().x-e.getX();
preY = node.getBounds().y-e.getX(); // <- That's the bad guy.
offset = new Point(preX, preY);
}
}
It has to be -e.getY() not -e.getX().

Why does my mouse event coordinates return wrong values?

I've got two classes: Connection extends JComponent and PostMachineView extends JPanel implements MouseListener.
What I'm trying to do is fire a method whenever the mouse button is pressed inside the Connection component.
I'm using SwingUtilities.convertPointToScreen so that the mouse event coordinates will be relative to where I am drawing. The problem is that it returns slightly different numbers for x and y than it should.
Code below:
public class PostMachineView extends JPanel implements MouseListener {
ArrayList<Connection> connections;
public PostMachineView() {
connections = new ArrayList<>();
setSize(new Dimension(800, 800));
setBackground(Color.white);
//here i create the points i'll need for the connection and instantiate it
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 200);
Connection conn = new Connection(p1, p2);
//add the connection component to this panel
add(conn);
//set the connection mouse listener
conn.addMouseListener(this);
connections.add(conn);
}
#Override
public void paint(Graphics g) {
super.paint(g);
//this will be done in a different way, just trying to get it to work first
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 200);
((Connection)getComponent(0)).paintComponent(g, p1, p2);
}
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("clicked in the component");
}
#Override
public void mouseReleased(MouseEvent e) {}
}
public class Connection extends JComponent {
private Rectangle2D.Double curveControl;
private GeneralPath path;
public Connection(Point p1, Point p2) {
path = new GeneralPath();
path.moveTo(p1.getX(), p1.getY());
int[] mid = getMidPointCoords(p1, p2);
curveControl = new Rectangle2D.Double(mid[0] - 5, mid[1] - 5, 10, 10);
path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
curveControl.getCenterX(), curveControl.getCenterY(),
p2.getX(), p2.getY());
}
public int[] getMidPointCoords(Point p1, Point p2) {
int[] v = { 0, 0 };
v[0] = (int) ((p1.getX() + p2.getX()) / 2);
v[1] = (int) ((p1.getY() + p2.getY()) / 2);
return v;
}
public void paintComponent(Graphics g, Point p1, Point p2) {
path.moveTo(p1.getX(), p1.getY());
path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
curveControl.getCenterX(), curveControl.getCenterY(),
p2.getX(), p2.getY());
Graphics2D g2d = (Graphics2D) g;
g2d.draw(path);
g2d.setColor(Color.red);
g2d.fill(curveControl);
g2d.setColor(Color.black);
}
#Override
public boolean contains(int x, int y) {
//here is where i use swingutilities to convert the coordinates
Point p = new Point(x, y);
SwingUtilities.convertPointToScreen(p, this);
return curveControl.contains(p.getX(), p.getY());
}
}
I also have a Teste class, which simply instantiates a JFrame and adds a PostMachineView to it:
public class Teste {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(new Dimension(800,800));
f.add(new PostMachineView());
f.setResizable(false);
f.setVisible(true);
}
}
This...
public void paint(Graphics g) {
super.paint(g);
//this will be done in a different way, just trying to get it to work first
Point p1 = new Point(100, 100);
Point p2 = new Point(100, 200);
((Connection)getComponent(0)).paintComponent(g, p1, p2);
}
Is a VERY bad idea. The Connection component is already a child of the PostMachineView component. You should simply be updating the state of Connection (maybe through a mouseClicked event) and calling repaint to update it.
In Connection, this...
public void paintComponent(Graphics g, Point p1, Point p2) {
path.moveTo(p1.getX(), p1.getY());
path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
curveControl.getCenterX(), curveControl.getCenterY(),
p2.getX(), p2.getY());
Graphics2D g2d = (Graphics2D) g;
g2d.draw(path);
g2d.setColor(Color.red);
g2d.fill(curveControl);
g2d.setColor(Color.black);
}
Should be slit into two methods, one that adds the new point and one that draws it...
public void add(Point p1, Point p2) {
path.moveTo(p1.getX(), p1.getY());
path.curveTo(curveControl.getCenterX(), curveControl.getCenterY(),
curveControl.getCenterX(), curveControl.getCenterY(),
p2.getX(), p2.getY());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.draw(path);
g2d.setColor(Color.red);
g2d.fill(curveControl);
g2d.setColor(Color.black);
g2d.dispose();
}
Because the MouseListener is attached to the Connection component, this...
#Override
public boolean contains(int x, int y) {
//here is where i use swingutilities to convert the coordinates
Point p = new Point(x, y);
SwingUtilities.convertPointToScreen(p, this);
return curveControl.contains(p.getX(), p.getY());
}
No longer makes sense and, to be honest, I'd be VERY careful with messing around with this
At best it should be more like...
public boolean contains(int x, int y) {
return curveControl.contains(x, y);
}
But remember, the MouseListener will no longer generate events unless the x/y coordinates are WITHIN the curveControl, which might not really be what you want.
Oh and this ArrayList<Connection> connections; scares me...but then, I don't have the context to your problem to really know what you're intentions are for this...

How to get rid of circle inside rectangle when switching between objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a combobox in which I can choose to draw either a rectangle, a circle or by freehand.
If I choose to draw a circle it draws it perfectly. If I then switch to draw a rectangle it draws a circle inside the rectangle. The same happens if I first choose to draw a rectangle and then a circle. (See Picture below)
My questions are:
How can I switch between drawing a circle and a rectangle without the circle appearing inside the rectangle?
How can I get the rectangle/circle to show while I'm dragging the mouse. What I mean is how can the lines show until I release the mouse click?
Why doesn't it work drawing by free hand?
This is my testclass:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Lab6 extends JFrame implements ActionListener {
int startX, startY, endX, endY, w, h;
ArrayList<Shape> shapeList = new ArrayList<Shape>();
Container cp = getContentPane();
private JPanel topPanel;
private JComboBox comboBox;
private final String[] boxOptions = new String[] {"Rektangel", "Cirkel", "Frihand"};
public Lab6(String title) {
super(title);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(840, 500);
this.initComponents();
this.setVisible(true);
}
private void initComponents() {
topPanel = new JPanel(new GridLayout(1,2));
topPanel.setPreferredSize(new Dimension(0,40));
comboBox = new JComboBox(boxOptions);
comboBox.setSelectedIndex(0);
comboBox.addActionListener(this);
topPanel.add(comboBox);
this.add(topPanel, BorderLayout.PAGE_START);
}
#Override
public void paint(Graphics g) {
for (Shape s : shapeList) {
s.draw(g);
}
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(comboBox)) {
JComboBox cb = (JComboBox)e.getSource();
if (cb.getSelectedItem().equals("Rektangel")) {
cp.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
#Override
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
int width = startX - endX;
int height = startY - endY;
w = Math.abs(width);
h = Math.abs(height);
Rectangle r = new Rectangle(startX, startY, w, h);
shapeList.add(r);
repaint();
}
});
}
else if (cb.getSelectedItem().equals("Cirkel")) {
cp.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
#Override
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
int width = startX - endX;
int height = startY - endY;
w = Math.abs(width);
h = Math.abs(height);
Circle c = new Circle(startX, startY, w, h);
shapeList.add(c);
repaint();
}
});
}
else if (cb.getSelectedItem().equals("Frihand")) { //I need help with this part
cp.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
#Override
public void mouseDragged(MouseEvent e) {
FreeHand fh = new FreeHand(startX, startY, e.getX(), e.getY());
shapeList.add(fh);
repaint();
}
});
}
}
}
public static void main(String args[]) {
new Lab6("Drawing Program");
}
}
In class Rectangle (class Circle looks the same):
import java.awt.*;
public class Rectangle extends Shape {
public Rectangle(int x, int y, int width, int height) {
super(x, y, width, height);
}
public Rectangle() {
super();
}
#Override
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(4));
g.drawRect(getX(), getY(), getWidth(), getHeight());
}
}
In class FreeHand (I need help with this part):
import java.awt.*;
public class FreeHand extends Shape {
public FreeHand(int x, int y, int width, int height) {
super(x, y, width, height);
}
public FreeHand() {
super();
}
#Override
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(4));
g2.drawLine(getX(), getY(), getWidth(), getHeight());
}
}
In class shape:
import java.awt.Graphics;
import javax.swing.JPanel;
public abstract class Shape extends JPanel {
private int startX, startY, width, height;
public Shape() {
this(0, 0, 1, 1);
}
public Shape(int startX, int startY, int width, int height) {
this.startX = startX;
this.startY = startY;
this.width = width;
this.height = height;
}
public abstract void draw(Graphics g);
#Override
public int getX() {
return startX;
}
#Override
public int getY() {
return startY;
}
#Override
public int getWidth() {
return width;
}
#Override
public int getHeight() {
return height;
}
}
There are a multitude of things going on...
Overriding paint of JFrame
Not calling super.paint before performing custom painting.
Adding a new MosueListener EVERY time you change the shape
Instead, create a custom component, extending from something like JPanel and override it's paintComponent method. Use this component has your basic drawing surface (your controls should contained in another component).
Make sure you call super.paintComponent before performing any custom painting so you don't break the paint chain
See Performing Custom Painting and Painting in AWT and Swing for more details
Create a SINGLE MouseListener and register it to the panel. When the use selects a different shape, change a state variable within the panel (via a setter) which tells the MouseListener what it should do when the user starts drawing.
Updated...
Create a custom class that extends from JPanel...
public static class ShapePane extends JPanel {
}
Override the classes paintComponent method...
public static class ShapePane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Custom Painting here...
}
}
Provide some sizing hints for the layout manager...
public static class ShapePane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Custom Painting here...
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
Provide a means by which the type of shape can be changed...so you know what to paint...
public static class ShapePane extends JPanel {
public enum ShapeType {
CIRCLE,
RECTANGLE
}
private ShapeType currentShapeType;
public void setCurrentShapeType(ShapeType currentShapeType) {
this.currentShapeType = currentShapeType;
}
public ShapeType getCurrentShapeType() {
return currentShapeType;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Custom Painting here...
}
}
Add a SINGLE MouseListener to the custom class to create the required type of shapes...
public static class ShapePane extends JPanel {
public enum ShapeType {
CIRCLE,
RECTANGLE
}
private ShapeType currentShapeType;
public ShapePane() {
addMouseListener(new MouseAdapter() {
private Point clickPoint;
#Override
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
}
#Override
public void mouseReleased(MouseEvent e) {
Point releasePoint = e.getPoint();
int x = Math.min(releasePoint.x, clickPoint.x);
int y = Math.min(releasePoint.y, clickPoint.y);
int width = Math.abs(clickPoint.x - releasePoint.x);
int height = Math.abs(clickPoint.y - releasePoint.y);
switch (getCurrentShapeType()) {
case CIRCLE:
// Make a circle
break;
case RECTANGLE:
// Make a rectangle...
break;
}
repaint();
}
});
}
public void setCurrentShapeType(ShapeType currentShapeType) {
this.currentShapeType = currentShapeType;
}
public ShapeType getCurrentShapeType() {
return currentShapeType;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Custom Painting here...
}
}
Fill in the blanks...
Create another JPanel (you can simply create an instance this time), add your controls to it
Create an instance of a JFrame, add the custom class to it and the controls panel (make sure they are laid out correctly so they don't override each other - see Laying Out Components Within a Container for more details)
Use appropriate listeners to the controls to determine the type of shape the user wants to draw and set the currentShapeType property accordingly...

how to add a mouse listener in JAVA?

private class Board extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
Graphics2D g2d = (Graphics2D) g;
The following code draws the grid:
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(4));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
The following code draws circles and xs by visiting elements in the array List:
for(Shape shape : shapes){
g2d.setPaint(Color.BLUE);
g2d.draw(shape);
}
}
}
public void addMouseListener(MouseListener ml){
// HOW CAN I ADD A MOUSE LISTENER HERE?
}
You can create a class that implements the MouseListener interface as so:
public class CustomMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
statusLabel.setText("Mouse Clicked: ("+e.getX()+", "+e.getY() +")");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
You want define each of those methods based on the action the method represents (which are self explanatory). The MouseEvent object will have all the info you need related to the mouse (ex. x and y position of mouse).
Now you want to add this new MouseListener to a JPanel (which in this case is your Board class):
//JPanel panel = new JPanel();
Board panel = new Board();
panel.addMouseListener(new CustomMouseListener());
Source
Create your class which implements MouseListener, and pass it by addMouseListener method... ?
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
you need to make the class implements MouseListener.
you need to add the unimplements methods.
and here you go :)
you got few new methods:
public void mouseClicked(MouseEvent e) // method calls when mouse clicked
public void mousePressed(MouseEvent e) // method calls when mouse pressed
public void mouseReleased(MouseEvent e) // method calls when mouse relesed
public void mouseEntered(MouseEvent e) // method calls when the mouse curser getting into the component's geometry
public void mouseExited(MouseEvent e) // method calls when mouse curser getting out of the component's geometry
hope it helped you :)

Java Swing - Valid approach for dragging rectangles onto a JPanel?

I have some code to draw rectangles. It's used to draw rectangles on a JPanel, to mark boundaries of widgets. Here the code first, after that I'll explain my problem cq. question.
First off, I have a class (WidgetDrawingPanel) which extends JPanel.
public WidgetDrawingPanel(int width, int height) {
/*To make things visible at least*/
widgets.add(new Widget(10,10,100,100, WidgetType.TextField));
widgets.add(new Widget(50,50,100,200, WidgetType.TextField));
this.width = width;
this.height = height;
this.setBackground(Color.BLUE);
addListener(); //adds both MouseMotionListener and MouseListener
}
Below you'll see me reference ch a lot. This is a CoordinateHolder, which holds start and current coordinates of my mouse movement.
private void addListener() {
this.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent arg0) {
ch.currentX = arg0.getX();
ch.currentY = arg0.getY();
System.out.println("dragging " + ch.currentX + ","+ch.currentY);
WidgetDrawingPanel.this.repaint();
}
});
this.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent event) {
ch.endX = event.getX();
ch.endY = event.getY();
try {
checkCoords();
} catch (OutsidePanelException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "drawn Outside Panel");
}
}
#Override
public void mousePressed(MouseEvent event) {
ch = new CoordinateHolder(event.getX(), event.getY());
}
});
}
and, finally, the paintComponent(Grapics) method. There's loop through Widgets, which are actually just already drawn Rects (x, y, w, h attributes), but which a little more information, which is not useful in the drawing part of the application. Everytime you release the mouse, the CoordinateHolder is converted into a Widget, and added to widgets.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Paint");
g.setColor(Color.BLUE);
g.fillRect(0, 0, width, height); //making the whole panel blue
g.setColor(Color.RED);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(3));
for (Widget w : widgets) {
g.drawRect(w.getX(), w.getY(), w.getW(), w.getH());
}
if (ch != null)
g.drawRect(ch.startX, ch.startY, ch.currentX - ch.startX, ch.currentY - ch.startY);
}
This code is working, but I suspect this is highly inefficient and inperformant, as above code continually refreshes the JPanel on mouse drag, which is, say, once every 10ms? I suppose it'll get slow really soon, especially when the user creates a heck of a lot rectangles (which are also continally redrawn, as seen in painComponent(Graphics)).
Question cq. Problem
Is there a better, less resource consuming method, where the user can drag rectangles smoothly?
I read an answer to this Drag rectangle on JFrame in Java, but the author of that answer seems to do it the same as me. But again, that's way inperformant, right? Or should computers be easily able to redraw the component continually, and is this actually a valid approach?
To show lots of non-changing background shapes, draw them to a BufferedImage and then show that BufferedImage in the paintComponent(...) method. So while a shape is being drawn, draw it in paintComponent(...) but once the shape is done being drawn, perhaps on mouseRelease, then draw it in the background BufferedImage.
Note that what will slow your current drawing code the most may be your debugging SOP statements, but I assume that these will be removed from the finished code.
For example:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
#SuppressWarnings("serial")
public class DrawingPanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 400;
private static final Color DRAWING_COLOR = new Color(255, 100, 200);
private static final Color FINAL_DRAWING_COLOR = Color.red;
private BufferedImage backgroundImg;
private Point startPt = null;
private Point endPt = null;
private Point currentPt = null;
public DrawingPanel() {
backgroundImg = new BufferedImage(PREF_W, PREF_H,
BufferedImage.TYPE_INT_ARGB);
Graphics g = backgroundImg.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, PREF_W, PREF_H);
g.dispose();
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseMotionListener(myMouseAdapter);
addMouseListener(myMouseAdapter);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, this);
}
if (startPt != null && currentPt != null) {
g.setColor(DRAWING_COLOR);
int x = Math.min(startPt.x, currentPt.x);
int y = Math.min(startPt.y, currentPt.y);
int width = Math.abs(startPt.x - currentPt.x);
int height = Math.abs(startPt.y - currentPt.y);
g.drawRect(x, y, width, height);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public void drawToBackground() {
Graphics g = backgroundImg.getGraphics();
g.setColor(FINAL_DRAWING_COLOR);
int x = Math.min(startPt.x, endPt.x);
int y = Math.min(startPt.y, endPt.y);
int width = Math.abs(startPt.x - endPt.x);
int height = Math.abs(startPt.y - endPt.y);
g.drawRect(x, y, width, height);
g.dispose();
startPt = null;
repaint();
}
private class MyMouseAdapter extends MouseAdapter {
#Override
public void mouseDragged(MouseEvent mEvt) {
currentPt = mEvt.getPoint();
DrawingPanel.this.repaint();
}
#Override
public void mouseReleased(MouseEvent mEvt) {
endPt = mEvt.getPoint();
currentPt = null;
drawToBackground();
}
#Override
public void mousePressed(MouseEvent mEvt) {
startPt = mEvt.getPoint();
}
}
private static void createAndShowGui() {
DrawingPanel mainPanel = new DrawingPanel();
JFrame frame = new JFrame("Drawing Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Categories