aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Output image file and displaying it on screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Output image file and displaying it on screen" Watch "Output image file and displaying it on screen" New topic
Author

Output image file and displaying it on screen

feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I want to write a program that allow user to select a number and based on that number an image is displayed on the monitor so is there a way for displaying images
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Of course there is. Go to the Java Tutorials and you find this, which should help.

We usually discuss this sort of thing in the Swing forum, so I shall move this thread.
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I tried the following code from the tutorial but it didnot work
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

There are two ways of displaying an Image:

- paint it directly on a Graphics object, mostly in paintComponent:


- use a JLabel to display it. The most common way of getting an Icon that displays an Image is a class with a name that says it all: ImageIcon.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
I tried it but it is not working?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What have you tried? Can you show us the image painting / creating code?
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Here a example:


Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
it is error in line 37 ImageScreen cannot be resolved to a type
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
No it isn't that error. You have an unnecessary declaration of a thrown Exception, which you ought to get rid of, and the invocation of super.paint(g); in the paintComponent() method should read "super.paintComponent(g);".
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
Hi, I got the following error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
ImageScreen cannot be resolved to a type

at image.main(image.java:39)

My code is:
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Sorry.. i only copy&paste and change some things.
Its orginally a JWindow.
Here it's fixed.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check your compiler error, and fix those first. Campbell already told you how to fix the main one.

Edit: never mind, you beat me to it
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Your code is wrong cause your class name is 'image' and have a Constructor named as ImageScreen and you call a new ImageScreen.

change


to:



If you want more options in this Screen theres a JImagePanel who a friend mine posted in another forum.

i'm searching the code.. when i found i post.

[/code]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
There is no end of confusion in this thread because people aren't copying each other's code correctly. Remember the tiniest spelling mistake can cause errors, and also look at this FAQ.
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
when running it it gives me this error " it doesnot contain a main type"
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
How are you compiling and running?
feda alshahwan
Ranch Hand

Joined: Jul 26, 2008
Posts: 170
using eclipse
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Marky Vasconcellos wrote:How are you compiling and running?
I copied the code you had corrected, when I said to get rid of the Exception declaration, changed the name of the image file and compiled it. The code ran first time, showing the image, only the window started off very small.
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Campbell Ritchie wrote:
Marky Vasconcellos wrote:How are you compiling and running?
I copied the code you had corrected, when I said to get rid of the Exception declaration, changed the name of the image file and compiled it. The code ran first time, showing the image, only the window started off very small.


This is cause i hard-coded the size of the window.

But it's only a example.

@feda
Copy it on the Eclipse and change the Compilation Unit Name for the name of the class (ImageScreen).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
What I meant was, your code worked first time, once I changed the name of the image file. I didn't mean the size was a particular problem. Sorry if I gave that impression. Au contraire, it was correct working code, which I shall remember for next time I can't get my own images displayed
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Dont worry. xD
Jason Check
Greenhorn

Joined: Feb 15, 2009
Posts: 2
If the image is not diplayed, I suggest you make the following changes of you paintComponent:

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}

View, validate and edit X9.37, X9.100-180 and UCD ICL files.
http://www.digertech.com
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Jason Check wrote:If the image is not diplayed, I suggest you make the following changes of you paintComponent:

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}


If the Image is not displayed dont use a workaround.. try understand why its not being displayed.. using Grapgics drawimage directly can cause weird paintning in the screen.
I cant explain it very well.. but the code i post can work perfectly.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Jason Check wrote:If the image is not diplayed, I suggest you make the following changes of you paintComponent:

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}

Also, you forgot to call super.paintComponent. And why did you make the method public? There is absolutely no need for that.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4547
    
    5

Rob Prime wrote:
Also, you forgot to call super.paintComponent.

Necessary if the image does not fill the component. If the image fills the visible area of the component, a call to the super method is redundant.

I usually keep it in and comment it out, if only to remind myself that it's not an oversight ;)

And why did you make the method public? There is absolutely no need for that.

Rob, would you have the time to explain why? I usually declare overridden protected methods as public, without ever really thinking about it.

the Sun tutorial on painting also declares the paintComponent overrids as public.
http://java.sun.com/docs/books/tutorial/uiswing/painting/step2.html
(about halfway down the page, in red)


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Output image file and displaying it on screen