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?
Can I store images as variables to use them in my code?
yes you can. by using this command: ImageIcon image = new ImageIcon("C:\image.gif");
(instead of C:\image.gif you must write your image location)
Where is the image usually stored in a java program and how can I
refer to it?
imageicon helps you to show your image and put it where you want.
Is an image a form of object?
everything in java is an object, except primitive types.
What is the code to include the image I have in the program I am writing on Dr. Java?
first you must place your image somewhere you wanna refer to. and then define it as I told. and use how you want according the references.
Im building my first GUI program.
Start by reading the Swing tutorial on Creating a GUI With Swing for all the basics and lots of working examples.
I want to include this image in my code.
You can start with the section on How to Use Icons.
Can I store images as variables to use them in my code?
Yes, images are usually stored as variables.
Where is the image usually stored in a java program and how can I refer to it?
Images, basically are termed as application resources, that are used to provide some information, apart from text displayed on the GUI
You can have a look at working with Images and this simple example
As you deploy an application, Images used in the application are often embedded into the JAR file, along with the rest of the application. This answer, might can give you more information, as to how to add a resource to the project
Or you could serialize a java.awt.image.BufferedImage under a class name and store it with the binaries in a resources folder, but why not simply properly structure your applications folders.
java.awt.Toolkit obtained from the swing or awt container is used to acquire images
the java.awt.Graphics object is used paint images onto the container and is also obtained through a method on the gui container you wish to paint on.
Another more complex imaging file manipulation system is in core java api
called javax.imageio
Related
For a project I am working on, I have made a Bingo Card. The basic functionality is that a Card is randomly generated and displayed in STD out as well as in a Swing GUI I made by hand. This application has multiple java files within.
BingoFinal.java - The main file. This is what's run when the program is run.
Bingo_Card.java - creates the Bingo card, prints it to STD out, and checks for bingo
BingoBG.java - Draws the background of the GUI with 2D graphics
DrawBingoCard.java - Calls BingoBG and also creates 25 labels with the board values. When Bingo_Card finds a matching number(inputted by STD IN), it no longer prints the number, and prints Chip.png(which is in the same package folder as the java files), a bingo chip image, making it look covered.
This works flawlessly when I run it through NetBeans, but when I clean and build it, then run the jar in terminal, everything works except for displaying the bingo chip image. Does anybody know why that would happen?
EDIT: Here is how I load the Image
ImageIcon chip; //declare ImageIcon
chip = new ImageIcon("chip75.png"); //define it as chip75.png. It is stored in package folder
JLabel B1Chip; //declare empty Label
B1Chip = new JLabel(chip); //define the Label with just the ImageIcon
B1Chip.setBounds(22, 112, chip.getIconWidth(), chip.getIconHeight()); //place at (22,112)
frame.add(B1Chip, null); //Add to frame
You should be accessing the images, by using :
ImageIcon chip = new ImageIcon(ClassName.class.getResource("/chip75.png"))
More information can be found on the info page of tag: embedded-resource A detailed answer regarding adding images to the NetBeans Project is mentioned on the info-page link.
More importantly, it would be wise, that you use ImageIO.read, since atleast it will let you know, if something goes wrong, by throwing IOException
Throws:
IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.
ImageIcon on the other hand will hide the exception if any :-)
copy the image files into jar or give absolute path of the image files
I would like to, if it is possible, to open MATLAB figure, generated in MATLAB function, on JButton click. (Maybe to add it to a JFrame or something like that.)
So far I done these steps:
in MATLAB: create m-file, add it to a java package project, build it with MATLAB Builder JA;
in Eclipse: create new project, import files, create GUI and on button click call matlab function to do all necessary calculation...and with setText write results in textFileds.
but how can I show figures on button click and is it possible at all?
It would be simplier if you use the matlab GUI (which uses JButton). You can use findjobj
to get the JButton object inside the matlab interface for the uicontrol('Style','pushbutton').
But, if you don't want to do so, you could use JMI. This article gives you some information about it. His book may be very useful for you.
I've created a new Java project without main class in NetBeans and then I've added a jApplet Form (let's call it MainWindow.java) to my project package. After that, I've added few other jApplet Forms that represent different "pages" of my applet application to my package using the GUI builder of NetBeans.
When I run my applet, I can see the MainWindow form with some buttons, label, etc. on the AppletViewer just fine.
Now, I want to make an event when one of my buttons on my MainWindow is pressed, to show another jApplet Form that I've created earlier and show that form instead of the MainWindow form. I'm trying to create a simple navigation system in my jApplet, and I don't know how to make it efficiently!
Can anyone help me with the code that I should write in the MouseClicked event of my button to make it show another form?
Basically, you can't (or shouldn't) design applets like this. There is no effective means to switch from one applet to another.
Instead, you should create one master applet and using something like CardLayout, design separate forms using one thing like JPanel
You'd then able to switch the forms using the CardLayout manager as needed
I am developing an application which have the following windows
If the information entered in the windows are correct than only the user will be prompted with the windows in the above sequence. Now the customer has demanded me with this user interface.
Now I have to add all these windows in the last window format, with the specification's as the user will be allowed in the 2nd portion of the last image if the first information entered is correct.And the user when launches this app see the last image and can change the values as any time in the respective portions of the last window.
I have coded it in Swing Java.I am new to Java. I am working in Netbeans 7.1.2 I have three files as
1)Login.java
-containing my LoginDemo class which have main and form object of extended Jframe class
-Login class extending J frame and implementing action listener(this class creates an J frame of next file Enter the information.
2)Algorithm.java
creates new J frame object of next file if information is correct.
3)TravellingSalesmanProblem.java
gives the output as shown in Optimal Travel Route window.
I am accessing the information using REST call to a website.
So can anyone help me in this?
This will depend on how you structured you code. If you simply placed components directly onto the the windows/frames themselves, then you might be in for some work.
Alternatively, if you used panels, which you then placed onto windows, this might save you some time.
Anyway. Assuming you only have windows.
for each window do
myLastWindow.add(window.getContentPane());
This is pretty simple, but you'll also need to know the layout you want. I'd suggest something like GridLayout or VerticalLayout from the SwingX project.
I make 2D game in AWT and I had all files in one package. Now I divided files into some packages. Images, which I called with:
ImageIcon ii=new ImageIcon(this.getClass().getResource(image));
img=ii.getImage();
now I call with:
ImageIcon ii=new ImageIcon("cz/ryska/awtgame/images/"+image);
img=ii.getImage();
This code is in class in package cz.ryska.awtgame.basic
But when I start game, displays game scene but not display images. Scene is empty. But any Java error is not induced. Images are probably found but not display. What is problem?
..Painting was functional before I change package structure.
You did more than change the package structure. The change is that the first is loading an image by URL, while the 2nd is loading an image from a File (where the String represents the path). An URL can be used with an embedded resource, while a File cannot.
See the info. page on embedded-resource for further info.