Image not displaying in Jframe
Hey, im new to java and really bad a programming in general and im require to make a CAD for designing and testing digital circuits. Ive been given some of this code to follow by and my AND.gif is in the Digital.images package hwoever when i drag and drop my ANDGate.java onto my JFrame in an attempt to view it the program does nothing. Im using NetBeans IDE 7.0.1, cheers.
Code:
package Digital;
import java.awt.Image;
public class ANDGate extends javax.swing.Jpanel{
private Image image;
private void setSize(int width, int height) {
throw new UnsupportedOperationException("Not yet implemented");
}
private static class graphics {
public graphics() {
}
}
private void paintComponent(graphics g) {
this.setSize(image.getWidth(null), image.getHeight(null));
java.net.URL url = getClass().getResource("http://www.java-forums.org/images/AND.gif");
image = new javax.swing.ImageIcon(url).getImage();
throw new UnsupportedOperationException("Not yet implemented");
}
public void paintComponent(java.awt.Graphics g)
{
g.drawImage(image, 0, 0, null);
}
}
Re: Image not displaying in Jframe
Quote:
when i drag and drop my ANDGate.java onto my JFrame
I don't understand what this has to do with displaying an image in a java program.
What happens when you compile and execute your program?
Re: Image not displaying in Jframe
I suggest you attempt to learn java at the beginning.
The code you posted is mostly useless bits and pieces that have been copied from somewhere and patched together here.
Re: Image not displaying in Jframe
Yeah ive been going through Programming and object oriented design using java and its given me an idea of how it all works, just not to the extent that is required. This is the exact document outlining what i'm meant to be doing but i've looked through my text book and it doesnt give me any idea of how to address this.
Implement a Java class to represent an AND gate. This Java class will have a visual interface, however we will not need to design it using the NetBeans WYSIWYG editors so, simply create a plain Java class named ANDGate and then change its definition so that it extends javax.swing.JPanel.
To give the AND gate its physical appearance we will load and paint a suitable image file. Firstly create a new Java Package Digital.images and inside that folder add the following image (provided as “AND.gif” in Zip file provided with assignment specification):
To load this image file at runtime we use the following code:
java.net.URL url = getClass().getResource("images/AND.gif");
image = new javax.swing.ImageIcon(url).getImage();
This is best done just once in the AND gate constructor and stored in a private field. In addition to loading the image, we also want to resize the current component to match the size of the image:
this.setSize(image.getWidth(null), image.getHeight(null));
To paint the image to the screen we override the paintComponent method of our base class:
@Override
public void paintComponent(java.awt.Graphics g)
{
g.drawImage(image, 0, 0, null);
}
You can now test the visual properties of this class by creating a JFrame form and dragging ANDGate.java from the Projects window onto the design surface:
Re: Image not displaying in Jframe
To understand what to do, you need to understand how to program with the Swing library, and this will take study and effort on your part. Fortunately there's a decent tutorial available that I know has helped me a great deal, and you can find it here: Intro to Swing
Much luck!
Re: Image not displaying in Jframe
I think you forgot to link the swing tutorial, cheers for this btw.
Re: Image not displaying in Jframe
Quote:
Originally Posted by
Umlaut
I think you forgot to link the swing tutorial, cheers for this btw.
Indeed! Fixed, and thanks!