Java Image Problems
  • I making a Swing Java Application and wish to include graphics. My images will not appear. I have a feeling that it has to do with the image itself rather than the code. I am using the following classes:
    import javax.swing.*;
    import java.awt.*;
    import javax.imageio.*;
    import java.io.*;
    What are the technical limitations of Java ImageIO? I can view the images normally, but they do not appear in my app - would this happen because of a specific type of encoding or file size etc? I have already shrunk the JPGs from a massive size and saved at least one of them as GIF, but no improvement. For some of the images I get an error:
    Please verify that you selected a valid image file
    Error: [Ljava.lang.StackTraceElement;@744a6cbf
    Here is my code:

    /*
    ** BunnyFieldPanel class
    ** by Hayden Bech
    **
    ** Controls the graphics for a paddock full of bunnies.
    */

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

    public class BunnyFieldPanel extends JPanel{
    private final String PATH = "images/front_bunny_small.jpg"; //path of image
    private Image img; //image object
    private int x;
    private int y;

    public BunnyFieldPanel() throws IOException{

    //load image
    img = ImageIO.read(new File(PATH));
    System.out.println("2");
    JLabel jlbBunny = new JLabel("B");
    this.add(jlbBunny);

    }

    public void paint(Graphics g){
    super.paint(g);

    //draw the image
    if( img != null)
    g.drawImage(img,0,0, this);

    g.drawString("Hello", 0, 0);
    }

    }

  • /*
    ** Graduation Game
    ** Version 2 - graphics-capable.
    ** by Hayden Bech
    **
    ** View readme.txt for more information.
    */
    import java.util.Scanner;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;

    public class Graduation{
    static BunnyManager bManage = new BunnyManager();
    static Scanner in = new Scanner(System.in); //Used for keyboard input.
    static String input = ""; //Holds keyboard input.
    static BunnyEvents turn = new BunnyEvents(); //Holds each turn's events.

    public static void main(String args[]) throws IOException{
    /*
    ** Main Program
    */

    try{
    //Set up the window
    JFrame f = new JFrame("Graduation");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //FIELD
    BunnyFieldPanel pnlField = new BunnyFieldPanel();
    f.add(pnlField);
    //MENU

    //HELLO
    JLabel jlbHelloWorld = new JLabel("Bunny Game. \rGraphics are coming... \rIt's hard!");
    f.add(jlbHelloWorld);
    f.setSize(400, 400);
    //f.pack();
    f.setVisible(true);
    }
    catch(Exception e){
    System.out.println ( "Please verify that you selected a valid image file");
    System.out.println ("Error: " + e.getStackTrace());
    }

    //Do stuff
    help(); //Initially print out a list of commands
    firstTurn();
    do{
    System.out.print("Enter another action: "); //Prompt
    input = in.next(); //Get input
    input = input.toLowerCase(); //Fix input
    System.out.println();

    chooseAction(input); //Choose action
    }while(!input.equals("exit"));
    }
    //EXCEPRT ONLY - FUNCTIONS CUT OUT TO SAVE CHARs
    }
  • Are you allowed to use an IDE? NetBeans has a GUI editor and I remember adding images to a window/form using it.
  • At this stage I will try anything.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Poll

No poll attached to this discussion.

In this Discussion