Results 1 to 14 of 14
Thread: Picture help
- 05-16-2008, 05:14 AM #1
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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!
- 05-16-2008, 05:30 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
The way you tried is ok. Use JLabel for that. What you have done up to now and what you get there.
- 05-16-2008, 05:45 AM #3
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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...
- 05-16-2008, 05:49 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 05-16-2008, 05:52 AM #5
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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
- 05-16-2008, 06:08 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-16-2008, 06:10 AM #7
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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?
- 05-16-2008, 06:16 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-16-2008, 06:43 AM #9
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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)?
- 05-16-2008, 06:51 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-16-2008, 04:51 PM #11
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); } }
- 05-20-2008, 04:09 PM #12
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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
- 05-20-2008, 06:41 PM #13
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.
- 06-03-2008, 05:46 PM #14
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
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
-
how to import a picture into an applet
By cecily in forum Java AppletsReplies: 2Last Post: 01-15-2010, 08:51 PM -
Picture in a JFrame problem
By saytri in forum New To JavaReplies: 3Last Post: 01-12-2008, 09:44 AM -
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