I just started to learn Java Swing and I was trying to make a program that puts 2 images into the Frame window. The code itself doesn't have any errors but when I compile it gives a nullpointerexception, I checked the line with the error and it had to do with my image file but I can confirm the name is correct and I tried both using the path and the name of the image in the package but neither is working.
Error message:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ImageTutorial.<init>(ImageTutorial.java:25)
at ImageTutorial.main(ImageTutorial.java:33)
I'm using the book "How to Program in Java 9th Edition". The following is code from that book. I'm using eclipse. When the code is run the images are not showing up in the frame. The images are located in the directory with the java file and I have tried using the fully qualified image locations with no success. I've looked around on the web but can't see any reason why this code won't work. The code compiles and no errors are reported.
// Fig. 9.13: LabelDemo.java // Demonstrates the use of labels package Fig9_13DisplayTextImagesWithLabels; import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JFrame; public class LabelDemo
I just finished a tutorial on youtube that covers the basic java, and now i want to try and make a game / program thing, only problem is that i have never worked with graphics before (i have tried to make ovals, rectangles, and other things with the paint method tho )
I know how to make a jframe and a jpanel, and i feel like i can make a simple "game" (not a complicated one, just a simple one with a guy walking around on the screen) in fact i have already make a such game, where you are a oval walking around on the screen, so thats not really my problem.
My problem is that i want to draw an image on the screen that can walk around instead of the oval, but how to do that. I have made a new project and set up the jframe and jpanel, and my code looks like this:
(main)
import javax.swing.JFrame; public class main_class { public static void main(String[] args){ JFrame f = new JFrame(); f.setTitle("title"); f.setVisible(true); f.setSize(800,600);
[code]...
but once again, i get a blank jframe with no image and no "test" string . How i can display images on the screen?
I am writing a program that will have a GUI window and displays an image when clicked on the image changes and clicked on again the image changes once more. Here is what i have.
Write a program that shows a square frame filled with 100 buttons labeled 1 to 100. Nothing needs to happen when you press any of the buttons. I keep getting errors i am a beginner.
I have one quick question. If you are extending the acm graphics program, how can you set the location of the entire window? All the set location methods within the graphics program only seem to set the location of graphical objects. I know with JFrames I can use the setLocationRelativeTo(null) to position the JFrame in the center. How to set the location of a graphics window.
how to put a console on a JFrame or on graphics window. I thought about simply using a JtextField however I don't have access to the readline command since it appears to me that it is only found in a Console Program. So basically I was wondering how to put up a console on JFrame so I can issue commands on it and watch graphical stuff happen on the same screen(minus where the console box is of course).
I was working on this project, and I have everything working, except that it doesn't change the colors of the shapes in the other window. Here's the code to see what I'm doing wrong:
Java Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JDemo { //Global Variables static Color color0 = Color.blue; //sets the value in the color0 variable to the method Color.blue . static Color color1 = Color.red; //Sets the value in the color1 variable to the method Color.red .
[Code] ....
The button0 is supposed to switch the color in window1, and button1 is supposed to switch the color in window0.
I am quite new to Java. I have downloaded a tictac example Java applet code and I run it on Eclipse with no problems. But when I try to test it outside Eclipse by doing the following:
I copy the .class file and two gif-images to another location and call the Java from html code:
If I draw (drawLine) the X and O instead of using gif, then it works.... so its really the gif-loading which is the problem.
I have a Windows XP. I also tried to run it inside XAMPP server folder in my machine but the same problem. How could I be able to run it on my machine?
Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:205) at GUI.<init>(GUI.java:26) at Apples.main(Apples.java:7)
i think the problem is to do with my images not being recognised. I put them in my source in User>...>workspace>src which is correct as far as i know. From what i know the images should show up if i look at my src file in eclipse but they dont. I tried changing the file type from .png to .jpg but it makes no difference.
I am new to javafx I start using it instead of swing i used the scene builder for my forms the problem i faced i don't know how to have main screen with menu bar at top and depending the select from the menu it will open other windows and those window must be inside my window just like for example netbeans.
I don't need to open new window in separate i need all windows inside my main window and controlling over them minimize maximize perhaps.
i have created a frame with a menu bar displaying contents..now i want to set a username and password to that flame through an applet how to add applet to the frame??
here is my code ..
import java.awt.*; import java.awt.event.*; import java.applet.*; class MenuFrame extends Frame{ String msg=""; int flag=0; CheckboxMenuItem debug,test;
I created a instance of a class AddItemView, inside StartUpMenuController. I then passed it into the class, the main method is below showing that as well. However when I do this:
else if(e.getSource()==menu.addBtn) { new addItem(); }
I get an error, little red line on the bottom of the text. I am testing the frame at the moment to make sure it is what I want before I move on to the Controller side of it. I just want to display it and go from there.
package mainMenu.Home.DataBase; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import addItemBtn.Home.DataBase.AddItemView; public class StartUpMenuController { StartUpMenu menu; AddItemView addItem;
[code]...
Not sure if this is the issue but the class I am making a new instance of is in a different package. I imported the package though.
i have been struggling with 2 frames.the question here is, how can you let a frame disappear and let another one appear.im making a game and when i click on start it needs to show another frame.got the whole code here:
import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class RushhourStart { public static void main(String [] args) { JFrame frame = new JFrame(); frame.setSize(700, 700); frame.setTitle("Rushhour"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.setVisible(true);
I'm trying to understand and learn Java for GUI but I'm stacked around a project. I had done some other "test app" but on this, I can't figure out what I had done. I can't move my circle around the panel.... Where I had made some errors?
Main class package main; // imports import javax.swing.JFrame; import java.awt.Dimension; public class Main { public static final int WIDTH = 320; public static final int HEIGHT = 240; public static final int SCALE = 2;
I'm pretty new at java and I was wondering on how to get all my methods on one world frame?
The code is this:
public class TurtleTester { public static void main (String[] args) { World world= new World(); Turtle turtle= new Turtle(world); turtle.drawRectangle(50,100); turtle.drawHexagon(100);
[Code]...
When I run the main method, it would give me a bunch of world frames with one method in each one. I'm using BlueJ btw.
This is my frame class, now I want to be able to change the name of the frame, and get the name with frame.getName() in another class. But I'm not sure how to do this, i tried making a new frame in that class but i feel it's just another frame and not the one i made, also tried something like
I wrote the following code to get a frame,button in south and a red color circle,which i got.In the "actionPerformed" method i wrote "setContentPane(mp2)" thinking it would put another panel on the frame with a blue circle but when i pushed the button,the button got stuck and the circled did not change.
import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class Fra implements ActionListener { MyPanel2 mp2; JFrame frame; JButton button; public static void main(String[] args)
I'm new to Java and I'm trying to pass several values from one frame to another. I've searched around and most codes I've come across are from auto generated GUI code. This what I've been trying to do
Frame1
private class SubmitButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { Frame2 f2 = new Frame2(); //call setValue from Frame2 f2.setValue(4.0); f2.setVisible(true); } }
Frame2
private double val; public double getvalue() { return val; } public void setValue(double v)