Results 1 to 10 of 10
Thread: [Help] W/ background image
- 01-10-2011, 01:25 AM #1
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
[Help] W/ background image
ide Netbeans 6.9.1
Ok so i am in the process of trying to make a game I just finished setting up some movement now i am trying to add a simple background.
My problem is that if I can make it show a back ground and I can make it show my game character but I can't show them both it will only show one or the other I can't figure out how to make it have both images. My code.
Sorry for the length
Main class
TextOnBackground- I found this on the internet and basically I am trying to mod it to make it work for what I want.PHP Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mygame; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; /** * * @author ***** */ public class Main { public static void main(String[] args) throws IOException { //changed for privacy String path = "C:\\Users\\*****\\Documents\\NetBeansProjects\\MyGame\\src\\Images\\background.jpg"; BufferedImage image = ImageIO.read(new File(path)); TextOnBackground test = new TextOnBackground(image); //image buffer JFrame f = new JFrame(); second s = new second(); // game character and controlls f.getContentPane().add(test);//adding background picture f.add(s); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800,600); f.setLocation(200,200); f.setVisible(true); } }
I dont know if you need this or not butPHP Code:package mygame; import java.awt.*; import java.awt.font.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class TextOnBackground extends JPanel { BufferedImage image; String text = "Hello World"; public TextOnBackground(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw image, centered. int x = (w - image.getWidth())/2; int y = (h - image.getHeight())/2; g2.drawImage(image, x, y, this); // Draw text centered over image. Font font = g2.getFont().deriveFont(36f); g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)font.getStringBounds(text, frc).getWidth(); LineMetrics lm = font.getLineMetrics(text, frc); float sx = (w - width)/2; float sy = (h + lm.getHeight())/2 - lm.getDescent(); g2.setPaint(Color.red); g2.drawString(text, sx, sy); } }
second
PHP Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mygame; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author ***** */ public class second extends JPanel implements ActionListener, KeyListener { Timer t = new Timer(5,this); int x=0 , y=0 , velx=0 , vely=0; String s ="C:\\Users\\****\\Documents\\NetBeansProjects\\MyGame\\src\\Images\\Left.Gif"; Image img=Toolkit.getDefaultToolkit().getImage(s); String h ="C:\\Users\\****\\Documents\\NetBeansProjects\\MyGame\\src\\Images\\background.jpg)"; Image back=Toolkit.getDefaultToolkit().getImage(h); public second() { t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img,x,y,null); } public void actionPerformed(ActionEvent e) { repaint(); x+=velx; y+=vely; } public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code==KeyEvent.VK_UP) { vely=-1; } if (code==KeyEvent.VK_DOWN) { vely=1; } if (code==KeyEvent.VK_LEFT) { velx=-1; } if (code==KeyEvent.VK_RIGHT) { velx=1; } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { int code = e.getKeyCode(); if (code==KeyEvent.VK_UP) { vely=0; } if (code==KeyEvent.VK_DOWN) { vely=0; } if (code==KeyEvent.VK_LEFT) { velx=0; } if (code==KeyEvent.VK_RIGHT) { velx=0; } } }Last edited by gundum584; 01-10-2011 at 01:31 AM.
-
I haven't looked at all of your code, but these two lines worry me:
Where f is your JFrame, you are essentially adding the test JPanel to the contentPane and then the s JPanel to the contentPane immediately after. If you don't know what this does, I strongly urge you to read the Swing layout tutorial where you'll see that the default layout for a JFrame's contentPane is the BorderLayout and that adding these components in a default way adds them both BorderLayout.CENTER. So you're covering the test JPanel with the s JPanel, covering it completely, so it should be no surprise that you see no image.Java Code:f.getContentPane().add(test);// adding background picture f.add(s);
A solution is to use different layouts if you want them side by side, or use a JLayeredPane and a non-opaque overlying JPanel if you want one over the other. Again the tutorials wil give you the necessary details.
- 01-10-2011, 01:54 AM #3
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
all right so now I understand the problem. I look the the swing tutorial a little and read about border layouts I tried messing with that with no real luck I seen that my character was in fact behind my supposed background image which atm is acting as a foreground image. I don't quit understand how to get it to work the way I want it. Could you explain in a little more detail? As im not the best in Java xD. I just need the background to be in the background and my character to be able to be in the front.
Last edited by gundum584; 01-10-2011 at 02:12 AM.
-
- 01-10-2011, 02:26 AM #5
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
I am just trying to have the background picture (test) to display in the back while my character (s) displays in front of it. I don't understand how to do this i looked at the tutorial(Swing layout manager? thats what came up when i googled) and I can't figure out how to show 2 different layers.
Back ground information:
I am trying to create a 2d game such as Mario where there is a back ground that goes by as your character moves forward.
I hope this is clear enough. It's hard for me to learn by just reading I am best at modding something that already exists I have been working on this problem for 3 hours trying to find anything that can help me and I just can't figure it out. So thats why I decided to go to forums for some help.
-
If you need to overlay one JPanel on top of another then consider as one possible way to do this by using a JLayeredPane. The tutorials explain this in pretty good detail.
- 01-10-2011, 02:37 AM #7
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
- 01-10-2011, 03:05 AM #8
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
To bad I am not a self motivater because I give up :( I can't figure it out
-
In my experience success in this (and in most fields) is earned by sitzfleisch -- the ability to stick with a problem and see it through until it is solved. Sorry to be blunt, but if you give up after just a 30 min trial of tutorial study, then you might as well give up on learning programming period because with this attitude, you won't succeed. I hope pray that this is only a temporary hiccup in your motivational powers and that you'll learn to tolerate frustration and embrace perseverance. You can learn self-motivation, and lord willing, you will. Much luck.
Last edited by Fubarable; 01-10-2011 at 03:14 AM.
- 01-10-2011, 05:48 AM #10
Member
- Join Date
- May 2010
- Posts
- 12
- Rep Power
- 0
i was working on the same problem for over 3 hours the only reason why im Giving up easily on this is because i plan on going to college to learn it atm im only 17 so yea I don't have very much experience in it. All though i did get an A in Ap java at my high school the course didn't go in depth in stuff like this so its hard for me to figure out stuff that I don't even know. Like that i was working on this for 3 hours before i went on the forums and i have never even heard of JLayeredPane().
Similar Threads
-
How to make a background image
By Purple Turtle in forum Java SoftwareReplies: 2Last Post: 11-04-2010, 07:32 AM -
how to put a background image in a JPanel
By yanipao in forum New To JavaReplies: 5Last Post: 10-21-2009, 02:27 PM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM -
Why this image background is black ?
By samson in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks