Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Resizing on mouse movement

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Resizing on mouse movement

    Hi, my program opens a rectangle on a jframe and I want to make the rectangle resizable by the user. Anyone any idea how to do this? Thanks


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Resizing on mouse movement

    Only the rectangle or the window?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizing on mouse movement

    just the rectangle....heres my code atm, moving the rectangle about works fine but i need to be able to resize it

    public class Cropping extends JPanel
    {
    BufferedImage image;
    Dimension size;
    Rectangle clip;
    boolean showClip;

    public Cropping(BufferedImage image)
    {
    this.image = image;
    size = new Dimension(image.getWidth(), image.getHeight());
    showClip = false;
    }

    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    int x = (getWidth() - size.width)/2;
    int y = (getHeight() - size.height)/2;
    g2.drawImage(image, x, y, this);
    if(showClip)
    {
    if(clip == null)
    createClip();
    g2.setPaint(Color.red);
    g2.draw(clip);
    }
    }

    public void setClip(int x, int y)
    {
    // keep clip within raster
    int x0 = (getWidth() - size.width)/2;
    int y0 = (getHeight() - size.height)/2;
    if(x < x0 || x + clip.width > x0 + size.width ||
    y < y0 || y + clip.height > y0 + size.height)
    return;
    clip.setLocation(x, y);
    repaint();
    }

    public Dimension getPreferredSize()
    {
    return size;
    }

    private void createClip()
    {
    clip = new Rectangle(140, 140);
    clip.x = (getWidth() - clip.width)/2;
    clip.y = (getHeight() - clip.height)/2;
    }

    private void clipImage()
    {
    BufferedImage clipped = null;
    try
    {
    int w = clip.width;
    int h = clip.height;
    int x0 = (getWidth() - size.width)/2;
    int y0 = (getHeight() - size.height)/2;
    int x = clip.x - x0;
    int y = clip.y - y0;
    clipped = image.getSubimage(x, y, w, h);
    }
    catch(RasterFormatException rfe)
    {
    System.out.println("raster format error: " + rfe.getMessage());
    return;
    }
    JLabel label = new JLabel(new ImageIcon(clipped));
    JOptionPane.showMessageDialog(this, label, "clipped image",
    JOptionPane.PLAIN_MESSAGE);
    }

    private JPanel getUIPanel()
    {
    final JCheckBox clipBox = new JCheckBox("show clip", showClip);
    clipBox.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    showClip = clipBox.isSelected();
    repaint();
    }
    });
    JButton clip = new JButton("clip image");
    clip.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    clipImage();
    }
    });
    JPanel panel = new JPanel();
    panel.add(clipBox);
    panel.add(clip);
    return panel;
    }

    public static void main(String[] args) throws IOException
    {
    File file = new File("images/cougar.jpg");
    Cropping test = new Cropping(ImageIO.read(file));
    ClipMover mover = new ClipMover(test);
    test.addMouseListener(mover);
    test.addMouseMotionListener(mover);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(test));
    f.getContentPane().add(test.getUIPanel(), "South");
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }

    class ClipMover extends MouseInputAdapter
    {
    Cropping cropping;
    Point offset;
    boolean dragging;

    public ClipMover(Cropping c)
    {
    cropping = c;
    offset = new Point();
    dragging = false;
    }

    public void mousePressed(MouseEvent e)
    {
    Point p = e.getPoint();
    if(cropping.clip.contains(p))
    {
    offset.x = p.x - cropping.clip.x;
    offset.y = p.y - cropping.clip.y;
    dragging = true;
    }
    }

    public void mouseReleased(MouseEvent e)
    {
    dragging = false;
    }

    public void mouseDragged(MouseEvent e)
    {
    if(dragging)
    {
    int x = e.getX() - offset.x;
    int y = e.getY() - offset.y;
    cropping.setClip(x, y);
    }
    }
    }
    Last edited by java_novice; February 3rd, 2012 at 06:21 AM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Resizing on mouse movement

    Wrap your code in code tags.
     // your code here
    And which rectangle you want to resize?

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizing on mouse movement

    Quote Originally Posted by Mr.777 View Post
    Wrap your code in code tags.
     // your code here
    And which rectangle you want to resize?
    Sorry I edited that comment to just contain the original code, its the 'clip' I am wanting to resize

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Resizing on mouse movement

    Well,
    createCropper(rectangleWidth, rectangleHeight);
    here you are actually creating the cropper, and passing it the width and height. Change the width and height and cropper will get the new size.

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizing on mouse movement

    Its giving strange resizes using that method because I'm trying to add an x coordinate to a width or height etc. Theres bound to be a better way of doing it, as with the code your looking at it has to redraw everytime which can't be good for performance. I have edited the comment above which contains the code to revert back to the working original which allows the rectangle to be moved about but not resized. Any ideas on how to best resize it?

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Resizing on mouse movement

    Its giving strange resizes using that method because I'm trying to add an x coordinate to a width or height etc.
    For me it resized exactly as i wanted it to be. Can you kindly explain it a little more that what do you want and what it's doing for you?

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizing on mouse movement

    It can be repositioned ok, the original code i posted which I think is the one your looking at (the one with my changes) can also be repositioned slightly but it is very temperamental. What I wanted was for the user to be able to click on an edge of the red rectangle which appears after clicking the tick box and drag the red rectangle out to resize it

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Resizing on mouse movement

    Oh so now you've put the right question. That's why it's always said, ask proper question to get proper answer.
    Well, you need to implement the action listener to the box and then handle all the size in the event.

Similar Threads

  1. [SOLVED] jsplipane and resizing
    By dabdi in forum Java Theory & Questions
    Replies: 9
    Last Post: June 7th, 2011, 12:10 PM
  2. 3D camera movement
    By macko in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2011, 07:53 AM
  3. JFrame Movement Listening
    By aussiemcgr in forum AWT / Java Swing
    Replies: 3
    Last Post: March 9th, 2011, 11:48 AM
  4. Dynamically resizing an applet
    By mjpam in forum AWT / Java Swing
    Replies: 6
    Last Post: September 19th, 2010, 10:32 PM
  5. movement
    By mlan in forum Java Theory & Questions
    Replies: 4
    Last Post: February 15th, 2010, 10:57 PM