Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2008, 03:53 PM
Member
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0
piachens is on a distinguished road
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 Images:
File Type: jpg fondo2.jpg (85.8 KB, 0 views)
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-24-2008, 05:28 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
The general idea is to draw it on the background of your contentPane.
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Include file from other proyect oleg_gunnar Advanced Java 1 03-08-2008 06:20 PM
TicTacToe Game Ebtihal New To Java 0 01-09-2008 11:01 AM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-15-2007 11:02 PM
Need help with random game! silverq_82 New To Java 4 08-07-2007 02:58 PM
Help with pong game Eric New To Java 2 07-03-2007 07:02 PM


All times are GMT +2. The time now is 09:29 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org