Results 1 to 2 of 2
  1. #1
    piachens is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Lightbulb Stand O' Food Game Proyect

    Hi!!
    Well Im going to tell you what Im doing
    In the university give me a task, to make a game in Java (lenguage Im learning)
    I choose the Stand O' Food


    As you can see is and ambitious proyect to me, but I want to try.
    The idea of the proyect is to make the game and olso with thread make a step by step tutorial
    To beging my first step is to make the backgruond of the game and thats were the problems starts, because i dont know how to putit
    what i have is this..

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    public class StandOFood{
    public static final int WIDTH = 1026;
    public static final int HEIGHT = 757;

    public StandOFood() {
    JFrame ventana = new JFrame("Stand O' Foof");
    ventana.setBounds(0,0,WIDTH,HEIGHT);
    ventana.setVisible(true);
    ventana.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }
    public static void main(String[] args) {
    StandOFood inv = new StandOFood();
    }
    }

    So the cuestion is.. how can i put a background image that i attach on my screen??

    THX!!!! very much!!
    Attached Thumbnails Attached Thumbnails Stand O' Food Game Proyect-fondo2.jpg  

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

    Default

    The general idea is to draw it on the background of your contentPane.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class StandOFoodRx {
        public static final int WIDTH = 1026;
        public static final int HEIGHT = 757;
    
        public StandOFoodRx() {
            JFrame ventana = new JFrame("Stand O' Foof");
            ventana.setContentPane(getContentPane());
            ventana.setBounds(0,0,WIDTH,HEIGHT);
            ventana.setVisible(true);
            ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        private JComponent getContentPane() {
            // Image file must be in your classpath.
            // Here it's in the current directory.
            BufferedImage image = getImage("stand-o-food-screenshot2.jpg");
            ImageBackground cp = new ImageBackground(image);
            cp.setLayout(new BorderLayout());
            // contentPanes must be opaque.
            cp.setOpaque(true);
            return cp;
        }
    
        private BufferedImage getImage(String path) {
            BufferedImage image = null;
            try {
                java.net.URL url = getClass().getResource(path);
                System.out.println("url = " + url);
                image = ImageIO.read(url);
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            }
            return image;
        }
    
        public static void main(String[] args) {
            StandOFoodRx inv = new StandOFoodRx();
        }
    }
    
    class ImageBackground extends JPanel {
        BufferedImage image;
    
        ImageBackground(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - image.getWidth())/2;
            int y = (getHeight() - image.getHeight())/2;
            g.drawImage(image, x, y, this);
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Include file from other proyect
    By oleg_gunnar in forum Advanced Java
    Replies: 1
    Last Post: 03-08-2008, 06:20 PM
  3. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 11:01 AM
  4. Need help with random game!
    By silverq_82 in forum New To Java
    Replies: 4
    Last Post: 08-07-2007, 02:58 PM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 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
  •