Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem changing the image in a JLabel

  1. #1
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Problem changing the image in a JLabel

    Hi,
    I am having a problem with the following program. The program is supposed load four random card images and put them in them on four JLabels. The user is supposed to enter an expression using the four numbers provided that equals 24 and verify that the expression is correct. Then you can hit the refresh button and generate four new cards. It works fine the first time but when I hit refresh the images do not change. I've checked the numbers and the filenames the program generates after the refresh button is hit and they look good. I've tried using the setIcon method with the ImageIcon's but I get NullPointerExceptions. Any clue as to what I am doing wrong?

    Casey

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Exercise25_7 extends JFrame {
      private Integer[] numbersPicked = {0, 0, 0, 0};
      private int[] fileNumber = new int[4];
      private MyArrayList<Integer> list;
      private ImageIcon card1Icon, card2Icon, card3Icon, card4Icon;
      private JLabel jlblCard1, jlblCard2, jlblCard3, jlblCard4;
      private JButton jbtRefresh = new JButton("Refresh");
      private JButton jbtVerify = new JButton("Verify");
      private JTextField jtfExpression = new JTextField(10);
     // private int expressionNumber1, expressionNumber2, expressionNumber3, expressionNumber4, imageFileNumber1, imageFileNumber2, imageFileNumber3, imageFileNumber4;
      private String imageFile1 = "image/card/";
      private String imageFile2 = "image/card/";
      private String imageFile3 = "image/card/";
      private String imageFile4 = "image/card/";
     
      public Exercise25_7() {
        pickCards();
        JPanel p1 = new JPanel(new BorderLayout());
    	p1.add(jbtRefresh, BorderLayout.EAST);
    	JPanel p2 = new JPanel(new GridLayout(1, 4));
    	p2.add(jlblCard1);
    	p2.add(jlblCard2);
    	p2.add(jlblCard3);
    	p2.add(jlblCard4);
    	JPanel p3 = new JPanel(new GridLayout(1, 3));
    	p3.add(new JLabel("Enter an expression: "));
    	p3.add(jtfExpression);
    	p3.add(jbtVerify);
    	add(p1, BorderLayout.NORTH);
    	add(p2, BorderLayout.CENTER);
    	add(p3, BorderLayout.SOUTH);
     
    	jbtRefresh.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    clearData();
    	    pickCards();
    	  }
    	});
     
    	jbtVerify.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    int result = 0;
    	    String expression = jtfExpression.getText();
    		boolean correct = checkExpression(expression);
    		if (correct) {
    		  result = EvaluateExpression.evaluateExpression(expression);
    		}
    		else {
    		  System.out.println("Expression includes invalid numbers");
    		}
    		if (result == 24)
    		  System.out.println(expression + " = 24");
    		else if ((result != 24) && correct)
    		  System.out.println(expression + " does not equal 24");
    	  }
    	});
     
      }
     
      public boolean checkExpression(String expression) {
        list = new MyArrayList<Integer>(numbersPicked);
        java.util.StringTokenizer tokens = new java.util.StringTokenizer(expression, "()+-/*", true);
       // String tokens[] = expression.split("[ ()+-*/]");
    	while (tokens.hasMoreTokens()) {
    	  String token = tokens.nextToken().trim();
    	  if (token.length() == 0)
    	    continue;
    	  else if (Character.isDigit(token.charAt(0))){
    	    if (!checkNum(Integer.parseInt(token)))
    		  return false;
    	  }
    	}
    	return true;
      }
      public boolean checkNum(int num) {
     
        boolean found = false;
        for (int i = 0; i < list.size(); i++){
    	  if (list.get(i) == num){
    	    found = true;
    		list.remove(i);
    	  }
    	}
    	return found;
      }
     
      public void pickCards() {
        int picked = 0;
    	//private boolean notPicked = true;
    	while (picked < 4) {
    	  int number = (int)(Math.random() * 13 + 1);
    	  if (! isPicked(number)){
    	    picked++;
    	  }
    	}
    	for (int i = 0; i < numbersPicked.length; i++)
    	  System.out.print(numbersPicked[i] + ", ");
    	System.out.println();
    	for (int i = 0; i < fileNumber.length; i++){
    	  int set = (int)(Math.random() * 4 + 1);
    	  if (set == 1)
    	    fileNumber[i] = numbersPicked[i];
    	  else if (set == 2)
    	    fileNumber[i] = numbersPicked[i] + 13;
    	  else if (set == 3)
    	    fileNumber[i] = numbersPicked[i] + 26;
    	  else
    	    fileNumber[i] = numbersPicked[i] + 39;
    	}
    	for (int i = 0; i < fileNumber.length; i++)
    	  System.out.print(fileNumber[i] + ", ");
    	System.out.println();
     
    	imageFile1 = imageFile1 + fileNumber[0] + ".png";
    	imageFile2 = imageFile2 + fileNumber[1] + ".png";
    	imageFile3 = imageFile3 + fileNumber[2] + ".png";
    	imageFile4 = imageFile4 + fileNumber[3] + ".png";
     
    	System.out.println("image file1 " + imageFile1);
    	System.out.println("image file2 " + imageFile2);
    	System.out.println("image file3 " + imageFile3);
    	System.out.println("image file4 " + imageFile4);
     
    	card1Icon = new ImageIcon(imageFile1);
    	card2Icon = new ImageIcon(imageFile2);
    	card3Icon = new ImageIcon(imageFile3);
    	card4Icon = new ImageIcon(imageFile4);
     
    	jlblCard1 = new JLabel(card1Icon);
    	jlblCard2 = new JLabel(card2Icon);
    	jlblCard3 = new JLabel(card3Icon);
    	jlblCard4 = new JLabel(card4Icon);
     
      }
     
      public boolean isPicked(int num) {
        for (int i = 0; i < numbersPicked.length; i++){
    	  if (numbersPicked[i] == num)
    	    return true;
    	  else if (numbersPicked[i] == 0) {
    	    numbersPicked[i] = num;
    		return false;
    	  }
    	}
    	numbersPicked[0] = num;
    	return false;
      }
     
      public void clearData() {
        for (int i = 0; i < numbersPicked.length; i++)
          numbersPicked[i] = 0;
    	imageFile1 = "image/card/";
        imageFile2 = "image/card/";
        imageFile3 = "image/card/";
        imageFile4 = "image/card/";
    	jtfExpression.setText("");
     
      }
     
      public static void main(String[] args) {
     
       Exercise25_7 frame = new Exercise25_7();
     
       frame.setTitle("Exercise 25.7");
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //frame.setSize(400, 300);
       frame.pack();
       frame.setVisible(true);
     
      }
    }


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problem changing the image in a JLabel

    Never mind. I fixed it. Thanks.

Similar Threads

  1. JLabel image?
    By frozen java in forum AWT / Java Swing
    Replies: 2
    Last Post: September 28th, 2011, 07:44 AM
  2. Replies: 0
    Last Post: February 14th, 2011, 06:10 AM
  3. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM
  4. A problem with JLabel!
    By niklas in forum Member Introductions
    Replies: 1
    Last Post: December 14th, 2010, 06:03 PM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM