Results 1 to 7 of 7
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default How do you move a picture?

    I cant find any methods to move an Image or BufferedImage position..
    Does anyone know how to do it?
    Cause I got a script that loops every second.
    And it have to readd the picture everytime it loops because the old one dicappears..
    So can I move the picture position without repainting? Or can I recover my picture so I doesnt have to load a new one?
    Cuase right now im getting ram memory full error..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Heres my code:
    PHP Code:
    /**
     * You must use Threading!
     */
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.*; 
    import java.io.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
     
    public class TestingArea {
    	static File file = new File("C:\\Users\\Andreas\\Bilder\\CNDF.png");
        static Vector <Image> list = new Vector<Image>();
        static int X = 0;
        static int Y = 0;
        public static void main(String[] args) {
        	
        	// TODO, add your application code
        	gameFrame gf = new gameFrame();
        	new Thread(gf).start();
        	paintIt obj = new paintIt();
        	gf.add(obj);
        	while (true){
        		X += 10;
        		Y += 2;
        		if (X>400){
        			X = 0;
        			Y = 0;
        		}
    
    
        		Graphics g;
        		gf.repaint();
        		try{
     				 //do what you want to do before sleeping
      				Thread.currentThread().sleep(10);//sleep for 1000 ms
      				//do what you want to do after sleeptig
    			} catch(Exception ie){
    				//If this thread was intrrupted by nother thread 
    			}
        	}
        }
        
    }
    
    class gameFrame extends JFrame implements Runnable{
    	public void run(){
    		paintIt p = new paintIt();
    		add(p);
    	}
    	public gameFrame(){
    		
    		
    		
    		//paintIt p = new paintIt();
    		//add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(600,400);
    		setTitle("Image test");
    		setVisible(true);
    		
    	}
    }
    
    class paintIt extends JPanel implements Runnable{
    	BufferedImage pic;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		if (TestingArea.list.length > 0 ){
    			pic = ImageIO.read(TestingArea.file);
    			TestingArea.list.add(pic);
    		}
    		try{
    		g.drawImage(pic,TestingArea.X,TestingArea.Y,null);
    		}catch (Exception e){
    			System.out.println("DOESNT WORK!");
    			e.printStackTrace();
    		}
    	}
    	public void run(){
    	}
    }
    I,v modd it heavily to get rid of the error but nothing I do seem to fix it.
    This will make the picture move sideways 5 times about then the memory error occurs..
    I'v tried everything and Im so stuck...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Use a Swing Timer, not a while (true) with Thread.sleep
    2) Never ever try to read in a file from within the paint or paintComponent methods.
    3) I'm gathering that you want to do a simple animation where you move a picture, correct? If so, why not just read the picture in once and simply move it from within the Swing timer by changing an imageX and imageY field and using this field within the JPanel's paintComponent with which to place the image?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, the JPanel that displays the image could look something like so:
    Java Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.JPanel;
    
    public class ImagePanel extends JPanel {
      private int imageX, imageY;
      private Image image;
      
      public ImagePanel(Image image) {
        this.image = image;
      }
      
      // change the image's position with this, then
      // call repaint on this JPanel
      public void setXY(int x, int y) {
        imageX = x;
        imageY = y;
      }
      
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
          g.drawImage(image, imageX, imageY, null);
        }
      }
    }
    To move the image, simply call setXY(...) on the JPanel and then repaint on the same JPanel.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You are drawing you image at its real size so there is no need to do custom painting on a JPanel. Just add an Icon to a JLabel and add the JLabel to a panel. Whenever you want to move the label just use label.setLocation(..) when the Swing Timer fires.

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I didnt use ur code cause mine was like the same, But ur real smart!
    I took ur idea and added the addPicture method so I wouldnt have to load the image in the graphics part and now its working perfectly!
    Thanks alot!

Similar Threads

  1. how to import a picture into an applet
    By cecily in forum Java Applets
    Replies: 2
    Last Post: 01-15-2010, 08:51 PM
  2. Picture Matching Game
    By schnappi in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2009, 01:38 PM
  3. picture backround
    By safiya in forum NetBeans
    Replies: 1
    Last Post: 10-18-2008, 07:13 AM
  4. Picture help
    By deathnote11 in forum AWT / Swing
    Replies: 13
    Last Post: 06-03-2008, 05:46 PM
  5. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •