Loading images in AWT

Reply

Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Loading images in AWT

 
0
  #1
Aug 31st, 2004
Hi everyone,
I am trying to load images in the canvas but it seems nothing gets loaded at all but the program compiles without any errors. It is actually a simple image viewer and have tried almost everything. Anyway here is my program and i hope someone can help me see what i did wrong in my program and show me what i am doing wrong. My program is an application and not an applet.
My e-mail is freesoft_2000@yahoo.com

Here is my program
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Images implements ActionListener, WindowListener
{

Frame fr = new Frame ("Frame");
Label Label1 = new Label("Label1 ");
Button Button1 = new Button("Button 1");
Canvas Canvas1 = new Canvas();
FileDialog fd = new FileDialog(fr,"Open", FileDialog.LOAD);
Image Image1;

void initialize ()
{
fr.setSize(500,500);
fr.setLocation(300,300);
fr.setBackground(Color.lightGray);
fr.setLayout(new FlowLayout());
fr.add(Label1);
fr.add(Button1);
Canvas1.setSize(1000,1000);
fr.add(Canvas1);
fr.addWindowListener(this);
Button1.addActionListener(this);
fr.show();
}

void imageload ()
{
fd.show();
if(fd.getFile() == null)
{
Label1.setText("You have not chosen any image files yet");
}
else
{
String d = (fd.getDirectory() + fd.getFile());

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);
}
}

public void paint(Graphics g)
{
g.drawImage(Image1, 0, 0, 1000, 1000, Canvas1);
}


public void windowClosing(WindowEvent e)
{

// Use fr.hide(); for subsequent forms in multi form applications

System.exit(0);
}

public void windowActivated(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}

public void actionPerformed(ActionEvent event)

{
Button b = (Button)event.getSource();

if(b == Button1)
{
imageload();
}

}

public static void main(String args[])
{
Images a = new Images();
a.initialize();
}
}

I apologize for the length of the program in advance

Thank You

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Loading images in AWT

 
0
  #2
Aug 31st, 2004
You see, the paint() method/event that you have "overidden" will never be "dispatched" in your program, because it is a member function of your Images class which does not extend any graphical components.

To solve this problem, I have added MyCanvas class which overrides the paint() event. So instead of adding the Canvas component to the application window, I added MyCanvas, which will respond to each paint event and draw the image.

Here is the code:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. class MyCanvas extends Canvas
  5. {
  6. Image img;
  7. public MyCanvas(Image img)
  8. {
  9. this.img = img;
  10. }
  11. public void paint (Graphics g)
  12. {
  13. if (img != null)
  14. {
  15. g.drawImage(img, 0, 0, 1000, 1000, this);
  16. }
  17. }
  18. public void setImage (Image img){
  19. this.img = img;
  20. }
  21.  
  22. }
  23. public class Images implements ActionListener, WindowListener
  24. {
  25. Frame fr = new Frame ("Frame");
  26. Label Label1 = new Label("Label1 ");
  27. Button Button1 = new Button("Button 1");
  28. Image Image1;
  29. MyCanvas Canvas1;
  30. FileDialog fd = new FileDialog(fr,"Open", FileDialog.LOAD);
  31.  
  32. void initialize ()
  33. {
  34. fr.setSize(500,500);
  35. fr.setLocation(300,300);
  36. fr.setBackground(Color.lightGray);
  37. fr.setLayout(new FlowLayout());
  38. fr.add(Label1);
  39. fr.add(Button1);
  40. fr.addWindowListener(this);
  41. Button1.addActionListener(this);
  42. Canvas1 = new MyCanvas(null);
  43. Canvas1.setSize(1000,1000);
  44. fr.add(Canvas1);
  45. fr.show();
  46. }
  47. void imageload ()
  48. {
  49. fd.show();
  50. if(fd.getFile() == null)
  51. {
  52. Label1.setText("You have not chosen any image files yet");
  53. }
  54. else
  55. {
  56. String d = (fd.getDirectory() + fd.getFile());
  57. Toolkit toolkit = Toolkit.getDefaultToolkit();
  58. Image1 = toolkit.getImage(d);
  59. Canvas1.setImage(Image1);
  60. Canvas1.repaint();
  61. }
  62. }
  63.  
  64. public void windowClosing(WindowEvent e)
  65. {
  66. // Use fr.hide(); for subsequent forms in multi form applications
  67. System.exit(0);
  68. }
  69. public void windowActivated(WindowEvent e)
  70. {
  71. }
  72. public void windowClosed(WindowEvent e)
  73. {
  74. }
  75. public void windowDeactivated(WindowEvent e)
  76. {
  77. }
  78. public void windowDeiconified(WindowEvent e)
  79. {
  80. }
  81. public void windowIconified(WindowEvent e)
  82. {
  83. }
  84. public void windowOpened(WindowEvent e)
  85. {
  86. }
  87. public void actionPerformed(ActionEvent event)
  88. {
  89. Button b = (Button)event.getSource();
  90. if(b == Button1)
  91. {
  92. imageload();
  93. }
  94. }
  95. public static void main(String args[])
  96. {
  97. Images a = new Images();
  98. a.initialize();
  99. }
  100. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC