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
Printable View
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
Only the rectangle or the window?
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);
}
}
}
Wrap your code in code tags.And which rectangle you want to resize?Code :// your code here
Well,
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.Code :createCropper(rectangleWidth, rectangleHeight);
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?
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?Quote:
Its giving strange resizes using that method because I'm trying to add an x coordinate to a width or height etc.
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
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.