Moving content (graphics) inside a JPanel
Hello everybody,
I am currently working on a project involving drawing and such.
I will quickly describe my goal: I need to draw some lines and other polygons onto a JPanel. The JPanel will be roughly 500x500 and, of course, by default it is fixed there (so whatever is outside that range, is not shown). What I want to do is to catch, using a listener, the mouse movements and move the content of the JPanel accordingly.
The issue is that I did not manage to find the command for that :/.
Another question: if I draw a 'HUGE' amount of polygons and data onto the JPanel, will be all stored at the same time or will get memory only when it is painted and therefore shown? If not, what would you think would be the best way to overcome the extremely high amount of memory used? (I was thinking of just drawing stuff only if they are within a certain range ["buffer area"], but this might cause issues for very long/large. But of course, if Java is smart enough to not render the whole thing if it is outside the bounds, I am good to go ;) )
Thanks in advance!
N.
Re: Moving content (graphics) inside a JPanel
For listening to Mouse actions, see
How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
How to Write a Mouse-Motion Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
As for the 'HUGE' numbers, why not try it and see? HUGE is relative, and oftentimes thinking about performance issues prior to knowing whether there will be a performance issue can be wasted energy thinking about something that doesn't exist.
Re: Moving content (graphics) inside a JPanel
Thanks :)
The main question still remains: how do I move the content?
And thanks for the links for the mouse listeners ^^
Re: Moving content (graphics) inside a JPanel
Quote:
Originally Posted by
Nesh108
The main question still remains: how do I move the content?
Change the x,y coordinates of the drawn object. There are hundreds of ways to implement this, and I'm still unclear if you want to move a single object or all of them at the same time, so I recommend providing more context to your question by posting an SSCCE.
Re: Moving content (graphics) inside a JPanel
Ok, I want to move ALL the objects drawn on the JPanel. Here is some sort of SSCCE, hopefully it is understandable, I am just trying out.
I managed to do something, but it is buggy. I managed to move the items according to my cursor, but I would like to move the JPanel as if it were a map (like google maps).
Code :
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class ShowColors extends JPanel implements MouseMotionListener, MouseListener{
int x = 0;
int y = 0;
int cx;
int cy;
int dx;
int dy;
// constructor sets window's title bar string and dimensions
public ShowColors()
{
this.addMouseMotionListener(this);
addMouseMotionListener(this);
this.addMouseListener(this);
addMouseListener(this);
setSize( 400, 400 );
setVisible( true );
}
// draw rectangles and Strings in different colors
public void paint( Graphics g )
{
super.paint( g );
g.drawLine(x, y, x + 300, y + 300);
}
// execute application
public static void main( String args[] )
{
ShowColors application = new ShowColors();
application.setBorder(BorderFactory.createLineBorder(Color.black));
JFrame x = new JFrame("Map");
x.setSize(500, 500);
x.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
x.add(application);
x.setVisible(true);
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
cx = p.x;
cy = p.y;
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void mouseDragged(MouseEvent ex) {
Point p = ex.getPoint();
dx = p.x;
dy = p.y;
if(cx > dx)
x -= (cx - dx) / 100;
else
x += (cx - dx) / 100;
if(cy > dy)
y -= (cy - dy) / 100;
else
y += (cy - dy) / 100;
cx = x;
cy = y;
repaint();
}
}
I added "/100" in order to make them move 'slower', otherwise I wouldnt be able to see where they were moving :/
For the moment there is only a diagonal line, which should be movable like an image.
Re: Moving content (graphics) inside a JPanel
If you want to move everything, have a look at using AffineTransforms
Transforming Shapes, Text, and Images (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)
Have your MouseListeners record the movement, and use the values from the movement to change the translate values of an AffineTransform which is used to transform all your shapes when you paint (BTW, recommend using paintComponent rather than paint)
Re: Moving content (graphics) inside a JPanel
Thanks I found some interesting stuff!