Solved

Background images change from my menu

Posted on 2004-08-25
40
252 Views
Last Modified: 2010-03-31
Ok here is my latest version of my game code:

import java.awt.*;
import java.awt.event.*;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.* ;
import javax.imageio.* ;
import java.io.* ;
import javax.swing.* ;

public class JaySnake extends JFrame
{
  class Game extends JPanel implements Runnable
  {
    private BufferedImage backBuffer ;
    private Graphics myBackBuffer ;
   
    public Game()
    {

      /******Try read in backgroundImage file and catch any exception*/
      try
      {
        backgroundImage = ImageIO.read( new File( "default.jpg" ) );
        backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
        backgroundImage = ImageIO.read( new File( "grassBack.jpg" ) );
        backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
      }
      catch( IOException e )
      {
        System.out.println( "Cannot load image file!" ) ;
      }
      backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB ) ;
      myBackBuffer = backBuffer.getGraphics() ;

//set up the menubar
      menubar = new JMenuBar();
      setJMenuBar(menubar);

    }

    long startTime, waitTime, elapsedTime ;
    public void paintComponent( Graphics g )
    {
      g.drawImage( backBuffer, DISPLAY_X, DISPLAY_Y, null ) ;
    }

    public void run()
    {
      //     1000/25 Frames Per Second = 40millisecond delay
      int delayTime = 1000 / 25 ;

      Thread thisThread = Thread.currentThread() ;
      while( running )
      {
        startTime = System.currentTimeMillis() ;
        // render to back buffer now
        render( myBackBuffer ) ;

        repaint() ;
        //  handle frame rate
        elapsedTime = System.currentTimeMillis() - startTime ;
        waitTime = Math.max( delayTime - elapsedTime, 5 ) ;

        try
        {
          Thread.sleep( waitTime ) ;
        }
        catch( InterruptedException e )
        {}
      }
      System.out.println( "Program Exited" ) ;
    }

   
  }

  private Thread gamePlay ;
  private boolean running = true ;
  private Game game ;

  private final int DISPLAY_X ; // value assigned in constructor
  private final int DISPLAY_Y ; // value assigned in constructor

  private static final int DISPLAY_W = 600 ;
  private static final int DISPLAY_H = 600 ;

  int size, hsize = 0, score1 = 0, score2 = 0, highscore = 0 ;

  /****Creates JMenuBar */
  private JMenuBar menubar ;
  /****Creates JMenu to house items */
  private JMenu gameMenu = new JMenu( "Game" ) ;
  private JMenuItem gameStart = new JMenuItem( "Start" ) ;
  private JMenuItem gameQuit = new JMenuItem( "Quit" ) ;

  private JMenu bgMenu = new JMenu( "Backgrounds" ) ;
  private ButtonGroup bgGroup = new ButtonGroup() ;
  private JRadioButtonMenuItem bg1 = new JRadioButtonMenuItem( "Default" ) ;
  private JRadioButtonMenuItem bg2 = new JRadioButtonMenuItem( "Long grass" ) ;
  private JRadioButtonMenuItem bg3 = new JRadioButtonMenuItem( "Yellow Sand" ) ;
  private JRadioButtonMenuItem bg4 = new JRadioButtonMenuItem( "Blue Glass" ) ;

  private JMenu soundMenu = new JMenu( "Enable sounds" ) ;
  private JCheckBoxMenuItem soundMusic = new JCheckBoxMenuItem( "Music" ) ;
  private JCheckBoxMenuItem soundSound = new JCheckBoxMenuItem( "Sounds" ) ;

  private JMenu helpMenu = new JMenu( "Help" ) ;
  private JMenuItem userInstruct = new JMenuItem( "User Instructions" ) ;
  private JMenuItem helpAbout = new JMenuItem( "About" ) ;

  /******JaySnake constructor*/
  public JaySnake()
  {
    /*****Sets title for main window*/
    //setTitle("JaySnake Game");
    //getContentPane().setLayout( new BorderLayout() ) ;
    //setResizable( false ) ;
    //Causes Menu Titles to not show up when un commented
    //setIgnoreRepaint( true ) ;
    game = new Game() ;
    getContentPane().add( game, BorderLayout.CENTER ) ;
    this.setJMenuBar( menubar );

    menubar.add( gameMenu ) ;
    gameMenu.add( gameStart ) ;
    gameMenu.add( gameQuit ) ;

    // ActionListener to quit the game when quit is clicked
    gameQuit.addActionListener( new ActionListener(){  
   
               public void actionPerformed( ActionEvent e )
     {
        System.exit( 0 );
     }
      } ) ;                                                  
                         
    menubar.add( bgMenu ) ;
    bgMenu.add( bg1 ) ;
    bgMenu.add( bg2 ) ;
    bgMenu.add( bg3 ) ;
    bgMenu.add( bg4 ) ;

    bgGroup.add( bg1 ) ;
    bgGroup.add( bg2 ) ;
    bgGroup.add( bg3 ) ;
    bgGroup.add( bg4 ) ;

    menubar.add( soundMenu ) ;
    soundMenu.add( soundMusic ) ;
    soundMenu.add( soundSound ) ;

    menubar.add( helpMenu ) ;
    helpMenu.add( userInstruct ) ;
    helpMenu.add( helpAbout ) ;

    addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        dispose();
        System.exit( 0 ) ;
      }
    } ) ;

    Insets insets = getInsets() ;
    DISPLAY_X = insets.left ;
    DISPLAY_Y = insets.top ;
    resizeToInternalSize( DISPLAY_W, DISPLAY_H ) ;
  }

  public void resizeToInternalSize( int internalWidth, int internalHeight )
  {
    Insets insets = getInsets() ;
    final int newWidth = internalWidth + insets.left + insets.right ;
    final int newHeight = internalHeight + insets.top + insets.bottom ;

    Runnable resize = new Runnable()
    {
      public void run()
      {
        setSize( newWidth, newHeight ) ;
      }
    } ;

    if( !SwingUtilities.isEventDispatchThread() )
    {
      try
      {
        SwingUtilities.invokeAndWait( resize ) ;
      }
      catch( Exception e )
      {}
    }
    else
      resize.run() ;

    validate() ;
  }

  public void start()
  {
    Thread gamePlay = new Thread( game ) ;
    gamePlay.start() ;
  }

  public static void main(String [] arguments)
{
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
    app.setSize( DISPLAY_W, DISPLAY_H ) ;
    app.setTitle( "JaySnake Game" ) ;
    app.setVisible( true ) ;
    Splash splash = new Splash(app);
}



/*Render method for graphics content*/
    public void render( Graphics g )
    {
     if( backgroundImage != null )
        g.drawImage( backgroundImage, 0, 0, this ) ;

      /******Draw scoreboard and player details, sets color for score and player
       ******draws rectangle in same green, draws strings and used variables score1
       ******and score2. Sets color again to black draws another rectangle
       ******that is used to highlight the playing area*/
      g.setColor( Color.green ) ;
      g.drawRect( 1, 0, 598, 32 ) ;
      g.drawString( "Player 1 score: " + score1, 10, 13 ) ;
      g.drawString( "Player 2 score: " + score2, 10, 27 ) ;
      g.drawString( "High score: " + highscore, 510, 15 ) ;
      g.setColor( Color.black ) ;
      g.drawRect( 1, 34, 598, 521 ) ;
     
    }

  /******declared class variables/containers*/
  private Image backgroundImage ;
}


As you may be aware from a previous question I asked about being able to make my background change when I click on the menu items.  See this question http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21105358.html  

I am struggling with this guys sorry and I am getting really stressed :( to an extent where I have just seem to be drawing a blank at every turn.  This is due to my lack of experiecne obviously.  

Anyway assuming that I click on the background menu items I need a solution as to where I should put my code.  Also how should I do it ... Ahhhhhhh :)

pjcrooks2000
0
Comment
Question by:pjcrooks2000
  • 17
  • 12
  • 11
40 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
But i've already answered this one ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
I know I know, lol

I am just being really dumb...  
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Declare the array inside the Game class:

HashMap imageMap = new HashMap(4);

then inside the constructor do:

imageMap.put("Default",  ImageIO.read( new File( "default.jpg" ));
imageMap.put("Long grass",  ImageIO.read( new File( "blueGlass.jpg" ));
imageMap.put("Yellow Sand",  ImageIO.read( new File( "grassBack.jpg" ));
imageMap.put("Blue Glass",  ImageIO.read( new File( "sand.jpg" ));

Then in your actionPerformed have the code:

public void actionPerformed(ActionEvent e)
{
     try
     {
          JMenuItem source = (JMenuItem)(e.getSource());
          // Using a previously instantiated Map (ask if you don't understand)
          activeBackgroundImage = (BufferedImage) game.imageMap.get(source.getText());
          repaint();
     }
     catch (Exception ex)
     {
          System.out.println(ex);
     }
}
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Actually I think you should ask for a delete of this question and continue the discussion on the other thread.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Thanks - since girionis has now kindly implemented my code - can you make sure he gets some points too ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Oooh yes that is of corse one of my main intentions :)  

Ok i will try and do this I am also trying to get the Snake class to work in my game.   It's sort of working but I do not see the previous snake itself in the game.  

No need to delete the question I don't think lets carry it on here :)  I havn't actually seen a methodical set of questions that helps towards making games in here so perhaps the millions of them that I will post will delve into many relevant issue that people will have in the future.

I will hang fire on the points until I have got somewhere with it .... thanks
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Girionis, just one point.   I have obviously an internal Game class and the Main JaySnake class.  Did you mean do the code inside my JaySnake constructor?

0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> Thanks - since girionis has now kindly implemented my code - can you make sure he gets some points too ;-)

Actually I didn't implement it, I just copied pasted it ;)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
ROTFL
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
I have uploaded this game with all of the files so that you can see what the score is with the game too.. seeing as you two are helping me out a real bunch.   :)  www.sfukgamers.co.uk/Game.rar
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
LOL at Girionis ---  Yeah reuasble code don't you just love it :)
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
Comment Utility
This should work:

import java.awt.*;
import java.awt.event.*;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.* ;
import javax.imageio.* ;
import java.io.* ;
import javax.swing.* ;
import java.util.*;

public class JaySnake extends JFrame implements ActionListener
{
  class Game extends JPanel implements Runnable
  {
    private BufferedImage backBuffer ;
    private Graphics myBackBuffer ;
    HashMap imageMap = new HashMap(4);

      public void setImage(String fileName) throws Exception
     {
          if (fileName.equals("default"))
          {
          backgroundImage = ImageIO.read( new File( "default.jpg" ) );
         }
         else if (fileName.equals("Long grass"))
         {
              backgroundImage = ImageIO.read( new File( "grassBack.jpg" ) );
         }
         else if (fileName.equals("Yellow Sand"))
         {
             backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
         }
         else
         {
              backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
         }
     }
      
    public Game()
    {

      /******Try read in backgroundImage file and catch any exception*/
      try
      {
              backgroundImage = ImageIO.read( new File( "default.jpg" ) );
      }
      catch( IOException e )
      {
        System.out.println( "Cannot load image file!" ) ;
      }
      backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB ) ;
      myBackBuffer = backBuffer.getGraphics() ;

//set up the menubar
      menubar = new JMenuBar();
      setJMenuBar(menubar);

    }

    long startTime, waitTime, elapsedTime ;
    public void paintComponent( Graphics g )
    {
      g.drawImage( backBuffer, DISPLAY_X, DISPLAY_Y, null ) ;
    }

    public void run()
    {
      //     1000/25 Frames Per Second = 40millisecond delay
      int delayTime = 1000 / 25 ;

      Thread thisThread = Thread.currentThread() ;
      while( running )
      {
        startTime = System.currentTimeMillis() ;
        // render to back buffer now
        render( myBackBuffer ) ;

        repaint() ;
        //  handle frame rate
        elapsedTime = System.currentTimeMillis() - startTime ;
        waitTime = Math.max( delayTime - elapsedTime, 5 ) ;

        try
        {
          Thread.sleep( waitTime ) ;
        }
        catch( InterruptedException e )
        {}
      }
      System.out.println( "Program Exited" ) ;
    }

   
  }

  private Thread gamePlay ;
  private boolean running = true ;
  private Game game ;

  private final int DISPLAY_X ; // value assigned in constructor
  private final int DISPLAY_Y ; // value assigned in constructor

  private static final int DISPLAY_W = 600 ;
  private static final int DISPLAY_H = 600 ;

  int size, hsize = 0, score1 = 0, score2 = 0, highscore = 0 ;

  /****Creates JMenuBar */
  private JMenuBar menubar ;
  /****Creates JMenu to house items */
  private JMenu gameMenu = new JMenu( "Game" ) ;
  private JMenuItem gameStart = new JMenuItem( "Start" ) ;
  private JMenuItem gameQuit = new JMenuItem( "Quit" ) ;

  private JMenu bgMenu = new JMenu( "Backgrounds" ) ;
  private ButtonGroup bgGroup = new ButtonGroup() ;
  private JRadioButtonMenuItem bg1 = new JRadioButtonMenuItem( "Default" ) ;
  private JRadioButtonMenuItem bg2 = new JRadioButtonMenuItem( "Long grass" ) ;
  private JRadioButtonMenuItem bg3 = new JRadioButtonMenuItem( "Yellow Sand" ) ;
  private JRadioButtonMenuItem bg4 = new JRadioButtonMenuItem( "Blue Glass" ) ;

  private JMenu soundMenu = new JMenu( "Enable sounds" ) ;
  private JCheckBoxMenuItem soundMusic = new JCheckBoxMenuItem( "Music" ) ;
  private JCheckBoxMenuItem soundSound = new JCheckBoxMenuItem( "Sounds" ) ;

  private JMenu helpMenu = new JMenu( "Help" ) ;
  private JMenuItem userInstruct = new JMenuItem( "User Instructions" ) ;
  private JMenuItem helpAbout = new JMenuItem( "About" ) ;

  /******JaySnake constructor*/
  public JaySnake()
  {
    /*****Sets title for main window*/
    //setTitle("JaySnake Game");
    //getContentPane().setLayout( new BorderLayout() ) ;
    //setResizable( false ) ;
    //Causes Menu Titles to not show up when un commented
    //setIgnoreRepaint( true ) ;
    game = new Game() ;
    getContentPane().add( game, BorderLayout.CENTER ) ;
    this.setJMenuBar( menubar );

    menubar.add( gameMenu ) ;
    gameMenu.add( gameStart ) ;
    gameMenu.add( gameQuit ) ;

    // ActionListener to quit the game when quit is clicked
    gameQuit.addActionListener( new ActionListener(){  
   
             public void actionPerformed( ActionEvent e )
     {
        System.exit( 0 );
     }
     } ) ;                                                  
                         
    menubar.add( bgMenu ) ;
    bgMenu.add( bg1 ) ;
    bgMenu.add( bg2 ) ;
    bgMenu.add( bg3 ) ;
    bgMenu.add( bg4 ) ;

    bgGroup.add( bg1 ) ;
    bgGroup.add( bg2 ) ;
    bgGroup.add( bg3 ) ;
    bgGroup.add( bg4 ) ;

      bg1.addActionListener(this);
    bg2.addActionListener(this);
    bg3.addActionListener(this);
    bg4.addActionListener(this);
      
    menubar.add( soundMenu ) ;
    soundMenu.add( soundMusic ) ;
    soundMenu.add( soundSound ) ;

    menubar.add( helpMenu ) ;
    helpMenu.add( userInstruct ) ;
    helpMenu.add( helpAbout ) ;

    addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        dispose();
        System.exit( 0 ) ;
      }
    } ) ;

    Insets insets = getInsets() ;
    DISPLAY_X = insets.left ;
    DISPLAY_Y = insets.top ;
    resizeToInternalSize( DISPLAY_W, DISPLAY_H ) ;
  }

  public void actionPerformed(ActionEvent e)
  {
       try
     {
          JMenuItem source = (JMenuItem)(e.getSource());
          System.out.println(source.getText());
          game.setImage(source.getText());
     }
     catch (Exception ex)
     {
          System.out.println(ex);
     }
  }

  public void resizeToInternalSize( int internalWidth, int internalHeight )
  {
    Insets insets = getInsets() ;
    final int newWidth = internalWidth + insets.left + insets.right ;
    final int newHeight = internalHeight + insets.top + insets.bottom ;

    Runnable resize = new Runnable()
    {
      public void run()
      {
        setSize( newWidth, newHeight ) ;
      }
    } ;

    if( !SwingUtilities.isEventDispatchThread() )
    {
      try
      {
        SwingUtilities.invokeAndWait( resize ) ;
      }
      catch( Exception e )
      {}
    }
    else
      resize.run() ;

    validate() ;
  }

  public void start()
  {
    Thread gamePlay = new Thread( game ) ;
    gamePlay.start() ;
  }

  public static void main(String [] arguments)
{
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
    app.setSize( DISPLAY_W, DISPLAY_H ) ;
    app.setTitle( "JaySnake Game" ) ;
    app.setVisible( true ) ;
    Splash splash = new Splash(app);
}



/*Render method for graphics content*/
    public void render( Graphics g )
    {
     if( backgroundImage != null )
        g.drawImage( backgroundImage, 0, 0, this ) ;

      /******Draw scoreboard and player details, sets color for score and player
       ******draws rectangle in same green, draws strings and used variables score1
       ******and score2. Sets color again to black draws another rectangle
       ******that is used to highlight the playing area*/
      g.setColor( Color.green ) ;
      g.drawRect( 1, 0, 598, 32 ) ;
      g.drawString( "Player 1 score: " + score1, 10, 13 ) ;
      g.drawString( "Player 2 score: " + score2, 10, 27 ) ;
      g.drawString( "High score: " + highscore, 510, 15 ) ;
      g.setColor( Color.black ) ;
      g.drawRect( 1, 34, 598, 521 ) ;
     
    }

  /******declared class variables/containers*/
  private Image backgroundImage ;
}

0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> Did you mean do the code inside my JaySnake constructor?

Nope, I meant inside your Game class :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Whooooooaaaaa, now thats what I was after .... Excellent job... Hmmm now how do I score this one.. sheeeesh

Ok girionis you got that one i think....  CEHJ thank you and I will get your to you very soon :)  
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Thank you but I really think this question needs to be deleted since I didn't say something new, I just put the pieces together.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
?
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Well it worked so I m happy with that, no point deleting it i don't think.  the process has been re defined and thats good enough for me and any other who need help in the future.

Please don't make me delete it :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Now then how about a similar on to do with sound?  Oooh yes and it works form the sound menu...... I got to get me some sounds first though and I will post the question when I do.

I did have a problem with AudioCip sounds the last time around when I was using my current jdk.   Perhaps you can put me straight on a few things hey.... thanks again!
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Ok it's up to you, if it helps you then I have no problem. Just make sure next time we try to resolve a question first by 100% before we move on to the next one :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Well if you're happy not deleting it, i'm not sure i'm happy with getting no points for the use of my code ;-)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Ok good stuff I will do.. I personally don't think I can award you enough points as it stands so I am trying to award you for all that I can... I gotcha though I will try to observe that approach.

Now then  CEHJ would you like to offer some advice on this one?

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21106537.html

Please do as it is an advice based question not a do one.. thanks!
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
CEHJ, I do not see your code being used anywhere.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>CEHJ, I do not see your code being used anywhere.

I'm getting rather confused.

1. pj asks question
2. you refer him to the answer per my code
3. you post answer that looks at a quick glance like the original
4. pj accepts

??
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
You still haven't answered my question. Where is your code being used at the accepted comment?
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility


Ooops keep me out of this one!  :)  
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>You still haven't answered my question

I have. My question answers yours. i.e. i see you have NOT used my code. Why not? Has the question been answered at all?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
I thnik it has been answered, yes (and twice to be fair), by *an* answer. I don't claim it is *the* answer but it is a possible solution.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Have you actually run that code pj?
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Yep i have CEHJ, the mneu now works just that the first iton on the menu set the image to the blueGrass one... but yes it does work without a shadow of a doubt.  
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
So what I mean is that the default image loads = default
when default is selected from the menu it changes it to = blueGlass but needs to be default

Sand, long grass and blue Glass menu items work fine I am just looking at it now!
l
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>but yes it does work

I wasn't disputing that. Just checking you're happy with it
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
yes I am very happy with it thanks apart from the first menu item not working as it should.  When i click on default on the menu it replaces my background with the blue glass one instead of the default one.. :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Well i'm sure someone will fix that for you ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
yep I hope so... heh!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I think that's down to the person who seems to have got points twice over for answering the same question ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
No comment !  But i do like to give points out you know!  :)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
I never run the code but I think I know where the problem is. It should be in the setImage method. The images that load wrongly is because I confused the if statements. Just change them to the ones you want:

 if (fileName.equals("default"))
          {
          backgroundImage = ImageIO.read( new File( "default.jpg" ) );
         }
         else if (fileName.equals("Long grass"))
         {
              // Put here the file you want, maybe it is the blueGrass.jpg instead of grassBack.jpg?
              // Do likewise for the rest of them.
              backgroundImage = ImageIO.read( new File( "grassBack.jpg" ) );
         }
         else if (fileName.equals("Yellow Sand"))
         {
             backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
         }
         else
         {
              backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
         }

I can also run it and see it myself. Do you have the images somewhere?
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Yes I uploaded the full game to www.sfukgamers.co.uk/Game.rar  The imahes I have are there for it.

However I did this and it works

public void setImage(String fileName) throws Exception
     {
          if (fileName.equals("default")) //Sets background to default image
          {
                      backgroundImage = ImageIO.read( new File( "default.jpg" ) );
         }
         else if (fileName.equals("Long grass")) //Sets background to longGrass image
         {
                    backgroundImage = ImageIO.read( new File( "longGrass.jpg" ) );
         }
         else if (fileName.equals("Yellow Sand"))//Sets background to sand image
         {
                   backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
         }
         
         else if (fileName.equals("Blue Glass")) //Sets background to blueGrass image
         {
                   backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
         }
         else //Sets to default if none mathced
         {
                    backgroundImage = ImageIO.read( new File( "default.jpg" ) );
         }
     }

added a new else if for the blue glass and made the else equal default....

:)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Ahh... I see. Nice you got it working at last :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
Comment Utility
Yeah aboput time I got something working on my own :)  Maybe I am starting to come down from the dizzy heights...  I doubt it though!  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now