I'm using Dr. Java as a compiler for Java. I am trying to include an image inside my code. I'm building my first GUI program.
This is the code I used:
import java.awt.*;
import javax.swing.*;
public class Test{
public static void main(String[] args){
JFrame frame = new JFrame("Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel primary = new JPanel();
primary.setBackground(color.green);
primary.setPreferredSize(new Dimension(100,100));
JLabel label = new JLabel("Tesing the program");
primary.add(label);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
Lets say there's a file on my desktop called image.gif and I want to include this image in my code.
- Can I store images as variables to use them in my code?
- Where is the image usually stored in a java program and how can I refer to it?
- Is an image a form of object?
- What is the code to include the image I have in the program I am writing on Dr. Java?