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 7 of 7

Thread: Moving content (graphics) inside a JPanel

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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 ^^

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Moving content (graphics) inside a JPanel

    Quote Originally Posted by Nesh108 View Post
    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.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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).

    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.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Moving content (graphics) inside a JPanel

    Thanks I found some interesting stuff!

Similar Threads

  1. Display a JTextField inside a JPanel
    By nivangerow in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 8th, 2012, 01:28 PM
  2. How to add scrollbar to a JPanel with Graphics
    By Tanguo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 08:53 PM
  3. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  4. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM