Results 1 to 14 of 14

Thread: Picture help

  1. #1
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default Picture help

    Hi I've been having a problem getting pictures to overlap eachother
    lets say i have 2 pictures 1 is the background, and the other is the person
    How do I place both pictures on the same spot while being able to see the person infront of the background??

    At the moment I've been trying to put imageicons into a JLabel..but that doesn't seem to work properly.

    Thanks a lot!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The way you tried is ok. Use JLabel for that. What you have done up to now and what you get there.



  3. #3
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    the thing is I tried using 2 panels and have the 2 labels at the same spot..
    but they just overlap eachother and I can't make the one on top visible
    I went browsing a bit, and it talked about RootPanes and Glasspanes Im not sure if that will help though...

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Using GlassPanes you can do it.

    Basically what you want to do is, on a frame you have a background image. It covered full area of the frame. Part of that you want to add an image. Am I correct?

  5. #5
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Okay well, you know DDR? (dance dance revolution)
    kinda like this:

    FFR The Game FlashFlashRevolution.com

    Im trying to get the arrows go under the 'top' arrows

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    There all going on flash.

    To do this on Java, better to check on GlassPane. I think it's possible. But I never do such thing before. Better to check that moving arrow is possible on GlassPane.

  7. #7
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    I'm not sure, but could it be possible to
    paint an image and then setting it on a Jframe using X and Y coordinates?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Using x,y coordinates it's possible to set an image in a JFrame. Using swing timer you can move the image. I'm worried about the transparent look of images as your link shows.

  9. #9
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    if I want to use the X Y coordinate method how would I do that?
    would I first add the JLabel into the frame, and then use Jlabel.setLocation(x, y)?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The best way is this. Add a panel on to the frame first. Because it's the container most of the time use. Much easy to work with.

    Then add your image to the label and set the label location as you said. I think it's clear to you.

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class OverlappingImages extends JPanel {
        BufferedImage bgImage;
        BufferedImage fgImage;
    
        OverlappingImages(BufferedImage[] images) {
            bgImage = images[0];
            fgImage = images[1];
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 100;
            int y = 100;
            g.drawImage(bgImage, x, y, this);
            x = 150;
            y = 150;
            g.drawImage(fgImage, x, y, this);
        }
    
        public static void main(String[] args) throws IOException {
            String[] ids = { "--g--", "---h-" };
            String prefix = "images/geek/geek";
            String ext = ".gif";
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int i = 0; i < images.length; i++) {
                String path = prefix + ids[i] + ext;
                images[i] = ImageIO.read(new File(path));
            }
            OverlappingImages test = new OverlappingImages(images);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(300,300);
            f.setVisible(true);
        }
    }

  12. #12
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    wow thanks it works!
    but how would I call the paint componet method again?
    I'm not sure how the method can pass a parameter to itself...
    How can graphics g be passed by the method..inside the method?
    thanks

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The proper way to "call" painting methods is to tell the enclosing class to repaint. For this to work then you must make changes to member variable in the class to control what/how you want the painting behavior to be. You can change the images or use boolean flags to control which images are displayed. The graphics component paints itself acccording the the state which are member variables used to control what happens in the method. Swing handles everything with the graphics context, g.

  14. #14
    deathnote11 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Okay So, I've managed to get it to work, but then sometimes
    it doesn't create the images smoothly..
    What can I do to make it produce the pictures smoothly?
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.lang.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.event.*; //action listener.
    import java.util.*;
    
    
    
    class DdrJ implements ActionListener, KeyListener
    {
    	int depth=1;
    	ArrayList Alist = new ArrayList()
    
    
    
    		
    	ImageIcon bgIMAGE = new ImageIcon (getClass().getResource("test2G.gif"));
    	ImageIcon p4IMAGE = new ImageIcon(getClass().getResource("p4.gif"));
    	ImageIcon p4ModIMAGE = new ImageIcon(getClass().getResource("p4Mod.gif"));
    	JLabel bgL ;
    	int x;
    	int y;
    	APane pane;
    	JButton btn2 = new JButton("coordinates");
    	
    	public static void main(String[] args)
        {
        	new DdrJ();
        }
        DdrJ()
        {
    
        	x=10;
        	y=10;
        	JFrame gameFrame = new JFrame("DdrJ");
        	
        	pane = new APane(x, y, new JLabel(   new ImageIcon(getClass().getResource("p4.gif")))    );
        	  	
        	gameFrame.setContentPane(pane);
        	
        	pane.setLayout(null);
    
    	
        	Insets insets = pane.getInsets();
        	
        	//backgroudn pic and size
        	JLabel bgL = new JLabel(bgIMAGE,new Integer(0));
        	Dimension bgSIZE = bgL.getPreferredSize();
        	
        	//region pic and size
        	JLabel regL = new JLabel(p4ModIMAGE,new Integer(10));
        	Dimension regSIZE = regL.getPreferredSize();
        	
        	//set where it is
        	regL.setBounds(300,300+insets.top,regSIZE.width,regSIZE.height);
        	bgL.setBounds(0+insets.left,0+insets.top,bgSIZE.width,bgSIZE.height);
    
    
        	pane.add(bgL, new Integer(0));
        	pane.add(regL,new Integer(1));
      
    //    	label = new JLabel(image);
      //  	label2 = new JLabel(   new ImageIcon(getClass().getResource("test1G.gif") ) );
        	
        	
        	gameFrame.addKeyListener(this);
        	pane.addKeyListener(this);
     	
        	gameFrame.setSize(500,500);
        	gameFrame.setVisible(true);
        	
        	
        	gameFrame.setSize(500,500);
        	gameFrame.setVisible(true);
        	
        	for(int i=0;i<100;i++)
        	{
        		try
        		{
        			Thread.sleep(500);
        		    make();	
        		}
        		catch(Exception w)
        		{
        			
        		}
    
        	}
    
    		pane.requestFocus();
      
    	
        	
        }
        
      	public void keyTyped ( KeyEvent p )
      	{	
        }  
        public void keyPressed ( KeyEvent p)
        {  
        	if(p.getKeyCode()==KeyEvent.VK_UP)
        	{
    
        		
        		try
    			{
    					
    			}		
    			catch(Exception m)
    			{
    				
    			}
    			
    			for(int i=0;i<Alist.size();i++)
    			{
    
    							
    				if( ((UP)(Alist.get(i) ) ).getY()>=250 )
    				{
    					System.out.println("________________________");
    					
    					if( ((UP)(Alist.get(i))).getY()  >=260  &&  ((UP)(Alist.get(i))).getY()<=290 )
    					{
    						System.out.println("Too early");
    						( (UP)Alist.get(i) ).quick();
    						( (UP)Alist.get(i) ).stop();
    						Alist.remove(i);
    					}
    					
    					
    					else if( ((UP)(Alist.get(i))).getY()  >=290  &&  ((UP)(Alist.get(i))).getY()<=300 )
    					{
    						System.out.println("Hit right on");
    						( (UP)Alist.get(i) ).quick();
    						( (UP)Alist.get(i) ).stop();
    						Alist.remove(i);
    					}
    					
    					else if( ((UP)(Alist.get(i))).getY()  >=300  &&  ((UP)(Alist.get(i))).getY()<=320 )
    					{
    						System.out.println("A little too late");
    						( (UP)Alist.get(i) ).quick();
    						( (UP)Alist.get(i) ).stop();
    						Alist.remove(i);
    					}	
    	
    						
    							
    				}
    				
    				if( ((UP)(Alist.get(i) ) ).getY()>=500 )
    				{
    						( (UP)Alist.get(i) ).quick();
    						( (UP)Alist.get(i) ).stop();
    						Alist.remove(i);
    					
    				}
    				
    				System.out.println(Alist.size());
    	
    				
    
    	  		} 
        	}
        		
    	  		
        }
        		
    	
        public void keyReleased ( KeyEvent p )
        {  
    	
        } 
        
        public void make()
        {
    		try
        	{
        		pane.labChng(  new JLabel(new ImageIcon(getClass().getResource("p4.gif"))));
        		
        		
        		pane.add(pane.label,new Integer(depth));
        		UP arrow1 = new UP(pane);	
        		    	    		
        		Alist.add(arrow1);
        		( (UP)Alist.get(Alist.size()-1) ).start();
        		depth++;
        	}
        	catch(Exception ex)
        	{
        		
        	}
        	pane.requestFocus();
        }
        
        
    	public void actionPerformed(ActionEvent e)
    	{
    	    pane.requestFocus();
    	    	
    
    		
    	}
    	
    } 
    
    class APane extends JLayeredPane
    {
    	int x=500;
    	int y=250;
    	JLabel label;
    	APane(int x, int y, JLabel label)
    	{
    		this.x=x;
    		this.y=y;
    		this.label=label;
    	}
    	
    	public void labChng(JLabel lab)
    	{
    		this.label=lab;
    	}
    	
    }
    
    
    class UP extends Thread
    {
    
    	APane pane;
    	
    	UP(APane pane)
    	{
    		pane = new APane(pane.x,pane.y,pane.label);
    		this.pane=pane;
    	}
    	public void run()
    	{
    		try
    		{
    			while(pane.y<400)
    			{
    				move(10);
    
    			}
    
    		}
    		catch(Exception ex)
    		{
    			
    		}
    		quick();
    
    		stop();
    		System.gc();
    	}
    	void move(int delay) throws InterruptedException
    	{
    		Thread.currentThread().sleep(delay);
    		Insets insets=pane.getInsets();
    		Dimension size = pane.label.getPreferredSize();
    		pane.y++;
    		pane.label.setBounds(300,pane.y+insets.top,size.width,size.height);    	
    		
    	}
    	
    	public void quick()
    	{
    		pane.label.setIcon(null);
    	}
    	
    	public int getY()
    	{
    		return pane.y;
    	}
    	
    	
    }

    Thanks!

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 in a JFrame problem
    By saytri in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 09:44 AM
  3. 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
  •