943,405 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 40885
  • Java RSS
Aug 31st, 2004
0

Loading images in AWT

Expand Post »
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
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Aug 31st, 2004
0

Re: Loading images in AWT

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:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 13
Solved Threads: 0
Newbie Poster
hivework is offline Offline
18 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with database assignment
Next Thread in Java Forum Timeline: Glass Pane





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC