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 ?
Printable View
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 ?
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?
This is my drawLogin class:Code:drawLogin login = new drawLogin();
sb.append(ke.getKeyChar());
login.username = sb.toString();
Because nothing happens when I try it.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);
}
}
How/where is your loginPaint() method called? How Are you sure its being called?Quote:
nothing happens
What is the value of username when it is called?
I gets called here:
getPanels class: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);
}
};
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;
}
}
}
Ok, next question: How Are you sure its being called?
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.
Where does loginPaint() call any method to report it has been called?Quote:
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!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:
and there is no errors the empty catch, just checked it.Code:public void keyTyped(KeyEvent ke) {
drawLogin login = new drawLogin();
sb.append(ke.getKeyChar());
sM(sb.toString());
sM(login.username);
}
Your assuming that your program is working the way you intend. Obviously it isn't.Quote:
What is the value of username when it is called?
What is the value of username when loginPaint is called?
Can I post my whole program here? Because I am so confused ! lol
Attached it.
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.
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
EDIT: Does the method has to be called after the username value is changed?
What do you think?Quote:
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.
Ok, if you look at this:
When I start the program debuging works fine.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..")Code:public void keyTyped(KeyEvent ke) {
drawLogin login = new drawLogin();
login.test = "OMG ROOODKF|km";
runGPanel();
}
Thanks for your help.
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 etcQuote:
it never gets called
Where in your code is "it" supposed to get called? Why isn't the code that makes the call being executed?
Meh, I give up.