Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

awt help required

  Asked By: Harry    Date: Jun 19    Category: Java    Views: 1066
  

I am trying to display an image by clicking a button.
Can anyone tell me what is wrong with my code that it doesnt work.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;

public class ImageLoading extends Frame implements ActionListener {
Image image;
Point origin = new Point(150, 150);



public ImageLoading(String title) {
super(title);
setSize(600, 600);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
System.exit(0);
}
});
setVisible(true);


Button l1 = new Button("search for:");
l1.setBounds(20,140,80,20);
add(l1);
l1.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
System.out.println("Button Pressed");
try
{
image = Toolkit.getDefaultToolkit().getImage("1.gif");
}
catch(NullPointerException e)
{
}


}

public void paint(Graphics g) {
try{
g.drawImage(image, origin.x, origin.y, this);
}
catch(NullPointerException e)
{
}


}


public static void main(String[] args) {
ImageLoading imageLoading1 = new ImageLoading("Image Loading
Example");
}
}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Rosie Hughes     Answered On: Jun 19

perhaps the error is in the line of
Toolkit.
To avoid why not use simple syntax
image = getImage(getDocumentBase(),"1.gif");
image = getImage(getCodeBase(),"1.gif");

 
Answer #2    Answered By: Freya Brown     Answered On: Jun 19

> I am trying to display  an image  by clicking  a button.
> Can anyone tell me what is wrong  with my code  that it doesnt work.
public  void actionPerformed(ActionEvent ae)
> {
> System.out.println("Button Pressed");
> try
> {
> image = Toolkit.getDefaultToolkit().getImage("1.gif");

Add "repaint();" right here, it might work.

> }
> catch(NullPointerException e)
> {
> }
> }


Better, load the image in your constructor (this way there won't be a
loading delay when the button  is pushed). Then use a boolean flag to
determine whether or not the button has been pushed. Make sure you
repaint() after changing the flag.

 
Answer #3    Answered By: Hilma Miller     Answered On: Jun 19

I previously posted #13233) about the interface Observer and the
Class Observable.

I was told on the forums at sun that there may be an issue with the
AWT Thread and updating a JTextArea that implements Observer.

My issue was that it does not update as the Observable class  notifys
the Observers it only does it AFTER my Observable instance has
finished executing.

Anyone have ideas?

 
Didn't find what you were looking for? Find more on awt help required Or get search suggestion and latest updates.




Tagged: