5 Replies - 4239 Views - Last Post: 05 May 2013 - 11:48 AM Rate Topic: -----

#1 metesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 13

move an image in jframe

Posted 04 May 2013 - 07:16 AM

i try to move an image in jframe.....for resource i found this code....i make it move.........but i can't able to create any panel and show another image in the frame.....i want to show another image....any help ....

import java.awt.*;  
   import javax.swing.*;
   import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

//
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class MyImage extends  JFrame {  
     
	
    static int xPixel = 96;  
    static int yPixel = 56;   
      
    Image myImage, offScreenImage;  
    Graphics offScreenGraphics;  
	
    
    public MyImage() {  
	    
	    
        
       try{
          myImage = Toolkit.getDefaultToolkit().getImage("bug2.JPG");   
       } 
         
        catch(Exception e) {}  
        setSize(800,600);  
        setVisible(true);  
moveImage();  
          
    }  
      
    public void update(Graphics g) {  
        paint(g);  
    }  
      
    public void paint(Graphics g) {  
          
        int width  = getWidth();  
        int height = getHeight();  
          
        if (offScreenImage == null) {  
            offScreenImage    = createImage(width, height);  
            offScreenGraphics = offScreenImage.getGraphics();  
        }  
          
        // clear the off screen image  
        offScreenGraphics.clearRect(76, 36, width + 1, height + 1);  
          
        // draw your image off screen  
        offScreenGraphics.drawImage(myImage, xPixel, yPixel, this);  
          
        // show the off screen image  
	 
        g.drawImage(offScreenImage, 96, 56, this);  
	
          
    }  
      
    void moveImage() {  
          
        for ( int i = 0 ; i < 500 ; i++ ){  
              
            System.out.println("next set of Pixels " + xPixel);  
                          
            xPixel +=1;  
		
           for(int k=0;k<5;k++){yPixel +=1;}
	   
           repaint(); 
		if(yPixel>=450)
		{yPixel=56;xPixel=96;}
              
            // then sleep for a bit for your animation  
            try { Thread.sleep(50); }   /* this will pause for 50 milliseconds */  
            catch (InterruptedException e) { System.err.println("sleep exception"); }  
              
        }  
    }  
      
    public static void main(String args[]){  
        
		
        
        MyImage me = new MyImage();  
         
    
    }     
} 





Is This A Good Question/Topic? 0
  • +

Replies To: move an image in jframe

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: move an image in jframe

Posted 04 May 2013 - 01:44 PM

This has been dormant for 6 hours, because no one knows what you're asking. Can you describe what you mean by "create panel" and "show another image in the frame." I'm sure it seems simple to you, but we can't be sure what you're talking about and asking for help with.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: move an image in jframe

Posted 04 May 2013 - 01:54 PM

and you should never call paint() directly
and calling Thread.sleep() in a paint() method is a no no either. Ideally paint() should execute in less tahn 16 millisecond. paint() actually draw in memory, it is only when the method exit that the actual image drawed in memory in displayed by the hardware.
Was This Post Helpful? 0
  • +
  • -

#4 metesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 13

Re: move an image in jframe

Posted 04 May 2013 - 11:55 PM

View PostGregBrannon, on 04 May 2013 - 01:44 PM, said:

This has been dormant for 6 hours, because no one knows what you're asking. Can you describe what you mean by "create panel" and "show another image in the frame." I'm sure it seems simple to you, but we can't be sure what you're talking about and asking for help with.

actually i want to show another image in the same frame...........and i want it also to move..............could u help me with any tutorial or source code of moving an image in java......i think it will be more easier to me to learn my mistake.....
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: move an image in jframe

Posted 05 May 2013 - 02:07 AM

There are many tutorials on the web. Search for tutorials on "java animation," and "java 2d game". I don't list any of those as recommendations for you, because most of the tutorials I've looked at assume the user has a basic understanding of Java, Swing, and graphics that I think you lack.

Instead, I recommend you work through a thorough discussion of the whole topic, beginning with a firm foundation. David Eck's free online book (also downloadable) is the kind of thing I think you need. Chapter 6 covers the basics of graphical programming in Java, including an intro to animation. Chapter 13 continues the discussion with some advanced topics.

The chapters in Eck's book stand alone, but they are presented in a certain order for a reason. Look at the titles of Chapters 1 - 5, and if you don't have confidence in those topics, work those before trying Chapter 6.
Was This Post Helpful? 0
  • +
  • -

#6 metesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-April 13

Re: move an image in jframe

Posted 05 May 2013 - 11:48 AM

View PostGregBrannon, on 05 May 2013 - 02:07 AM, said:

There are many tutorials on the web. Search for tutorials on "java animation," and "java 2d game". I don't list any of those as recommendations for you, because most of the tutorials I've looked at assume the user has a basic understanding of Java, Swing, and graphics that I think you lack.

Instead, I recommend you work through a thorough discussion of the whole topic, beginning with a firm foundation. David Eck's free online book (also downloadable) is the kind of thing I think you need. Chapter 6 covers the basics of graphical programming in Java, including an intro to animation. Chapter 13 continues the discussion with some advanced topics.

The chapters in Eck's book stand alone, but they are presented in a certain order for a reason. Look at the titles of Chapters 1 - 5, and if you don't have confidence in those topics, work those before trying Chapter 6.

thanks.....hope for the best
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1