components not intially displayed
This is probably a beginnerish problem but please bear with me.
I've recently tried to convert my awt program to swing and the only problem I'm getting is that the components only appear on the applet when i mouse over them (such as a button or a text area). the awt components did not used to do this and I'm not quite sure why it does do this. I've created a container in the the main public void init, setLayout to null, and added all the components.
Also on a different note, what exactly does thread-safe mean, I've seen it in a variety of places but am not quite sure how it works or if I should worry about it.
Any help is appreciated.
[674 byte] By [
tw11a] at [2007-11-27 5:50:42]

# 5
the program is on a JApplet because i need to draw several lines and grids, it also used Graphics2D which = (Graphics2D)g; Here is the init method and the paint method.
public void init()
{
setLayout(null);
Font myFont = new Font("Monospaced", Font.PLAIN, 11);
crashtab="\t";
a[0]=390;a[1]=510;
a[8]=390;a[9]=530;
x=0;wait=0;kx=0; ky=0;
makeBorder();
start = new Line2D.Double(390,490,390,550);
pass = new Line2D.Double(395,50,395,110);
move = new Button("Move");
rp = new Button("Repaint");
ymove = new TextField("",100);
xmove = new TextField("",100);
errormsgl = new Label("");
whoturn=new Label("Next move: Blue ");
rp.setBounds(0,0,50,25);
move.setBounds(140,250,200,50);
xmove.setBounds(140,300,100,20);
xmove.addActionListener(this);
ymove.setBounds(240,300,100,20);
ymove.addActionListener(this);
errormsgl.setBounds(140,225,300,25);
whoturn.setBounds(140,200,300,25);
add(move);
add(whoturn);
add(rp);
add(xmove);
add(ymove);
add(errormsgl);
xmove.requestFocus();
lom = new TextArea("List of Vectors\n\nBlue\t\tMagenta\n-\t\t-\n(0,0)\t\t(0,0)\n",0,0,1);
lom.setBounds(455,150,220,320);
lom.setEditable(false);
lom.setFont(myFont);
add(lom);
ins = new JTextArea("Please enter the x coordinate in the first box and the y coordinate in the second box.\nWhen entering in coordinates, note that positive means to the right and up while negative means down and to the left. After entering the coordinates, press \"Move\" or Enter.\n\nWhen you crash, the black dot is where you will begin on your turn.");
ins.setBounds(140,330,280,147);
ins.setLineWrap(true);
ins.setWrapStyleWord(true);
ins.setEditable(false);
add(ins);
move.addActionListener(this);
rp.addActionListener(this);
starter = true;
playgame=false;
crash=false;
apass=false;
bpass=false;
gameover=false;
}
public void paint(Graphics g)
{
g2 = (Graphics2D) g;
if(starter)
{
printMain();
starter = false;
}
else if(playgame)
{
g2.setStroke(new BasicStroke(3));
Line2D.Double move = new Line2D.Double(a[0+x], a[1+x], a[2+x], a[3+x]);
Point2D.Double cpt = didCrash(move);
if (cpt!=null)
{
if(crash) wait++;
crash=true;
appendLom();
double crashx = cpt.getX(), crashy = cpt.getY();
move.setLine(a[x], a[x+1], crashx, crashy);
Ellipse2D.Double boom = new Ellipse2D.Double(crashx-5,crashy-5,10,10);
a[2+x] = ((int)crashx/10)*10+kx;
a[3+x] = ((int)crashy/10)*10+ky;
g2.setColor(Color.red);
a[4+x]=0;a[5+x]=0;
g2.fill(boom);
errormsgl.setText("You crashed!");
g2.setColor(Color.black);
g2.fill(new Ellipse2D.Double(a[x+2]-3,a[x+3]-3,6,6));
kx=0;ky=0;
}
else appendLom();
if(x==0) g2.setColor(Color.blue);
else g2.setColor(Color.magenta);
g2.draw(move);
a[x] = a[2+x];
a[1+x] = a[3+x];
if(crash&&wait==1)
{
x=Math.abs(x-8);
crash=false;
appendLom();
wait=0;
}
if(crash) wait++;
x=Math.abs(x-8);
if(x==0) whoturn.setText("Next move: Blue");
else whoturn.setText("Next move: Magenta");
playgame=false;
}
}
basically the ins jtextarea does not display unless i hightlight the text, i also wanted to make lom a Jtextarea but had the same problems
Message was edited by:
tw11
tw11a at 2007-7-12 15:38:37 >
