| | |
Loading images in AWT
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
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
Views: 18810 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me jarfile java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen server set size sms socket sort sorting spamblocker sql string swing test text-file thread threads time transfer tree windows





