Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Background images change from my menu

Posted on 2004-08-25
40
258 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
ID: 11891600
But i've already answered this one ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11891645
I know I know, lol

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

Expert Comment

by:girionis
ID: 11891661
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:girionis
ID: 11891681
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
ID: 11891695
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
ID: 11891749
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
ID: 11891761
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
ID: 11891775
> 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
ID: 11891806
ROTFL
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11891820
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
ID: 11891828
LOL at Girionis ---  Yeah reuasble code don't you just love it :)
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 11891829
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
ID: 11891842
> Did you mean do the code inside my JaySnake constructor?

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

Author Comment

by:pjcrooks2000
ID: 11891877
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
ID: 11891896
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
ID: 11891915
?
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11891924
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
ID: 11891941
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
ID: 11891961
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
ID: 11891994
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
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11892002
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
ID: 11892008
CEHJ, I do not see your code being used anywhere.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11892115
>>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
ID: 11892155
You still haven't answered my question. Where is your code being used at the accepted comment?
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11892224


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

Expert Comment

by:CEHJ
ID: 11892272
>>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
ID: 11892343
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
ID: 11892369
Have you actually run that code pj?
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11892409
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
ID: 11892446
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
ID: 11892486
>>but yes it does work

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

Author Comment

by:pjcrooks2000
ID: 11892559
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
ID: 11892605
Well i'm sure someone will fix that for you ;-)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11893293
yep I hope so... heh!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11893354
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
ID: 11893404
No comment !  But i do like to give points out you know!  :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11894241
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
ID: 11894370
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
ID: 11894437
Ahh... I see. Nice you got it working at last :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11894563
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
Way to decrease size of apk file 9 86
login form jsp example 2 36
java mysql insert application 14 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 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