Results 1 to 2 of 2
  1. #1
    sepide is offline Member
    Join Date
    Nov 2011
    Rep Power

    Question how can create movable and resizable shapes in java

    I have written a program for my java project on. i have attached the code here. i have to use JPanel..
    the program allows users to select some shapes from a box and the important things is that users can move this shapes and also can change the size of them with mouse..both of this action must do with mouse..i just can write a program that when you run it it has a Rectangle that you can change size of it,but i don't know how i can move it just have resizable Property but it must has movable property too.

    and another problem is that i just can write a program that when it runs the shape appear but we must have a program that when runs it just has a form and when you click For example a button the shapes appear,,i use button but when i make an object of class and then add it to form it doesn't work,
    ,it means in this codes that i send them, if you have a form with a button and in actionPerformed of button you add the object of class ResizeRectangle, it doesn't work when you click button in running time.

    Any help will be appreciated.
    thank you...

    this is code of my program:

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class ResizeRectangle extends JPanel {
    private int SIZE = 8;
    private Rectangle2D[] points = { new Rectangle2D.Double(50, 50,SIZE, SIZE), new Rectangle2D.Double(150, 100,SIZE, SIZE) };
    Rectangle2D s = new Rectangle2D.Double();

    ShapeResizeHandler ada = new ShapeResizeHandler();

    public ResizeRectangle() {

    public void paintComponent(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    for (int i = 0; i < points.length; i++) {
    s.setRect(points[0].getCenterX(), points[0].getCenterY(),
    Math.abs(points[1].getCenterY()- points[0].getCenterY()));


    class ShapeResizeHandler extends MouseAdapter {
    Rectangle2D r = new Rectangle2D.Double(0,0,SIZE,SIZE);
    private int pos = -1;
    public void mousePressed(MouseEvent event) {
    Point p = event.getPoint();

    for (int i = 0; i < points.length; i++) {
    if (points[i].contains(p)) {
    pos = i;

    public void mouseReleased(MouseEvent event) {
    pos = -1;

    public void mouseDragged(MouseEvent event) {
    if (pos == -1)

    points[pos].setRect(event.getPoint().x,event.getPoint().y,poi nts[pos].getWidth(),

    public static void main(String[] args) {

    JFrame frame = new JFrame("Resize Rectangle");

    frame.add(new ResizeRectangle());
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: how can create movable and resizable shapes in java

    Don't double post the same question. Your other thread in New to Java has been removed.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Drawing in Java? or Maybe movable buttons?
    By Bagzli in forum Advanced Java
    Replies: 7
    Last Post: 06-29-2011, 11:57 AM
  2. Non resizable java windows turn black
    By captain alge in forum New To Java
    Replies: 8
    Last Post: 03-20-2011, 12:48 PM
  3. java shapes
    By Kyle227 in forum New To Java
    Replies: 7
    Last Post: 05-20-2010, 12:21 AM
  4. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 06:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts