Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with loading an image to a Panel

Reply
Thread Tools

Problem with loading an image to a Panel

 
 
icepac
Guest
Posts: n/a
 
      06-21-2006
Salutations everybody. I want to load an image into a panel. I have
sub-classed the JPanel class in order to be able to override the pain
method, like this :

public class ImageCanvas
extends JPanel {
Image image;
public ImageCanvas() {
super();
this.setBackground(Color.white);
}
public void paint(Graphics g) {
if (image!=null)
{
g.drawImage(image, 0, 0, this);
}
}
public void SetImage(Image img)
{
image = img;
int iW = Math.min(image.getWidth(this),this.getWidth());
int iH = Math.min(image.getHeight(this), this.getHeight());

this.setSize(iW>>1,iH>>1);
//this.setSize(image.getWidth(this)>>1,image.getHeig ht(this)>>1);
setVisible(true);
this.repaint();
}

Now I use this code in order to load my image into a normal jPanel :
(this is the snippet of code that executes loading the image :

//Display images
Image img =
Toolkit.getDefaultToolkit().createImage(strFilePat h);//strFilePath of
course contains the path where the "image.png" is located
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try
{
tracker.waitForAll();
}
catch (InterruptedException exI)
{
System.out.println("Error while waiting tracker"+
exI.getMessage());
}
jCanvas.setMaximumSize(jPanelImg.getSize());
jCanvas.SetImage(img);
jPanelImg.revalidate(); //jPanelImg (type = JPanel) contains
jCanvas which is of type //ImageCanvas
jPanelImg.repaint();


However, when I use this program it loads the image all right but the
background is full of trash. Also if I try to load multiple images
then they all get jumbled together...
How can I clean this up ?

Thanx alot !
Pascal

 
Reply With Quote
 
 
 
 
Andrew T.
Guest
Posts: n/a
 
      06-21-2006

icepac wrote:
> Salutations everybody. I want to load an image into a panel. I have
> sub-classed the JPanel class in order to be able to override the pain


'paint'

> method, like this :
>
> public class ImageCanvas
> extends JPanel {

....
> public void paint(Graphics g) {
> if (image!=null)

....

In Swing components you should override paintComponent(),
rather than paint().

BTW there is a more specialised GUI group..
http://groups.google.com/group/comp.lang.java.gui

HTH

Andrew T.

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      06-21-2006
Andrew T. wrote:
> icepac wrote:
>> Salutations everybody. I want to load an image into a panel. I have
>> sub-classed the JPanel class in order to be able to override the pain

>
> 'paint'
>
>> method, like this :
>>
>> public class ImageCanvas
>> extends JPanel {

> ...
>> public void paint(Graphics g) {
>> if (image!=null)

> ...
>
> In Swing components you should override paintComponent(),
> rather than paint().
>
> BTW there is a more specialised GUI group..
> http://groups.google.com/group/comp.lang.java.gui
>
> HTH
>
> Andrew T.
>


You don't need a MediaTracker if you are going to use the Image
Observer/Producer. It does help if you don't know the size of the image
and need to prepare for it dynamically however.

import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class ImageObserverExample extends Panel {
Image image;
String msg = "Loading Image";
int n;

public ImageObserverExample() {
try {
URL url = new URL(
"http://www.thealpacastore.com/alpacacam/latest640.jpg");
Toolkit toolkit = Toolkit.getDefaultToolkit();
// use createImage instead of getImage - see api docs for why
image = toolkit.createImage(url);
} catch (MalformedURLException murle) {
msg = "MalformedURLException";
}

setPreferredSize(new Dimension(400,300));
}

public void paint(Graphics g) {
// show the number of calls to paint()
System.out.println(++n);
// if there is an image and drawing has not finished
// note: if the image is null drawImage will return true
if (image == null ||
!g.drawImage(image,0,0,getWidth(),getHeight(),this )) {
// draw the message
g.drawString(msg,20,getHeight()/2);
System.out.println(msg);
}
}

public static void main(String[] args) {
Frame frame = new Frame("ImageObserverExample");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
ImageObserverExample examp = new ImageObserverExample();
frame.add(examp,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
icepac
Guest
Posts: n/a
 
      06-21-2006
Thank you alot everybody...
I found my problem. I am replying in order to give those who have the
same problem the answer:
When you override the paint or paintComponent method you need to make
sure that you still call the super.paint or super.paintComponent. This
is what you need to fill out your image background in order not to have
too much static...

so the method should go like

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image!=null)
{
g.drawImage(image, 0, 0, this);
}
}


And it is indeed better to use paintComponent in Swing ...

C you around ! (Also thanx for redirecting me in the GUI specialised
thread)

P.S. : Last question, is there a thread specifically used for JFM and
the MediaPlayer ?

Pascal

 
Reply With Quote
 
Andrew T.
Guest
Posts: n/a
 
      06-21-2006

icepac wrote:
...
> P.S. : Last question,


...yeah, yeah - that's what they all say.

>..is there a thread specifically used for JFM and
> the MediaPlayer ?


You mean the JMF? You might try the forum here..
http://forum.java.sun.com/forum.jspa?forumID=28

HTH

Andrew T.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
loading image -> detect when image is done loading edfialk Javascript 0 05-10-2007 07:28 PM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM
Delete a panel inside a panel (2) HS1 Java 4 12-08-2004 02:48 AM
Delete a panel inside a panel HS1 Java 3 12-07-2004 10:50 PM
Email From Forms with asp:panel.(s) Only one panel works? Harry ASP .Net 3 11-10-2003 03:13 AM



Advertisments