This is a conceptual question... I'm planning to make a simple paint program using swing and I want to plan it in advance before I actually code it.

I want to be able to draw some rectangles using fillRect, etc, and then be able to drag those rectangles around the canvas.

How would I go about doing this? I have been able to draw rectangles, but I can't seem to get how to drag them around with grabby points. Does that make sense?

Similarly, what if I want to select the rectangle and then resize it?

Yes, it makes sense. This code is available on the web, it doesn't make 100% sense to me right now (haven't looked that hard really), but it does what you're looking for. It seems to just use the drawLine method to continuously draw lines where the mouse is positioned and give the appearance of a dragged rectangle. Wouldn't normally give you code but I believe you that this isn't an assignment so...

// Rubber.java    basic rubber band  
//                Draw rectangle. left mouse down=first point      
//                Drag to second point. left mouse up=final point 


import java.awt.*;
import java.awt.event.*;

public class Rubber extends Frame
{
  int winWidth = 500;
  int winHeight = 500;
  boolean tracking = false; // left button down, sense motion 
  int startX = 0;
  int startY = 0;
  int currentX = 0;
  int currentY = 0;
  
  Rubber()
  {
    setTitle("Rubber");
    setSize(winWidth,winHeight);
    setBackground(Color.white);
    setForeground(Color.black);
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });
    setVisible(true);
    this.addMouseListener (new mousePressHandler());
    this.addMouseListener (new mouseReleaseHandler());
    this.addMouseMotionListener (new mouseMotionHandler());
  }
  
  void mouseMotion(int x, int y)
  {
    if(tracking)
    {
      currentX = x;
      currentY = y;
    } 
    requestFocus();
    repaint();
  }

  void startMotion(int x, int y)
  {
    tracking = true;
    startX = x;
    startY = y;
    currentX = x+4;
    currentY = y+4; // nonzero size, may choose to ignore later 
    requestFocus();
    repaint();
  }

  void stopMotion(int x, int y)
  {
      tracking = false; // no more rubber_rect 

      // save final figure data for 'display' to draw 
    currentX = x;
    currentY = y;
    requestFocus();
    repaint();
  }

  class mousePressHandler extends MouseAdapter
  {
    public void mousePressed (MouseEvent e)
    {
      int b, x, y;

      b = e.getButton();
      x = e.getX();
      y = e.getY();
      System.out.println("press x="+x+"   y="+y+"   b="+b); // debug print
      if(b==1) startMotion(x, y);
    }
  }

  class mouseReleaseHandler extends MouseAdapter
  {
    public void mouseReleased (MouseEvent e)
    {
      int b, x, y;

      b = e.getButton();
      x = e.getX();
      y = e.getY();
      System.out.println("release x="+x+"   y="+y+"   b="+b); // debug print
      if(b==1) stopMotion(x, y);
    }
  }

  class mouseMotionHandler extends MouseMotionAdapter
  {
    public void mouseDragged (MouseEvent e)
    {
      int b, x, y;

      b = e.getButton();
      x = e.getX();
      y = e.getY();
      System.out.println("motion x="+x+"   y="+y+"   b="+b); // debug print
      mouseMotion(x, y);
    }
  }

  void rubberRect(Graphics g, int x0, int y0, int x1 , int y1)
  { 
    // can apply to all figures 
    // draw a rubber rectangle, mouse down, tracks mouse
    int x,y,x2,y2,x3,y3; // local coordinates
    x2=x0;
	x3=x1;
    if(x1<x0) {x2=x1; x3=x0;};
    y2=y0;
	y3=y1;
    if(y1<y0) {y2=y1; y3=y0;};
    g.setColor(Color.black);
	for(x=x2; x<x3-3; x=x+8)
	{
	  g.drawLine(x, y0, x+4, y0);
	  g.drawLine(x, y1, x+4, y1);
	}
    for(y=y2; y<y3-3; y=y+8)
	{
	  g.drawLine(x0, y, x0, y+4);
	  g.drawLine(x1, y, x1, y+4);
	}
  }

  public void paint(Graphics g)
  {
    if(tracking) rubberRect(g, startX, startY, currentX, currentY);
    // may draw more
  }

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

Thanks BestJewSinceJC:)
Yes, I'm trying to learn Java because right now I'm just not very confident with it. I have had a few classes but it was nothing complicated. One of my java programmer work friends told me that I should do a paint program to better understand objects better, so I thought it seemed like a good challenge. Right now I have a simple paint program using just really simple geometric shapes, pen colors, etc. Then I thought maybe it could be more like Flash, where you can draw a rectangle and then grab it and drag it around. Something like that.

If i get this presentable with my current code, I'll post it and see what you guys think. I definitely need constructive criticism because I really want to learn Java in case I need it for a career. I graduate soon so I'm trying to buff up my languages, and my school really doesn't bother with language classes. :[

Sadly, that was not really helpful, but thank you for responding. Your code makes a border for a shape that's being drawn but doesn't move the shapes on the canvas. I have temp border boxes in my code already but I appreciate your help.

Since my code is really long and would clutter up the post, I tried to find something close to what I did. I found someone online who had a program similar, sort of, to what I want to do. But they also don't have the ability to move objects around the canvas.

http://web.njit.edu/~vmj3/DrawingPad.html

They posted their code here:
http://web.njit.edu/~vmj3/DrawingPad2.java

It's enough to explain my problem though.

If you click on rectangle in their applet and then draw it, it makes a rectangle. And I have already coded this part, and the canvas... But I don't have the ability to select objects and move them around the canvas.

I am not really sure how to approach this. I need to create my shapes and then save them somehow and track them. I don't really know how to go about doing that. Do you know what I mean?

To drag rectangles around that you have created, lets say when you click and drag, all you would need to do is get the current position of the mouse and keep calling repaint, drawing the rectangle in the new position. So basically, you'd just need to keep calling repaint, assuming that inside the paint method, you called the drawRect method, passing it the current mouse coordinates.

Note: There are some issues with paintComponent, paint, and repaint. . I'm not sure the reasoning behind having all three, and which ones call what, so be aware that they all exist and that if you're going to play around with it, you should learn those issues.

To be able to move, resize, and interact with an object, you'll need to create a class to represent it. It should keep track of things like its current bounds, "grabby points", color, etc. and be able to render itself to a supplied graphics object. Your painting canvas needs to maintain a collection of those objects and mouse listeners will have to check those objects and react accordingly.

There are several classes you can use directly or extend that implement Shape and have some methods that make this easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.