Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java advice

  • 25-11-2009 11:44AM
    #1
    Registered Users, Registered Users 2 Posts: 244 ✭✭


    im making a simple game, i have a 2d array to store a grid of images representing the play area. are jlabel icons the way to go or is there a different way to use images...

    also, ive done a bit of swing but whats the best way to output the??

    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    do you mean you want to display the image in a label?

    You can just display the image without the need for a label

    Is it an applet your making or a full application?

    you can use swing or awt


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    draffodx wrote: »
    do you mean you want to display the image in a label?

    You can just display the image without the need for a label

    Is it an applet your making or a full application?

    you can use swing or awt

    its a full applictaion. how does one display images without labels? will it still all go in a jframe?


  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    theliam wrote: »
    its a full applictaion. how does one display images without labels? will it still all go in a jframe?

    yep you can just add it to a jframe, see the java documentation


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    what is wrong with this -
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.imageio.*;
    
    public class display {
        Image[][] ImageArray = new Image[11][11];
        try {
            BufferedImage wallImg = ImageIO.read(new File("wall.png"));
            BufferedImage goalImg = ImageIO.read(new File("goal.png"));
            BufferedImage boxOnEmptyFloorImg = ImageIO.read(new File("boxonemptyfloor.png"));
            BufferedImage boxOnGoalImg = ImageIO.read(new File("boxongoal.png"));
            BufferedImage manImg = ImageIO.read(new File("manonemptyfloorU.png"));
            BufferedImage manOnGoalImg = ImageIO.read(new File("manongoalU.png"));
            BufferedImage floorImg = ImageIO.read(new File("emptyfloor.png"));
        } catch (IOException e) {
        }...
    

    for 'try' and 'catch' it says illegal start of type
    and for all the other lines it says 'unreported exception java.io.IOException; must be caught or delcared to be thrown'


  • Registered Users, Registered Users 2 Posts: 39 Talon.ie


    You haven't imported BufferedImage. It's in in the java.awt.image package, so import java.awt.* won't import it.

    Using wildcards in java import statements is generally frowned on. It's better to explicitly import all of your classes.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    Talon.ie wrote: »
    You haven't imported BufferedImage. It's in in the java.awt.image package, so import java.awt.* won't import it.

    Using wildcards in java import statements is generally frowned on. It's better to explicitly import all of your classes.

    i have import java.awt.image.*; anyway

    the problem is something to do with the try catch statements, theyre not working...?


  • Registered Users, Registered Users 2 Posts: 39 Talon.ie


    sorry, brain not working so good right now.

    You have to put your code in a method, not in the class body.
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class display {
        private Image[][] imageArray = new Image[0][0];
        
        public void doSomething() {
            try {
                BufferedImage wallImg = ImageIO.read(new File("wall.png"));
                BufferedImage goalImg = ImageIO.read(new File("goal.png"));
                BufferedImage boxOnEmptyFloorImg = ImageIO.read(new File("boxonemptyfloor.png"));
                BufferedImage boxOnGoalImg = ImageIO.read(new File("boxongoal.png"));
                BufferedImage manImg = ImageIO.read(new File("manonemptyfloorU.png"));
                BufferedImage manOnGoalImg = ImageIO.read(new File("manongoalU.png"));
                BufferedImage floorImg = ImageIO.read(new File("emptyfloor.png"));
            } catch (IOException e) {
            }
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    okay, new question! thought i might as well keep it in this thread.

    i have a sequnce of 'if' statements that get run through and only 1 condition of all the statements will ever work until that one runs changing the variables being tested, which can then interfere. i need it to jump out of the sequence of ifs once one has been run. cant think of a way to do this well... any suggestions?


  • Registered Users, Registered Users 2 Posts: 3,766 ✭✭✭Reku


    theliam wrote: »
    okay, new question! thought i might as well keep it in this thread.

    i have a sequnce of 'if' statements that get run through and only 1 condition of all the statements will ever work until that one runs changing the variables being tested, which can then interfere. i need it to jump out of the sequence of ifs once one has been run. cant think of a way to do this well... any suggestions?
    2 possible ways I can think of;
    • use if only for the first one and else if for the others, so it only tries each if all the preceding conditions failed.
    • use a break command to exit the structure.


  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    theliam wrote: »
    okay, new question! thought i might as well keep it in this thread.

    i have a sequnce of 'if' statements that get run through and only 1 condition of all the statements will ever work until that one runs changing the variables being tested, which can then interfere. i need it to jump out of the sequence of ifs once one has been run. cant think of a way to do this well... any suggestions?

    hmmmm, found what was actually causing the problem but still not convinced something couldnt go wrong from it


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 244 ✭✭theliam


    farohar wrote: »
    2 possible ways I can think of;
    • use if only for the first one and else if for the others, so it only tries each if all the preceding conditions failed.
    • use a break command to exit the structure.

    if, else... :o thanks


Advertisement