Results 1 to 7 of 7
Thread: How do you move a picture?
- 11-03-2009, 05:56 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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..
-
You might want to give more details and code here so we can know exactly what you're doing.
- 11-03-2009, 06:27 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Heres my code:
I,v modd it heavily to get rid of the error but nothing I do seem to fix it.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(){ } }
This will make the picture move sideways 5 times about then the memory error occurs..
I'v tried everything and Im so stuck...
-
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?
-
For instance, the JPanel that displays the image could look something like so:
To move the image, simply call setXY(...) on the JPanel and then repaint on the same JPanel.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); } } }
- 11-03-2009, 07:26 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 11-03-2009, 08:44 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Similar Threads
-
how to import a picture into an applet
By cecily in forum Java AppletsReplies: 2Last Post: 01-15-2010, 08:51 PM -
Picture Matching Game
By schnappi in forum AWT / SwingReplies: 3Last Post: 08-09-2009, 01:38 PM -
picture backround
By safiya in forum NetBeansReplies: 1Last Post: 10-18-2008, 07:13 AM -
Picture help
By deathnote11 in forum AWT / SwingReplies: 13Last Post: 06-03-2008, 05:46 PM -
Print a picture file
By oli001 in forum New To JavaReplies: 0Last Post: 11-26-2007, 01:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks