Results 1 to 10 of 10
  1. #1
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default GLabels - escape characters and pause

    Hi, I'm new to Java, and even newer to this forum (hope I'm posting in the right section). I've been give a class assignment to create and ATM simulator and I have some problems with displaying messages. I use GLabels, and the first issue I came across was not being able to use escape characters. It crossed my mind that a GLabel might not support their use, but every time I use \n in a GLabel string (as in new GLabel("this is \n my string")), it doesn't change anything. The message is displayed on screen and totally ignores not only \n, but other escape characters like \t. Can I actually use a new line in a GLabel, or do I gave to use 2 different objects for each line?

    Another thing I'm struggling with is making a message appear on the screen for a while and then automatically removing it.

    pause(2000);
    removeScreenPin();
    remove(testpin);
    screenPinWait();
    pause(1000);
    removeScreenPinWait();
    screenTranzactions();

    Those methods contain some add or remove methods for GLabels, and when I tried to use pause, it did pause, but it crossed from the code before the first pause to screenTranzactions();. I also tried using Thread.sleep() and wait(), but my code pretty much broke, and no interactions were possible afterwards. I should also mention I'm using that block of code in mouseClicked().

    Any help would be great, but please don't tell me something like go and read java api cus I work with that too, and I couldn't find anything helpful. I also searched on google, but I could only find those sleep and wait methods which don't seem to work. Many thanks to whomever takes their time to leave me a response.

  2. #2
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    Please put your code in code tags:
    example
    Remember to remove the dots!
    [.code]
    Codes goes in here!
    [./code]

    And please post all your code

  3. #3
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    The entire code has up to 1000 lines....and the only thing most of them do is adding or removing GLabels and GRects. I tried in a new program and the pause(); method worked as I wanted it to, but still no clue how to deal with the \n. I'll keep trying to simplify the program to the point where pause(); starts working, and if I don't figure it out I'll post that.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: GLabels - escape characters and pause

    What's a GLabel?

  5. #5
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    import java.awt.event.*;
    import java.awt.Font;
    
    
    public class MyLabels extends GraphicsProgram{
    	
    
    	GRect buttonN1;
    	GRect buttonN2;
    	GRect buttonN3;
    	GRect buttonN4;
    	GRect buttonN5;
    	GRect buttonN6;
    	GRect buttonN7;
    	GRect buttonN8;
    	GRect buttonN9;
    	GRect buttonN0;
    
    	
    	GLabel mesPin;
    	
    	GLabel mesTranz;
    
    
    	
    	private static final int xStart=50;
    	private static final int yStart=20;
    	private static String screenCount=null;
    	private static int[] keyPressed=new int[100];
    	private static int numCounter=0;
    	
    	
    	public void run (){
    		
    		setSize(1000, 650);
    		createInterface();
    		screenPin();
    		addMouseListeners();
    		
    	}
    	
    
    	void createInterface(){
    		
    		
    		buttonN1=new GRect(xStart+120,yStart+420,35,30);
    		buttonN2=new GRect(xStart+160,yStart+420,35,30);
    		buttonN3=new GRect(xStart+200,yStart+420,35,30);
    		buttonN4=new GRect(xStart+120,yStart+460,35,30);
    		buttonN5=new GRect(xStart+160,yStart+460,35,30);
    		buttonN6=new GRect(xStart+200,yStart+460,35,30);
    		buttonN7=new GRect(xStart+120,yStart+500,35,30);
    		buttonN8=new GRect(xStart+160,yStart+500,35,30);
    		buttonN9=new GRect(xStart+200,yStart+500,35,30);
    		buttonN0=new GRect(xStart+160,yStart+540,35,30);
    
    
    		
    		add(buttonN1);
    		add(buttonN2);
    		add(buttonN3);
    		add(buttonN4);
    		add(buttonN5);
    		add(buttonN6);
    		add(buttonN7);
    		add(buttonN8);
    		add(buttonN9);
    		add(buttonN0);
    	
    		
    	}
    	
    	double insertTextCenterX(double x){
    		return xStart+70+(350-x)/2;
    	}
    	
    	double insertTextCenterY(double y){
    		return yStart+20+(300-y)/4;
    	}
    	
    	
    	double insertTextR1X(double x){
    		return xStart+415-x;
    	}
    	
    	double insertTextR1Y(double y){
    		return yStart+120+(35+y/2)/2;
    	}
    	
    		
    	
    	int checkForB1(double x, double y){
    		if(x>xStart+120 && x<xStart+155 && y>yStart+420 && y<yStart+450)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB2(double x, double y){
    		if(x>xStart+160 && x<xStart+195 && y>yStart+420 && y<yStart+450)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB3(double x, double y){
    		if(x>xStart+200 && x<xStart+235 && y>yStart+420 && y<yStart+450)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB4(double x, double y){
    		if(x>xStart+120 && x<xStart+155 && y>yStart+460 && y<yStart+490)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB5(double x, double y){
    		if(x>xStart+160 && x<xStart+195 && y>yStart+460 && y<yStart+490)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB6(double x, double y){
    		if(x>xStart+200 && x<xStart+235 && y>yStart+460 && y<yStart+490)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB7(double x, double y){
    		if(x>xStart+120 && x<xStart+155 && y>yStart+500 && y<yStart+530)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB8(double x, double y){
    		if(x>xStart+160 && x<xStart+195 && y>yStart+500 && y<yStart+530)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB9(double x, double y){
    		if(x>xStart+200 && x<xStart+235 && y>yStart+500 && y<yStart+530)
    			return 1;
    		return 0;
    	}
    	
    	int checkForB0(double x, double y){
    		if(x>xStart+160 && x<xStart+195 && y>yStart+540 && y<yStart+570)
    			return 1;
    		return 0;
    	}
    	
    	
    	void returnKeyValue(double x, double y){
    		
    		if(checkForB1(x,y)==1)
    			keyPressed[++numCounter]=1;
    		else if(checkForB2(x,y)==1)
    			keyPressed[++numCounter]=2;
    		else if(checkForB3(x,y)==1)
    			keyPressed[++numCounter]=3;
    		
    		else if(checkForB4(x,y)==1)
    			keyPressed[++numCounter]=4;
    		else if(checkForB5(x,y)==1)
    			keyPressed[++numCounter]=5;
    		else if(checkForB6(x,y)==1)
    			keyPressed[++numCounter]=6;
    		
    		else if(checkForB7(x,y)==1)
    			keyPressed[++numCounter]=7;
    		else if(checkForB8(x,y)==1)
    			keyPressed[++numCounter]=8;
    		else if(checkForB9(x,y)==1)
    			keyPressed[++numCounter]=9;
    		
    
    		else if(checkForB0(x,y)==1)
    			keyPressed[++numCounter]=0;
    	
    	}
    	
    	void screenPin(){
    		mesPin=new GLabel("Introdu PIN:");
    		mesPin.setLocation(insertTextCenterX(mesPin.getWidth()), insertTextCenterY(mesPin.getHeight()));
    		add(mesPin);
    		screenCount="Pin";
    	}	
    	
    	void screenTranzactions()
    	{
    		mesTranz=new GLabel("Tipul Tranzactiei:");
    		mesTranz.setLocation(insertTextCenterX(mesTranz.getWidth()), insertTextCenterY(mesTranz.getHeight()));
    		
    		add(mesTranz);
    
    		screenCount="Tranzactions";
    	}
    	
    	void removeScreenPin()
    	{
    		remove(mesPin);
    	}
    	
    
    	GLabel pinwait;
    	void screenPinWait(){
    		pinwait=new GLabel("Se proceseaza");
    		pinwait.setLocation(insertTextCenterX(pinwait.getWidth()), insertTextCenterY(pinwait.getHeight()));
    		add(pinwait);
    	}
    	
    	
    	void removeScreenPinWait(){
    		remove(pinwait);
    	}
    	
    	GLabel testpin;
    	void showPin(){//metoda de test
    		if(numCounter==1){
    		testpin=new GLabel (keyPressed[1]+" ");
    		testpin.setLocation(insertTextR1X(testpin.getWidth()), insertTextR1Y(testpin.getHeight()));
    		add(testpin);
    		}
    		else if(numCounter==2){
    			testpin=new GLabel (keyPressed[1]+" "+keyPressed[2]);
    			testpin.setLocation(insertTextR1X(testpin.getWidth()), insertTextR1Y(testpin.getHeight()));
    			add(testpin);
    		}
    		else if(numCounter==3){
    			testpin=new GLabel (keyPressed[1]+" "+keyPressed[2]+" "+keyPressed[3]);
    			testpin.setLocation(insertTextR1X(testpin.getWidth()), insertTextR1Y(testpin.getHeight()));
    			add(testpin);
    		}
    		else if(numCounter==4){
    			testpin=new GLabel (keyPressed[1]+" "+keyPressed[2]+" "+keyPressed[3]+" "+keyPressed[4]);
    			testpin.setLocation(insertTextR1X(testpin.getWidth()), insertTextR1Y(testpin.getHeight()));
    			add(testpin);
    		}
    	}
    	
    	
    	
    	public void mouseClicked(MouseEvent e){
    		
    		double x=e.getX();
    		double y=e.getY();
    		
    
    		if (screenCount.equals("Pin") && numCounter>3){
    			remove(testpin);
    			numCounter=0;
    		}
    
    		
    		else if (screenCount.equals("Pin") && numCounter<4){
    			if(numCounter>0)
    				remove(testpin);
    			returnKeyValue(x,y);
    			showPin();
    			
    		}
    		
    			
    		if(screenCount.equals("Pin") && numCounter==4){
    			pause(1000);
    			removeScreenPin();
    			remove(testpin);
    			screenPinWait();
    			pause(1000);
    			removeScreenPinWait();
    			screenTranzactions();
    		}
    		
    		
    	}
    	
    }
    Sorry for the many blank lines. It's just easier for me to see the code if I write it like that. This is the best I could do to simplify it. Oh, and the program requires acm.java.
    Last edited by flo28rin; 10-27-2011 at 09:11 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: GLabels - escape characters and pause

    If a GLabel extends from a JComponent, you can try html tags:

    Java Code:
    new GLabel("<html>this is<br>my string</html>");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    Quote Originally Posted by JosAH View Post
    If a GLabel extends from a JComponent, you can try html tags:

    Java Code:
    new GLabel("<html>this is<br>my string</html>");
    kind regards,

    Jos
    I think GLabel is extended from GObject. But I'll try to use something from JComponent, see how that turns out.

  8. #8
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    I'm gonna go a bit off topic and sorry about that. I was just wondering if GObject-s such as GRect and GLabel are officially releases of java and if they are included in the official java api. Also, are the being thought in schools? I know they used to teach it at Stanford in 2008 (because of their online classes), but do they still teach it? Most importantly, should a Java teacher know about them? I'm just asking people what they know, if anyone knows for sure. Thanks.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: GLabels - escape characters and pause

    I was just wondering if GObject-s such as GRect and GLabel are officially releases of java
    No, they're not. (That was why I asked my question above).

    I had a look at the documentation at the time (I think from Stanford) and it looks like you *can't* have embedded newlines. Basically the GLabel just uses a graphics context drawString() method on the text content. That's from the source code rather than the - silent on this point - documentation.

    Most importantly, should a Java teacher know about them?
    The classes are part of the "ACM Java Task Force" library. Problems concerning this code occur from time to time on forums like this. I've seen problems arise from the fact that the code is a bit shoddy in places (counterintuitive, lacking error checking, incompletely documented) but also the mere fact that it is nonstandard means that it can be difficult to get help on online fora. My advice - for *students* - would be to try and find some other learning source. There are online tutorials, ebooks etc.

    For *teachers* the problem is a bit more subtle. Clearly, hard work was done by clever people to write the library. There is a rationale for it at the JTF home page. I would recommend a teacher read that information (I haven't done so) and decide for themselves if - and how - they might use the ideas within their course. Sorry for the nonanswer!, but I really don't think there is One Right Way.

    -----

    An aside, but, yesterday, I did see a discussion somewhere in that documentation, of the question of how to handle animation which you raised earlier in this thread. Basically the authors point out that there are *several* approaches which they describe. The problem for the teacher of an *introductory* course is that they must pick one and run with it.

  10. #10
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: GLabels - escape characters and pause

    Quote Originally Posted by pbrockway2 View Post
    No, they're not. (That was why I asked my question above).

    I had a look at the documentation at the time (I think from Stanford) and it looks like you *can't* have embedded newlines. Basically the GLabel just uses a graphics context drawString() method on the text content. That's from the source code rather than the - silent on this point - documentation.



    The classes are part of the "ACM Java Task Force" library. Problems concerning this code occur from time to time on forums like this. I've seen problems arise from the fact that the code is a bit shoddy in places (counterintuitive, lacking error checking, incompletely documented) but also the mere fact that it is nonstandard means that it can be difficult to get help on online fora. My advice - for *students* - would be to try and find some other learning source. There are online tutorials, ebooks etc.

    For *teachers* the problem is a bit more subtle. Clearly, hard work was done by clever people to write the library. There is a rationale for it at the JTF home page. I would recommend a teacher read that information (I haven't done so) and decide for themselves if - and how - they might use the ideas within their course. Sorry for the nonanswer!, but I really don't think there is One Right Way.

    -----

    An aside, but, yesterday, I did see a discussion somewhere in that documentation, of the question of how to handle animation which you raised earlier in this thread. Basically the authors point out that there are *several* approaches which they describe. The problem for the teacher of an *introductory* course is that they must pick one and run with it.
    Actually, thanks for clarifying. I'll start learning the java source objects and classes, and hopefully I wont have any more problems like these.

Similar Threads

  1. Replies: 5
    Last Post: 10-09-2011, 08:59 PM
  2. Escape Characters in xml
    By higashij in forum XML
    Replies: 2
    Last Post: 09-23-2010, 08:58 AM
  3. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 06:09 AM
  4. Need help with escape characters
    By jayjones149 in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 08:10 AM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 08:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •