Results 1 to 20 of 30
Thread: Add keys to arraylist
- 07-19-2010, 01:44 PM #1
- 07-19-2010, 01:55 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Use a StringBuilder? And add the typed charaters to the StringBuilder using the append method? IOW, don't use an array at all?
- 07-19-2010, 02:04 PM #3
Ok, thanks.
Now can I do this to make it show on my applet?
This is my drawLogin class:Java Code:drawLogin login = new drawLogin(); sb.append(ke.getKeyChar()); login.username = sb.toString();
Because nothing happens when I try it.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); } }Last edited by PhQ; 07-19-2010 at 02:08 PM.
- 07-19-2010, 02:52 PM #4
How/where is your loginPaint() method called? How Are you sure its being called?nothing happens
What is the value of username when it is called?
- 07-19-2010, 03:03 PM #5
I gets called here:
getPanels class: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); } };
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; } } }
- 07-19-2010, 03:41 PM #6
Ok, next question: How Are you sure its being called?
- 07-19-2010, 03:46 PM #7
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.
- 07-19-2010, 03:49 PM #8
Where does loginPaint() call any method to report it has been called?I get a message saying Loading game panel
NOTE: This code has an empty catch() clause for an exception. POOR Technique. Add a println() to report any errors!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?Last edited by Norm; 07-19-2010 at 03:51 PM.
- 07-19-2010, 03:51 PM #9
username's value changes because I use it in my keylistener method:
and there is no errors the empty catch, just checked it.Java Code:public void keyTyped(KeyEvent ke) { drawLogin login = new drawLogin(); sb.append(ke.getKeyChar()); sM(sb.toString()); sM(login.username); }Last edited by PhQ; 07-19-2010 at 03:54 PM.
- 07-19-2010, 03:53 PM #10
Your assuming that your program is working the way you intend. Obviously it isn't.What is the value of username when it is called?
What is the value of username when loginPaint is called?
- 07-19-2010, 03:55 PM #11
Can I post my whole program here? Because I am so confused ! lol
- 07-19-2010, 03:58 PM #12
Attached it.
- 07-19-2010, 03:58 PM #13
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.
- 07-19-2010, 04:02 PM #14
- 07-19-2010, 04:08 PM #15
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
- 07-19-2010, 04:28 PM #16
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.
- 07-19-2010, 04:40 PM #17
What do you think?Does the method has to be called after the username value is changed
If it is called before the change, then the old value would be there.
- 07-19-2010, 05:05 PM #18
Ok, if you look at this:
When I start the program debuging works fine.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"); } }; }
But when I use this method in:
it never gets called (I just get "Running game panel..")Java Code:public void keyTyped(KeyEvent ke) { drawLogin login = new drawLogin(); login.test = "OMG ROOODKF|km"; runGPanel(); }
Thanks for your help.
- 07-19-2010, 05:19 PM #19
Sound like you need to do more debugging.
What is the "it" you are talking about? Please don't use "it", use the name of the method etcit never gets called
Where in your code is "it" supposed to get called? Why isn't the code that makes the call being executed?
- 07-19-2010, 05:25 PM #20
Similar Threads
-
foreign keys in jtable gui
By blackpearlmoni in forum New To JavaReplies: 0Last Post: 10-31-2009, 04:52 PM -
Getting all the keys for a specific value from a multimap??
By Ms.Ranjan in forum New To JavaReplies: 0Last Post: 05-13-2009, 03:55 PM -
auto generated keys
By abhi_iips in forum New To JavaReplies: 3Last Post: 03-13-2009, 07:14 AM -
Media Keys
By tikigod in forum New To JavaReplies: 0Last Post: 01-28-2009, 11:30 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks