0

I have created a draggable JComponent in Java Swing and want to display it bigger than the JFrame it contains is (it's supposed to be a game map, movable by drag&drop). As i found didn't find a way to display the component alone bigger than the frame, I tried to put it into a JScrollPane and remove the horizontal as well as the vertical scrollbar, but Eclipse says "invalid verticalScrollBarPolicy". Is there a workaround for that or - even better - a way to scale my Draggable JComponent to a size greater than the JFrame without having to add it to extra panels or components?

Thanks in advance!

2 Answers 2

1

I tried to put it into a JScrollPane and remove the horizontal as well as the vertical scrollbar, but Eclipse says "invalid verticalScrollBarPolicy".

Make sure you use the proper variables:

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

a way to scale my Draggable JComponent to a size greater than the JFrame

Implement the getPreferredSize() method to return the appropriate size of your component.

1
  • thanks, I wrote "scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);, shame on me. Thanks for the helpful replies though!
    – calthon
    May 23, 2013 at 16:23
1

You can create your own reusable, draggable component by implementing MouseMotionListener in a JPanel. You will have to keep track of the mouse position, image and panel dimensions, and the current offset of the image you wish to display.

private Image image;
private int imageWidth, imageHeight, width, height;
private int mouseX, mouseY;
private int offsetX, offsetY;

Implement the mouseMoved method to simply track the mouse position.

public void mouseMoved(MouseEvent e)
{
    mouseX = e.getX();
    mouseY = e.getY();
}

In the mouseDragged method, update the offset to reflect the change in position.

public void mouseDragged(MouseEvent e)
{
    int mx = e.getX();
    int my = e.getY();
    offsetX += (mouseX - mx);
    offsetY += (mouseY - my);
    // don't let offset exceed bounds of image
    offsetX = Math.max(offsetX, 0);
    offsetX = Math.min(offsetX, imageWidth - width);
    offsetY = Math.max(offsetY, 0);
    offsetY = Math.min(offsetY, imageHeight - height);
    mouseX = mx;
    mouseY = my;
    repaint();
}

Finally, override the paint method to display the image at the correct offset.

@Override
public void paint(Graphics g)
{
    g.drawImage(image, -offsetX, -offsetY, this);
}

Remember to add itself as a mouseMotionListener in the constructor!

this.addMouseMotionListener(this);
2
  • Also consider Point#translate()
    – trashgod
    May 22, 2013 at 18:22
  • thanks for that, but I already have the draggable component, what I wanted to know is if I can scale the component (extends JComponent) to be larger than the frame it is in
    – calthon
    May 22, 2013 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.