im working on code for a basic hangman game with a swing UI. i am using a for loop to initiate all the buttons for the letters. however im getting a null pointer exception on line 39. I have looked it over and am not sure why it is not working properly. The bottom 10 or so lines of code is were the problem is being thrown.
import java.awt.Color;
import javax.swing.*;
public class GameUI {
public static void main(String[] args){
GameUI ui = new GameUI();
ui.initializeUI();
}
public void initializeUI(){
//initialize the window
JFrame window = new JFrame();
window.setSize(500,500);
window.setResizable(false);
window.setVisible(true);
//initialize main panel
JPanel wrapper = new JPanel();
wrapper.setLayout(null);
Color BGColor = new Color(240,230,140);
wrapper.setBackground(BGColor);
window.getContentPane().add(wrapper);
//Creates JLAbel title, this is used for the title of the game
JLabel title = new JLabel("Hangman v0.1");
title.setBounds(10, 5, 200, 50);
wrapper.add(title);
//=================================================================
//Creates JButtons for each letter (ERROR OCCURS BELLOW ON FIRST LINE AFTER LOOP BEIGNS)
//=================================================================
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
JButton[] letterButtons = new JButton[26];
int counter = 0;
for (char letter:alphabet){
letterButtons[counter].setText("" + letter);
//sets positions for each button
int posY = 50;
int posX = counter*5 + 10;
letterButtons[counter].setBounds(posX, posY, 10, 10);
wrapper.add(letterButtons[counter]);
counter++;
}
}
}
Objects in Java are null by default. Those in an Object array are no different. You need to initialise your JButton array letterButtons prior to attempting to invoke any operations on them
for (int i=0; i < letterButtons.length; i++) {
letterButtons[i] = new JButton();
}
JButton[] letterButtons = new JButton[26]; initializes each array element to null. You have to loop through the array and assign each position a new JButton()
Related
I am trying to make a GUI with a Grid layout that presents 3 random non repeatable cards within a file. I named all the cards 1-53.png and am trying to insert it into the panels of left, center, and right. When I try adding the files into my panels, there is an error withe the .add, and I do not know how to fix it.
I have already tried to change the .add and the index. I even tried to turn the int into a component, but nothing works.
public class Question_2 {
static String location = "cards/";
public static void main(String[] args) {
JFrame frmMyWindow = new frmMyWindow("Random Cards");
frmMyWindow.setSize(300, 200);
frmMyWindow.setLocationRelativeTo(null);
frmMyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMyWindow.setVisible(true);
}
}
class frmMyWindow extends JFrame {
JLabel lblName, l;
JPanel panelMain, panelLeft, panelCenter, panelRight;
JFrame f;
public frmMyWindow(String Cards) {
super("Random Cards");
lblName = new JLabel("Cards");
panelMain = new JPanel(new GridLayout(1, 3, 10, 10));
setLayout(new BorderLayout(20, 10));
add(lblName, BorderLayout.NORTH);
add(panelMain, BorderLayout.CENTER);
panelLeft = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));
panelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
panelRight = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));
panelMain.add(panelLeft);
panelMain.add(panelCenter);
panelMain.add(panelRight);
panelLeft.setBorder(new TitledBorder("Card 1"));
panelCenter.setBorder(new TitledBorder("Card 2"));
panelRight.setBorder(new TitledBorder("Card 3"));
int index = (int) Math.round(Math.random() * 53);
int index2 = (int) Math.round(Math.random() * 53);
int index3 = (int) Math.round(Math.random() * 53);
while (index == index2) {
index2 = (int) Math.round(Math.random() * 53);
}
while (index3 == index2 || index3 == index)
;
{
index3 = (int) Math.round(Math.random() * 53);
}
String image = index + ".png";
String image2 = index2 + ".png";
String image3 = index3 + ".png";
panelLeft.add(index);
panelCenter.add(index2);
panelRight.add(index3);
}
}
I want the program to present 3 random cards into the panels, but there is an error with the .add.
The problem is indeed to the add method and how you call it. Container#add method takes as arguments Components. You call it with int arguments.
I even tried to turn the int into a component, but nothing works.
The simplest way (I guess) to "add a number in a container" is to create a JLabel and add to it the number as text. By seeing your first attempt, I guess that you again messed up the methods. Probably in JLabels constructor. You did something like new JLabel(index) where index is an Integer. Which again fails because there is no constructor with int argument. The solution is to create a JLabel AND convert the integer to text:
panelLeft.add(new JLabel(String.valueOf(index)));
After that program can be compiled and run. However some notes are:
Always run your application using SwingUtilities#invokeLater since all Swing applications must run on their own thread.
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
JFrame frmMyWindow = new frmMyWindow("Random Cards");
frmMyWindow.setSize(300, 200);
frmMyWindow.setLocationRelativeTo(null);
frmMyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMyWindow.setVisible(true);
});
}
All class names "should" (well, its the standard convention) start with an Uppercase letter. Rename frmMyWindow to FrmMyWindow.
I am trying to create a program that computes for the Round Robin algorithm. The logic works fine. My problem is with the overriden JPanel that I use to draw the timeline. The timeline goes on and on without definite line length. I want to add the overriden panel to a scroll pane so it can be scrollable.
SampleGPane.class
import java.awt.*;
import javax.swing.*;
public class
SampleGPane
{
/* Timeline elements */
Container timelineContainer;
JFrame timelineFrame = new JFrame ();
JPanel pnlDraw = new JPanel ();
JScrollPane timelineScroll;
public void
launchFrame ()
{
GPanel gpane = new GPanel ();
timelineContainer = timelineFrame.getContentPane ();
timelineScroll = new JScrollPane (gpane);
timelineContainer.add (timelineScroll);
timelineFrame.setSize (500, 250);
timelineFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
timelineFrame.setVisible (true);
}
private class
GPanel extends JPanel
{
#Override
public void
paintComponent (Graphics g)
{
super.paintComponent (g);
int runningLineX = 0;
int runningLineY = 0;
// g.drawLine (50, 50, orderCount * 5, 50);
runningLineX += 50;
runningLineY += 50;
for (int count = 0; count < 35; count++) {
g.drawString ("J" + (count + 1), runningLineX + 50, 25);
runningLineX += 50;
// runningLineY += 50;
g.drawLine (runningLineX, runningLineY, runningLineX + 50, runningLineY);
}
}
}
}
SampleGPane.class is called by SampleLaunch.class
public class
SampleLaunch
{
public static void main (String args[]) {
SampleGPane sgp = new SampleGPane ();
sgp.launchFrame ();
}
}
The problem is, the JScrollPane won't work. It doesn't seem to detect the line. How do I fix this?
You need to override the getPreferredSize() method of your custom panel to return a reasonable size.
The scrollbars will only appear when the preferred size of the component added to the viewport of the scroll pane is greater than the size of the scroll pane.
The timeline goes on and on without definite line length.
The line length will need to match your painting code. So you need parameters to control what to paint. These parameters will also be used in the calculation of the size of the component. In your example you iterate 35 times and increment the x by 50 so the width would be 1750 plus the starting x offset.
So I am making this ABC learning game , What I want to do is if I click on the A button more than once then , the three Images will change their position, What I want to do is this
![enter image description here][1]
When I click on A button, the three image will appear on the screen, the first is apple as I set it that way in the loop, but the second two images will appear randomly, though sometimes one o them is apple again, I could fix that.
My Question is, how can I change that position of the Apple to the second and second image to the first and third image to the second position if the "A" button is clicked more than once.
SO, the result will be the apple will change position based on the click "A" button and other two picture changes their position and chosed randomly from the array.
So, here is my code for the JPanel, where everything takes place.Most of the code is explained in the comments
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import java.text.AttributedCharacterIterator;
import java.util.Random;
import javax.swing.ImageIcon;
/**
*
* #author Dip
*/
public class AbcGeniusPanel extends JPanel implements ActionListener {
//Declare the necessary Variables here
private JButton[] buttons; //create an array for buttons
private BorderLayout layout; //Declare object of BorderLayout
private Image image = null;
private boolean showImage = false;
//Initialize all the variables here
static int index = 0;
int randNumber = 0, id = 0;
int q = 0, w = 0;
int buttonClick = 0;
//Store all the imahges that will appear on the screen into an String type array
private static String[] imageList = {"src/Images/1.png", "src/Images/2.png", "src/Images/3.png", "src/Images/4.png", "src/Images/5.png", "src/Images/6.png", "src/Images/7.png", "src/Images/8.png", "src/Images/9.png", "src/Images /10.png",
"src/Images/11.png", "src/Images/12.png", "src/Images/13.png", "src/Images /14.png", "src/Images/15.png",
"src/Images/16.png", "src/Images/17.png", "src/Images/18.png", "src/Images /19.png", "src/Images/20.png",
"src/Images/21.png", "src/Images/22.png", "src/Images/23.png", "src/Images /24.png", "src/Images/25.png",
"src/Images/26.png"
};
//Define the constructor here
public AbcGeniusPanel() {
ImageIcon[] alphabets = new ImageIcon[26];
setBackground(Color.yellow);
//Load the images for alphabet images into the alphabets array using a for loop
for (int i = 0; i < alphabets.length; i++) {
alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\" + (i + 1) + ".png");
}
//Create a JPnael object
JPanel panel = new JPanel();
//Set a layoutManager on the panel
//panel.setLayout(new FlowLayout(FlowLayout.CENTER)); //This is not workling good
panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now
//Create an array for holdoing the buttons
buttons = new JButton[26];
//This Loop will Store the buttons in the buttons array attatching each image for each button
//Try passing Images inside the JButton parameter later.
for (int i = 0; i < 26; i++) {
buttons[i] = new JButton(alphabets[i]);
}
// Now Setting up a new Borderlayout so that we can set the whole gridLayout at the botton of the panel
setLayout(new BorderLayout(2, 0));
//add the panel to the Border layout
add(panel, BorderLayout.SOUTH);
//Add evenHandling mechanism to all the buttons
for (int k = 0; k < 26; k++) {
buttons[k].addActionListener(this);
}
for (int count1 = 0; count1 < 26; count1++) {
panel.add(buttons[count1]);
}
}
//This Method will generate a random Number and return it
public int random_number() {
int rand_num;
Random generator = new Random(System.currentTimeMillis());
rand_num = generator.nextInt(26);
return rand_num;
}
//This method will draw the font on the Panel
public void paintComponent(Graphics g) {
Font font; //Declare Font object here
font = new Font("Wide Latin", Font.BOLD, 22); //Set font
super.paintComponent(g); //Ensure the drawing in super class
g.setFont(font); //Set the font
g.setColor(Color.RED);
String text = "CLICK ON THE RIGHT IMAGE!"; //Display the text
g.drawString(text, 255, 20);
}
//To draw the picture on the screen we need to override the paint Method
#Override
public void paint(Graphics g) {
super.paint(g);
//Here, x and y will determine the x and y position os each image
int x = 0, y = 0;
// the varibale q is declared above
for (q = 0; q < 3; q++) //This loop will generate three images on the screen
{
if (showImage) {
x = x + 265; //X-Position of the image
y = 90; //Y-Position of the image
//q is declared as q=0, so this will always be true
if (w == 1 || q == 0) {
g.drawImage(image, x, y, image.getWidth(null), image.getHeight(null), null); //This method will put the image on the screen
showImage = true;
w = 0;
}
while (true) //this loop will run anyway
{
//go inside this loop only when the generated random
//doesn't match with the index of the button that was pressed
while ((randNumber = random_number()) != index) {
index = randNumber; //Now put the randomVlaue in the index
this.image = new ImageIcon(imageList[randNumber]).getImage();
showImage = true;
//make w=1 so that we can break from the outer loop
w = 1;
//break from the inner loop
break;
}
//Since we have made the w=1, so we are breaking out of the outer loop
if (w == 1) {
break;
}
}
}
}
}
#Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
id = 0;
while (true) {
//id is set to zero, for example if the button A (buttons[0])is not pressed then it will go below
//to increase id until it matches the index of the button that we pressed
if (source == buttons[id]) {
//get the image of that same index of the buttons and then set the showImage true
//SO the the paint function above can draw the image
this.image = new ImageIcon(imageList[id]).getImage();
showImage = true;
//save the index of the button that is presed in another variable
//then break from the while loop
index = id;
break;
} else {
id++;
//This is necessary to make sure that id will cross 26
//becasue we have only 26 letters or the array index is 26
//so highest value can be 26 only
id = id % 26;
}
}
repaint();
}
}
Add 3 JLabels or JButtons (whatever will be displaying the images) into a JPanel container. The JPanel will likely use a GridLayout(1, 3, horizontal_gap, 0) layout.
Place all images as ImageIcons into an ArrayList.
Shuffle the ArrayList when needed
After shuffling place the Icons into the JLabels/JButtons in a for loop using the setIcon(...) method.
Note that
your JPanel should override paintComponent, not paint. The paint method is responsible for painting a component's children and borders, and it does not use double buffering by default, making a more dangerous method to override.
Putting in a while (true) loop into your Swing GUI without regard to threading is extremely dangerous.
Putting this into a painting method such as paint is GUI suicide. Never do this since a painting method is a major determinant in the perceived responsiveness of your program. If you slow it down, the program will be perceived as being slow and poorly responsive, and thus it must be lean and fast as possible, and you should have painting and only painting code within it.
Your paint method has program logic in it, something that also shouldn't be done. You don't have full control over whether or even if a painting method will be called, and so program logic should never be placed inside one of these.
As MadProgrammer well notes, don't use the src path for your images as this won't exist once you build your program into a jar file. Better to Create a resource directory in the jar file, and to refer to your images as resources, not as files.
so im trying to create a program in java which will create a 10 by 10 matrix, with each element displaying either a 1 or a 0 randomly. Here is what i have so far:
package random.matrix;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
class ex2 extends JFrame {
class Random {
GridLayout setLayout= new GridLayout(10, 10);
for (int i = 0; i < 10; i++) {
int number = (int) (Math.random() * 2);
String str = Integer.toString(number);
add(new JLabel(str, JLabel.CENTER));
}
}
public static void main(String[] args) {
JFrame frame = new ex2();
frame.setTitle("RandomMatrix");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
As far as I can tell, this program should run perfectly. However, every time I try, it says something along the lines of "illegal start of type," referring specifically to the for loop line. Can anyone help me troubleshoot this? I've never encountered an error quite like this one.
You need to place your code in a code block such as a method or constructor rather than the class block of an inner class
/**
* TODO: Refactor later NOT to extend from JFrame
*/
class MyFrame extends JFrame {
void initComponents() {
GridLayout setLayout = new GridLayout(10, 10);
for (int i = 0; i < 10; i++) {
...
}
}
...
}
You can't have arbitrary statements inside a class definition. Perhaps you want to put it in the constructor?
class Random {
public Random() {
GridLayout setLayout = new GridLayout(10, 10);
for (int i = 0; i < 10; i++)
{
int number = (int) (Math.random() * 2);
String str = Integer.toString(number);
setLayout.add(new JLabel(str, JLabel.CENTER));
}
}
}
Or, you could just create another method and place it in there.
Ok, so I'm trying to add an array of 64 JButtons to a JFrame with and 8 by 8 grid layout (chess board type thing). Here's the relevant code section:
public class othello implements ActionListener{
int [][] board = new int[8][8];
JFrame window = new JFrame();
JButton[] buttons = new JButton[64];
public othello(){
window.setSize(400,400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(8,8));
window.setVisible(true);
for (int i=0;i<64;i++){
buttons[i] = new JButton("");
buttons[i].addActionListener(this);
window.add(buttons[i]);
}
for (int i=0;i<8;i++){
for (int j=0;j<8;j++){
board[i][j]=2;
}
}
board[3][3]=0;board[4][4]=0;
board[3][4]=1;board[4][3]=1;
}
public void actionPerformed(ActionEvent e){
for (int i=0;i<8;i++){
for (int j=0;j<8;j++){
if(e.getSource()==buttons[i]){
buttons[i].setEnabled(false);
board[i][j]=1;
check();
}
}
}
}
public static void main (String[] args){
new othello();
}
}
What this code results in is a seemingly random number of buttons actually being added. Occasionally it adds all 64, more commonly it adds perhaps half or so, it always starts properly but stops at an arbitrary point (I tested by having the button labels count up).
I added some println's to see if the loop itself was actually completing, no problem there, it's going round the loop all 64 times, it just stops adding buttons at some point.
I'm something of a beginner at Java so I'm sure it's something really simple and stupid, but I currently have no idea what's going wrong. Can anyone help?
Edited for more code.
Have you initializated the array? like
JButton[] buttons = new JButtons[x];
make some prints to check in what number the loop stops.
You should be adding to a ContentPane not directly to a JFrame.add()
Like all other JFC/Swing top-level containers, a JFrame contains a
JRootPane as its only child. The content pane provided by the root
pane should, as a rule, contain all the non-menu components displayed
by the JFrame.
Are you setting the layout manager to something other than the default?
The default content pane will have a BorderLayout manager set on it.
Refer to RootPaneContainer for details on adding, removing and setting
the LayoutManager of a JFrame.
the proper idiomatic code to add contents is as follows:
window.getContentPane().add(child);
Refer to the Javadoc for details on proper use.
Maybe you are not using the event dispatch thread to manipulate the UI?
From the Swing Tutorial:
The event dispatch thread, where all event-handling code is executed.
Most code that interacts with the Swing framework must also execute on
this thread.
Try to run your UI construction code using SwingUtilities.invokeAndWait().
Edit: the corrected source code would be:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class othello implements ActionListener
{
int[][] board = new int[8][8];
JFrame window = new JFrame();
JButton[] buttons = new JButton[64];
public othello()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
window.setSize(400, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(8, 8));
window.setVisible(true);
for (int i = 0; i < 64; i++)
{
buttons[i] = new JButton("");
buttons[i].addActionListener(othello.this);
window.getContentPane().add(buttons[i]);
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
board[i][j] = 2;
}
}
board[3][3] = 0;
board[4][4] = 0;
board[3][4] = 1;
board[4][3] = 1;
}
});
}
catch (Exception e)
{
// TODO Handle exception
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (e.getSource() == buttons[i])
{
buttons[i].setEnabled(false);
board[i][j] = 1;
// check();
}
}
}
}
public static void main(String[] args)
{
new othello();
}
}