Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Add keys to arraylist

    How can I make it when for example a person types A and the A character goes into an array and then if the person types in Z the Z gets added to array, and then the person clicks go and the person should get a message saying AZ ?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Use a StringBuilder? And add the typed charaters to the StringBuilder using the append method? IOW, don't use an array at all?

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by masijade View Post
    Use a StringBuilder? And add the typed charaters to the StringBuilder using the append method? IOW, don't use an array at all?
    Ok, thanks.
    Now can I do this to make it show on my applet?
    Java Code:
    drawLogin login = new drawLogin();
    		sb.append(ke.getKeyChar());
    		login.username = sb.toString();
    This is my drawLogin class:
    Java Code:
    package org.swc.GamePanels.Login;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontFormatException;
    import java.awt.Graphics;
    import java.io.IOException;
    import java.io.InputStream;
    
    
    
    public class drawLogin {
    	
    	public String username = "";
    	
    	public void loginPaint(Graphics g){
    		try{
    		  InputStream fin = this.getClass().getResourceAsStream("GlueRegular.ttf");
    		  Font asimov = Font.createFont ( // Load font from InputStream fin
    		    Font.PLAIN,
    		    fin
    		  ).deriveFont(60f);
    		g.setColor(Color.LIGHT_GRAY);
    		g.fillRoundRect(183, 100, 400, 300, 20, 20);
    		g.setColor(Color.BLUE);
    		g.setFont(asimov);
    		g.drawString("Login", 345, 160);
    		}catch(Exception e){}
    		g.setColor(Color.BLACK);
    		g.setFont(new Font("Arial", Font.PLAIN, 12));
    		g.drawString("Username :", 200, 220);
    		g.setColor(Color.WHITE);
    		g.fillRect(270, 207, 290, 18);
    		g.setColor(Color.BLACK);
    		g.drawRect(270, 207, 290, 18);
    		g.drawString(username, 273, 221);
    		
    	}
    
    }
    Because nothing happens when I try it.
    Last edited by PhQ; 07-19-2010 at 02:08 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    nothing happens
    How/where is your loginPaint() method called? How Are you sure its being called?
    What is the value of username when it is called?

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    I gets called here:
    Java Code:
    		gamePanel = new JPanel(){
    
    			private static final long serialVersionUID = 1L;
    			
    			public void paint(Graphics g){
    				g.fillRect(0, 0, 765, 503);
    				
    				getPanels panel = new getPanels();
    				panel.setPanels(g);
    			}
    		};
    getPanels class:
    Java Code:
    package org.swc.GamePanels;
    
    import java.awt.Graphics;
    
    import org.swc.GamePanels.Login.drawLogin;
    import org.swc.Methods.Logging;
    
    
    
    public class getPanels extends Logging {
    
    	public boolean loggedIn = false;
    	
    	public enum panels{
    		LOGIN, GAME;
    	}
    	
    	public panels GetPanel(){
    		if(loggedIn == false){
    			return panels.LOGIN;
    		}
    		if(loggedIn == true){
    			return panels.GAME;
    		}
    		return null;
    	}
    	
    	public void setPanels(Graphics g){
    		switch(GetPanel()){
    		case LOGIN:
    			sM("Loading login panel..");
    			drawLogin draw = new drawLogin();
    			draw.loginPaint(g);
    			break;
    		case GAME:
    			sM("Loading game panel..");
    			break;
    		}
    	}
    
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Ok, next question: How Are you sure its being called?

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    I get a message saying Loading game panel. Because my sM method is just
    public void sM(String s)
    System.out.println(s);.
    oh, and my painting comes up.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    I get a message saying Loading game panel
    Where does loginPaint() call any method to report it has been called?

    Java Code:
    public void loginPaint(Graphics g){
    		try{
    		  InputStream fin = this.getClass().getResourceAsStream("GlueRegular.ttf");
    		  Font asimov = Font.createFont ( // Load font from InputStream fin
    		    Font.PLAIN,
    		    fin
    		  ).deriveFont(60f);
    		g.setColor(Color.LIGHT_GRAY);
    		g.fillRoundRect(183, 100, 400, 300, 20, 20);
    		g.setColor(Color.BLUE);
    		g.setFont(asimov);
    		g.drawString("Login", 345, 160);
    		}catch(Exception e){}
    		g.setColor(Color.BLACK);
    		g.setFont(new Font("Arial", Font.PLAIN, 12));
    		g.drawString("Username :", 200, 220);
    		g.setColor(Color.WHITE);
    		g.fillRect(270, 207, 290, 18);
    		g.setColor(Color.BLACK);
    		g.drawRect(270, 207, 290, 18);
    		g.drawString(username, 273, 221);
    		
    	}
    NOTE: This code has an empty catch() clause for an exception. POOR Technique. Add a println() to report any errors!


    And the next question: What is the value of username when it is called?
    Last edited by Norm; 07-19-2010 at 03:51 PM.

  9. #9
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Where does loginPaint() call any method to report it has been called?

    Java Code:
    public void loginPaint(Graphics g){
    		try{
    		  InputStream fin = this.getClass().getResourceAsStream("GlueRegular.ttf");
    		  Font asimov = Font.createFont ( // Load font from InputStream fin
    		    Font.PLAIN,
    		    fin
    		  ).deriveFont(60f);
    		g.setColor(Color.LIGHT_GRAY);
    		g.fillRoundRect(183, 100, 400, 300, 20, 20);
    		g.setColor(Color.BLUE);
    		g.setFont(asimov);
    		g.drawString("Login", 345, 160);
    		}catch(Exception e){}
    		g.setColor(Color.BLACK);
    		g.setFont(new Font("Arial", Font.PLAIN, 12));
    		g.drawString("Username :", 200, 220);
    		g.setColor(Color.WHITE);
    		g.fillRect(270, 207, 290, 18);
    		g.setColor(Color.BLACK);
    		g.drawRect(270, 207, 290, 18);
    		g.drawString(username, 273, 221);
    		
    	}


    And the next question: What is the value of username when it is called?
    username's value changes because I use it in my keylistener method:
    Java Code:
    public void keyTyped(KeyEvent ke) {
    		drawLogin login = new drawLogin();
    		sb.append(ke.getKeyChar());
    		sM(sb.toString());
    		sM(login.username);
    	}
    and there is no errors the empty catch, just checked it.
    Last edited by PhQ; 07-19-2010 at 03:54 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    What is the value of username when it is called?
    Your assuming that your program is working the way you intend. Obviously it isn't.

    What is the value of username when loginPaint is called?

  11. #11
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Can I post my whole program here? Because I am so confused ! lol

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Attached it.
    Attached Files Attached Files

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Try debugging your code by adding println() statements in the constructors for the sub classes you are using.
    Also add a println() statement to the loginPaint() method to show the value of username when it is called.
    The output from these should show you some problems.

  14. #14
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Try debugging your code by adding println() statements in the constructors for the sub classes you are using.
    Also add a println() statement to the loginPaint() method to show the value of username when it is called.
    The output from these should show you some problems.
    Ok, maybe I need to refresh the panel so it can show the changes?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    You need to consider how to debug your code. Using println()s is very useful.

    You have several classes and methods that are called. Are you sure that they are the ones you are calling or could there be extra ones floating around? To test for that case, add println("constructor for xxx") in all the constructors.
    To be sure that the draw method has the correct String to draw, do a println("username=" + username +"<"); just before calling draw

  16. #16
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    EDIT: Does the method has to be called after the username value is changed?
    Last edited by PhQ; 07-19-2010 at 04:36 PM.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Does the method has to be called after the username value is changed
    What do you think?
    If it is called before the change, then the old value would be there.

  18. #18
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Ok, if you look at this:
    Java Code:
    	public void runGPanel(){
    		sM("Running game panel..");
    		gamePanel = new JPanel(){
    			
    			private static final long serialVersionUID = 1L;
    			
    			public void paint(Graphics g){
    				sM("1");
    				g.fillRect(0, 0, 765, 503);
    				sM("2");
    				getPanels panel = new getPanels();
    				sM("3");
    				panel.setPanels(g);
    				sM("4");
    			}
    		};
    	}
    When I start the program debuging works fine.

    But when I use this method in:

    Java Code:
    	public void keyTyped(KeyEvent ke) {
    		drawLogin login = new drawLogin();
    		login.test = "OMG ROOODKF|km";
    		runGPanel();
    	}
    it never gets called (I just get "Running game panel..")

    Thanks for your help.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Sound like you need to do more debugging.
    it never gets called
    What is the "it" you are talking about? Please don't use "it", use the name of the method etc
    Where in your code is "it" supposed to get called? Why isn't the code that makes the call being executed?

  20. #20
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

Page 1 of 2 12 LastLast

Similar Threads

  1. foreign keys in jtable gui
    By blackpearlmoni in forum New To Java
    Replies: 0
    Last Post: 10-31-2009, 04:52 PM
  2. Replies: 0
    Last Post: 05-13-2009, 03:55 PM
  3. auto generated keys
    By abhi_iips in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 07:14 AM
  4. Media Keys
    By tikigod in forum New To Java
    Replies: 0
    Last Post: 01-28-2009, 11:30 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

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