| | |
Loading images in AWT
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 7
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
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
•
•
Join Date: Mar 2004
Posts: 18
Reputation:
Solved Threads: 0
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:
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)
import java.awt.*; import java.awt.event.*; import java.awt.image.*; class MyCanvas extends Canvas { Image img; public MyCanvas(Image img) { this.img = img; } public void paint (Graphics g) { if (img != null) { g.drawImage(img, 0, 0, 1000, 1000, this); } } public void setImage (Image img){ this.img = img; } } public class Images implements ActionListener, WindowListener { Frame fr = new Frame ("Frame"); Label Label1 = new Label("Label1 "); Button Button1 = new Button("Button 1"); Image Image1; MyCanvas Canvas1; FileDialog fd = new FileDialog(fr,"Open", FileDialog.LOAD); void initialize () { fr.setSize(500,500); fr.setLocation(300,300); fr.setBackground(Color.lightGray); fr.setLayout(new FlowLayout()); fr.add(Label1); fr.add(Button1); fr.addWindowListener(this); Button1.addActionListener(this); Canvas1 = new MyCanvas(null); Canvas1.setSize(1000,1000); fr.add(Canvas1); 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); Canvas1.setImage(Image1); Canvas1.repaint(); } } 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(); } }
![]() |
Similar Threads
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- Problem loading images in ajax (JavaScript / DHTML / AJAX)
- Loading Images (Java)
- Loading images from executable jar file? (Java)
- PHP Dynamic Images Loads Slowly! (PHP)
- Loading images (Java)
Other Threads in the Java Forum
- Previous Thread: I can't figure out how to change my jbutton font and color ( heres the code )
- Next Thread: Factorial program
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods mobile netbeans object open-source os pong problem producer program project projectideas property read recursion reference replaysolutions rim scanner server set size sms sort sql stop string swing terminal threads transforms tree ui unicode validation web windows





