Results 1 to 10 of 10
  1. #1
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default [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

    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);
        }
    
    }
    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:
    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);
        }
    
    }
    I dont know if you need this or not but

    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 02:31 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I haven't looked at all of your code, but these two lines worry me:
    Java Code:
          f.getContentPane().add(test);// adding background picture
          f.add(s);
    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.

    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.

  3. #3
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    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 03:12 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gundum584 View Post
    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.
    You'll have to clarify your questions then. I'm not going to try to re-write the tutorials.

  5. #5
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You'll have to clarify your questions then. I'm not going to try to re-write the tutorials.
    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Ok, I was look at the wrong spot I<<< fail so I will look through this and see what I can do.

  8. #8
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    To bad I am not a self motivater because I give up :( I can't figure it out

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gundum584 View Post
    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 04:14 AM.

  10. #10
    gundum584 is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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

  1. How to make a background image
    By Purple Turtle in forum Java Software
    Replies: 2
    Last Post: 11-04-2010, 08:32 AM
  2. how to put a background image in a JPanel
    By yanipao in forum New To Java
    Replies: 5
    Last Post: 10-21-2009, 03:27 PM
  3. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 06:49 PM
  4. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-03-2008, 12:37 AM
  5. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •