Results 1 to 8 of 8
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] ??.setTitle()

    I'm following this tutorial and hope someone can let me know what is wrong with this code. Unfortunately the API is not very helpful if you don't really know the packages.

    :confused:

    Java Code:
    ImageFrame.java
    import javax.swing.JFrame;
    import java.awt.*;
    
    public class ImageFrame {
    	public ImageFrame()
    	{
    		[B]setTitle("ImageTest"); //what did he mean?[/B]
    		setSize("DEFAULT_WIDTH, DEFAULT_HEIGHT"); //what did he mean?
    		
    		//add component to the frame
    		ImageComponent component = new ImageComponent();
    		add(component);
    	}
    	
    	public static final int DEFAULT_WIDTH=300;
    	public static final int DEFAULT_HEIGHT=200;
    }
    Error (argh):
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
    The method setTitle(String) is undefined for the type ImageFrame
    The method setSize(String) is undefined for the type ImageFrame
    The method add(ImageComponent) is undefined for the type ImageFrame

    at ImageFrame.<init>(ImageFrame.java:7)
    at Test$1.run(Test.java:17)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this ImageFrame supposed to extend a JFrame? Are you missing the "extends JFrame" bit at the top? Just a guess here.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    Is this ImageFrame supposed to extend a JFrame? Are you missing the "extends JFrame" bit at the top? Just a guess here.
    Thanks that was it :).

    1. How do I read from a picture file from a relative path. For example within my solution I have an 'images' sub-folder and I would like the path to remain relative.

    Java Code:
    image = ImageIO.read(new File("c:\\log\\car.jpg"));
    2. The JFrame just does not show up anywhere. Am I missing out something?

    Java Code:
    import java.awt.*;
    
    import javax.swing.JFrame;
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		final boolean debug = true;
    		
    		EventQueue.invokeLater(new Runnable()
    			{
    				public void run()
    				{
    					ImageFrame frame = new ImageFrame();
    					[B]//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   INVALID!
    					//frame.setVisible(true); INVALID![/B]
    					if (debug) System.out.println(JFrame.EXIT_ON_CLOSE);
    				}
    			}
    		);
    
    	}
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For number 1, I would place the images in a myImages directory off of the directory that contains your class files, and then use getResource to get the image. Something like:
    Java Code:
        Image image = null;
        try {
          image = ImageIO.read(getClass().getResource("myImages/myImage.jpg"));
        } catch (IOException e) {
          e.printStackTrace();
        }
    For problem number 2, uncomment your code now that you've fixed the extends JFrame issue.
    Last edited by Fubarable; 05-23-2009 at 08:40 PM. Reason: corrected resource String.

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Well I tried this, but got a bugger...

    Java Code:
    	//acquire the image
    		try 
    		{
    			image = [B]ImageIO.read(getClass().getResource("http://www.java-forums.org/images/car.jpg"));[/B]
    		}
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    Output
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at ImageComponent.<init>(ImageComponent.java:23)
    at ImageFrame.<init>(ImageFrame.java:12)
    at Test$1.run(Test.java:18)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Image is under \<root folder for java project under eclipse\images\ See erm.png - Windows Live


    ne ideas?

    :confused:

    Otherwise when I remove the comments :), the picture shows (so it works :).

    I noticed is tiled (see http://cid-b712073b3513eb8e.skydrive...%20i%20zee.png). How can I change this? Do I have to find or build libraries for that?
    Last edited by jon80; 05-24-2009 at 10:48 AM. Reason: update

  6. #6
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Hi,

    All of a sudden the image does not show up within the JFrame, any idea why?

    Java Code:
    Test.java
    import java.awt.*;
    
    import javax.swing.JFrame;
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		final boolean debug = false;
    		
    		EventQueue.invokeLater(new Runnable()
    			{
    				public void run()
    				{
    					@SuppressWarnings("unused")
    					ImageFrame frame = new ImageFrame();
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    					frame.setVisible(true);
    					if (debug) System.out.println(JFrame.EXIT_ON_CLOSE);
    				}
    			}
    		);
    
    	}
    
    }
    
    
    ImageFrame.java
    import javax.swing.JFrame;
    import java.awt.*;
    
    @SuppressWarnings("serial")
    public class ImageFrame extends JFrame {
    	public ImageFrame()
    	{
    		setTitle("ImageTest"); 
    		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
    		
    		//add component to the frame
    		ImageComponent component = new ImageComponent();
    		add(component);
    	}
    	
    	public static final int DEFAULT_WIDTH=300;
    	public static final int DEFAULT_HEIGHT=200;
    }
    
    /**
     * A component that displays a tiled image.
     * @author Administrator
     *
     */
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    
    @SuppressWarnings("serial")
    public class ImageComponent extends JComponent {
    
    	public ImageComponent()
    	{
    	    try {
    	    @SuppressWarnings("unused")
    		Image image = ImageIO.read(new File("c:\\log\\car.jpg"));
    	    } catch (IOException e) {
    	      e.printStackTrace();
    	    }
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		if (image==null) return;
    		
    		int imageWidth = image.getWidth(this);
    		int imageHeight = image.getHeight(this);
    		
    		//draw the image in the top-left corner
    		g.drawImage(image, 0,0,null);
    		
    		//tile the image across the component
    		for (int i = 0; i * imageWidth <= getWidth(); i++)
    		{
    			for (int j = 0; j * imageWidth <= getHeight(); j++)
    			{
    				if (i + j > 0) g.copyArea(0, 0, imageWidth,imageHeight, i * imageWidth, j * imageHeight);
    			}
    		}
    	}
    	
    	private Image image;
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are creating a "shadow" image variable in your ImageComponent constructor which is likely constructed with the proper image, but is not visible to the rest of the class, and thus not used. Let's take this step by step...

    Let's look at your ImageComponent's constructor to see what is going on:
    Java Code:
      public ImageComponent() {
        try {
          @SuppressWarnings("unused")
          Image image = ImageIO.read(new File("c:\\log\\car.jpg")); 
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    On this line:
    Java Code:
    Image image = ImageIO.read(new File("c:\\log\\car.jpg"));
    you first declare that image used here is a new Image variable with this:
    Java Code:
    Image image //....
    By doing this, you're creating a "shadow" variable that has the same name as the class's image variable, but is a completely different variable, one that is visible only within the constructor. If you construct this with the proper image, you will not change the class's image variable which still holds "null". The compiler must have complained to you that you never use this new image variable, and so you apparently tried to fix this with the annotation:
    Java Code:
    @SuppressWarnings("unused")
    Unfortunately by doing this, if you fix the first mistake and remove the "Image" class declaration before the "image" variable, your code will not compile. The solution is to remove the annotation, and remove the Image declaration. I also recommend using a variable or constant to represent the image file's path. This makes it easier to maintain your code. Something like so:

    Java Code:
      public ImageComponent() {
        try {
          //@SuppressWarnings("unused")  **** this line is farkin' you up!
          //Image image = ImageIO.read(new File(IMAGE_PATH)); *** as is this line
          image = ImageIO.read(new File(IMAGE_PATH));  // IMAGE_PATh is a constant String
        } catch (IOException e) {
          e.printStackTrace();
        }
        // I sprinkled in some debug statements throughout your code to find out
        // where the error was.  I see that you've done this too... keep doing it!
        System.out.println("Image is null: " + String.valueOf(image == null));
      }
    Good luck, and HTH

  8. #8
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Oh thanks that was it :) any idea how to change the style from 'Tiled' to something else?

    "If only error messages were clear..."

Similar Threads

  1. Another newbie
    By PhHein in forum Introductions
    Replies: 0
    Last Post: 04-22-2009, 01:26 PM
  2. Newbie Help
    By mattkid in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 04:55 AM
  3. I am newbie
    By Seoplanner in forum Introductions
    Replies: 0
    Last Post: 11-11-2008, 01:22 PM
  4. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •