Results 1 to 8 of 8
Thread: [SOLVED] [newbie] ??.setTitle()
- 05-23-2009, 01:01 PM #1
[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:
Error (argh):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; }
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)
-
Is this ImageFrame supposed to extend a JFrame? Are you missing the "extends JFrame" bit at the top? Just a guess here.
- 05-23-2009, 03:30 PM #3
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.
2. The JFrame just does not show up anywhere. Am I missing out something?Java Code:image = ImageIO.read(new File("c:\\log\\car.jpg"));
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); } } ); } }
-
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:
For problem number 2, uncomment your code now that you've fixed the extends JFrame issue.Java Code:Image image = null; try { image = ImageIO.read(getClass().getResource("myImages/myImage.jpg")); } catch (IOException e) { e.printStackTrace(); }Last edited by Fubarable; 05-23-2009 at 08:40 PM. Reason: corrected resource String.
- 05-23-2009, 09:40 PM #5
Well I tried this, but got a bugger...
OutputJava 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(); } }
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
- 05-24-2009, 10:46 AM #6
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; }
-
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:
On this line:Java Code:public ImageComponent() { try { @SuppressWarnings("unused") Image image = ImageIO.read(new File("c:\\log\\car.jpg")); } catch (IOException e) { e.printStackTrace(); } }
you first declare that image used here is a new Image variable with this:Java Code:Image image = ImageIO.read(new File("c:\\log\\car.jpg"));
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:Image image //....
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:@SuppressWarnings("unused")
Good luck, and HTHJava 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)); }
- 05-24-2009, 05:50 PM #8
Similar Threads
-
Another newbie
By PhHein in forum IntroductionsReplies: 0Last Post: 04-22-2009, 01:26 PM -
Newbie Help
By mattkid in forum New To JavaReplies: 4Last Post: 03-25-2009, 04:55 AM -
I am newbie
By Seoplanner in forum IntroductionsReplies: 0Last Post: 11-11-2008, 01:22 PM -
newbie needs help...
By vicky08 in forum New To JavaReplies: 2Last Post: 03-31-2008, 04:26 PM -
Newbie
By CSnoob87 in forum IntroductionsReplies: 2Last Post: 02-18-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks