• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Output image file and displaying it on screen

 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code from the tutorial but it didnot work
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it but it is not working?
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? Can you show us the image painting / creating code?
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here a example:
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is error in line 37 ImageScreen cannot be resolved to a type
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.. i only copy&paste and change some things.
Its orginally a JWindow.
Here it's fixed.

 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when running it it gives me this error " it doesnot contain a main type"
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you compiling and running?
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using eclipse
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont worry. xD
 
Jason Check
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic