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 10 of 10

Thread: How to update a JLabel in a basic Graphics exercise

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How to update a JLabel in a basic Graphics exercise

    Hi

    I'm having problem with a basic Graphics exercise from Deitel & Deitel's book.

    The task consists of
    1. generate 1 of 3 shapes in a window - line, oval and rectangle - I have set the total number of shapes to 8 but the number of each individual shapes is random (upto a total of 8) and
    2. JLabel at the bottom of the window giving the number of each shape generated

    I can't seem to get point 2 to work - I can get the number of each shape to be shown in IDE console but not in the actual window. I've only attached the code of the MyOval class as the other 2 are pretty much the same.

    Thanks for any help offered.

    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JPanel;
     
     
    public class DrawPanel extends JPanel 
     
    {
    private Random randomNumbers = new Random();
    //private MyLine firstLine;
    //private MyOval firstOval;
    //private MyRectangle firstRectangle;
     
    public static int countL = 0;
    public static int countO = 0;
    public static int countR = 0;
     
    MyShape [] myShapes = new MyShape[8];//randomNumbers.nextInt(5)];
     
     
    int shapeType;  
     
    int x2;
    int x1;
    int y1;
    int y2;
    Color color;
     
     
     
    	public DrawPanel()
    	{
    		setBackground(Color.WHITE);
     
    	}
     
    	public int getx1()
    	{
    		return x1 = randomNumbers.nextInt(300);
     
    	}
     
    	public int getx2()
    	{
    		return x2 = randomNumbers.nextInt(300);
    	}
     
    	public int gety1()
    	{
    		return y1 = randomNumbers.nextInt(300);
    	}
     
    	public int gety2()
    	{
    		return y2 = randomNumbers.nextInt(300);
    	}
     
    	public int shapeType()
    	{
    		return  shapeType = randomNumbers.nextInt(3);
    	}
     
     
    	public Color getColor()
    	{
    		return color = new Color(randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256))  ;
    	}	
     
     
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
     
    		/*for each array element randomly create an object of different shape type*/		
     
    		for (int i=0; i < myShapes.length; i++)
    	{
    		int ws = shapeType(); //decide which of the 3 shape objects is created
     
    		if (ws == 0)
    				{
    				countL++;
    				myShapes[i] = new MyLine( getx1(), getx2(), gety1(), gety2(), getColor() );
    				}
     
    		else if (ws == 1)
    				{
    				countO++;
    				myShapes [i] = new MyOval( getx1(), getx2(), gety1(), gety2(), getColor() );
    				}
     
    		else if (ws == 2)
    				{
    				myShapes[i] = new MyRectangle( getx1(), getx2(), gety1(), gety2(), getColor() );
    				countR++;
    				}
    		}
    	System.out.printf("%d %d %d\n", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);
     
    	for (int i=0; i < myShapes.length; i++)
    	{
    		myShapes[i].draw(g);
    	}
    }
    }
     
     
     
     
     
     
     
     
    import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import javax.swing.JLabel;
     
     
     
    public class TestDraw {
     
    	/**
    	 * @param args
    	 * @return 
    	 */
    	public static void main(String[] args) 
    	{
     
     
     
    		DrawPanel panel = new DrawPanel();
    		JFrame application = new JFrame();
     
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		application.add(panel);
     
    		JLabel southLabel = new JLabel("South");
    		southLabel.setText(whatToSay() );
    		application.add(southLabel, BorderLayout.SOUTH);
     
    		application.setSize(300,300);
    		application.setVisible(true);
     
    	}
     
    		public static String whatToSay()
    		{
    			String wts;
    			wts =  String.format("Lines %d Ovals %d Rectangles %d", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);
    			return wts;
    		}
    	}
     
     
     
     
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class MyOval extends MyShape
    {
    	public MyOval(int x1, int x2, int y1, int y2, Color color)
    	{
    	super (x1,x2,y1,y2,color);
    	}
     
    	public void draw(Graphics g)
    	{
    	g.setColor( getColor() );
    	g.drawOval( getx1(), getx2(), getwdth(), gethght() );
    	}	
    }
     
     
     
     
    import java.awt.Color;
    import java.awt.Graphics;
     
     
    public abstract class MyShape {
     
    	private int x1, x2, y1, y2, xStart, yStart, xEnd, yEnd, wdth, hght;
    	private Color myColor;
     
    	public MyShape()
    	{
    		x1 = 0; x2 = 0; y1 = 0; y2 = 0;
    		myColor = (Color.BLACK);
     
    	}
    	public MyShape (int x1, int x2, int y1, int y2, Color color)
    	{
    		setx1(x1);
    		setx2(x2);
    		sety1(y1);
    		sety2(y2);
    		setmyColor(color);
     
    		//for ovals and recatngles
    		xStart = Math.min(x1,x2);
    		yStart = Math.min(y1,y2);
    		xEnd = Math.max(x1,x2);
    		yEnd = Math.max(y1,y2);
    		wdth = xEnd - xStart;
    		hght = yEnd - yStart;
     
     
    	}
     
    	public void setx1(int x)
    	{
    		if (x >= 0 && x <= 250)
    		x1 = x;
    		else x1 = 0;
    		//return x1;
    	}
     
    	public void setx2(int x)
    	{
    		//if (x >= 0 && x <= 250)
    		x2 = x;
    		//else x2 = 0;
    		//return x1;
    	}
     
    	public void sety1(int y)
    	{
    		//if (y >= 0 && y <= 250)
    		y1 = y;
    		//else y1 = 0;
    		//return x1;
    	}
     
    	public void sety2(int y)
    	{
    		//if (y >= 0 && y <= 250)
    		y2 = y;
    		//else y2 = 0;
    		//return x1;
    	}
     
    	public void setmyColor(Color color)
    	{
    		myColor = color;
    	}
     
     
    	public int getx1()
    	{
    		return x1;
    	}
     
    	public int getx2()
    	{
    		return x2;
    	}
     
    	public int gety1()
    	{
    		return y1;
    	}
     
    	public int gety2()
    	{
    		return y2;
    	}
     
     
    	public Color getColor()
    	{
    		return myColor;
    	}
     
    	public int getwdth()
    	{
    		return wdth;
    	}
     
    	public int gethght()
    	{
    		return hght;
    	}
     
    	abstract void draw(Graphics g);
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Commented out missing class references.

    Where does the code set the text to be shown in the GUI after the counts have been set?

    BTW The count variables should not be static. Use the reference to the class to access the counts vs having them static.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hairLess (January 8th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Thank you for your reply

    The commented out class references should have been deleted as they aren't used.

    The code to set the text shown in the GUI is in the whatToSay() method in the TestDraw class: (sorry forgot how to tag the code here)


    public static String whatToSay()
    {
    String wts;
    wts = String.format("Lines %d Ovals %d Rectangles %d", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);
    return wts;
    }

    I used satitic variables to count the number of objects created of each class type - it seemed to work as using the

    System.out.printf("%d %d %d\n", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);

    in the DrawPanel class produces the numbers in the consoles -but not in the window. When I set the counters eg 1, 2 , 3 those initial numbers are displayed in the window but not the actual updated numbers.

    How can you count the number of objects of a class created if not with a static variable - would a non-static variable work?

    Thanks in advance

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    would a non-static variable work
    yes. The code needs to be changed to get rid of all the static items (except the main() method)
    code to set the text shown in the GUI is in the whatToSay() method
    That method builds a String but does NOT do anything to show that String in the GUI.

    After the counts are made, where does the code call any methods to show those counts in the GUI?
    There is a call to the println method to print the counts, but nothing is done to show those counts in the GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Thanks again I'll work on the non-static variables.

    I thought the code for the label was set up with

    southLabel.setText(whatToSay() );

    in classs TestDraw.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Is that method called AFTER the counts have been assigned values?
    To see what the code sets the text to, print out what the method returns. Add this statement right after the above posted code:
    System.out.println("wTS="+whatToSay());
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    hairLess (January 8th, 2013)

  9. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Thanks again

    That helped identify the issue: each time I re-size the window I wanted the JLabel to update with the number of each shape eg 2,4,2
    This does happen in the console.
    But it doesn't happen in the window because I had assumed that:

    southLabel.setText(whatToSay() ); in the TestDraw Class would be called/updated each time I re-sized the window.

    How can this be achieved?

  10. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Thanks again

    That helped identify the issue: each time I re-size the window I wanted the JLabel to update with the number of each shape eg 2,4,2
    This does happen in the console.
    But it doesn't happen in the window because I had assumed that:

    southLabel.setText(whatToSay() ); in the TestDraw Class would be called/updated each time I re-sized the window.

    How can this be achieved?

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    each time I re-sized the window.
    Add a listener for a component for its being resized. In the listener's method call the method you want called.

    I had assumed that:
    Add a println() statement to print out a message so you will know when a method is being called.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    hairLess (February 14th, 2013)

  13. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How to update a JLabel in a basic Graphics exercise

    Thanks; just saw this as I've been away & also unwell. I'll work on it

Similar Threads

  1. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  2. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  3. Auto Update JLabel on JButton Press?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 06:55 PM
  4. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  5. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM