• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Output image file and displaying it on screen

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 78438
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the following code from the tutorial but it didnot work
 
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it but it is not working?
 
Rob Spoor
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried? Can you show us the image painting / creating code?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here a example:
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is error in line 37 ImageScreen cannot be resolved to a type
 
Campbell Ritchie
Marshal
Posts: 78438
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78438
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you compiling and running?
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using eclipse
 
Campbell Ritchie
Marshal
Posts: 78438
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78438
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont worry. xD
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic