JTextfields and JButtons and JLabels and paintings in applets
Ok, so I added a textfield, button, and label to an applet. The purpose is to type something in the textfield and click Draw and it will draw the text in the picture. It compiles and displays but all I see is the painting and no textfield or button or label. Any ideas?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import javax.swing.*;
publicclass P1extends JApplet
implements KeyListener, ActionListener{
JButton btnDraw;
JTextField txtName;
JLabel lblName;
int x1=(int)(450*Math.random()), y1=(int)(450*Math.random()), x2=(int)(450*Math.random()), y2=(int)(Math.random()), n=20;
int w=(int)(100*Math.random()), h=(int)(100*Math.random());
int key;
int x3[]=newint[12];
int y3[]=newint[12];
int w3[]=newint[12];
int h3[]=newint[12];
Font f =new Font("Courier",Font.BOLD,100);
Font f2 =new Font("Serif",Font.BOLD,20);
publicvoid init(){
JPanel row1 =new JPanel();
lblName =new JLabel("Enter your name: ");
row1.add(lblName);
txtName =new JTextField(10);
row1.add(txtName);
btnDraw =new JButton("Draw");
btnDraw.addActionListener(this);
row1.add(btnDraw);
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(row1);
setContentPane(content);
setVisible(true);
setBackground(Color.yellow);
addKeyListener(this);
for (int i=0;i<12;i++){
x3[i] = (int)(450*Math.random());
y3[i] = (int)(450*Math.random());
w3[i] = (int)(50*Math.random());
h3[i] = (int)(50*Math.random());
}
}
publicvoid actionPerformed(ActionEvent e){
Graphics ga = this.getGraphics();
ga.setColor(Color.black);
ga.drawString(txtName.getText(),200,200);
ga.dispose();
}
publicvoid paint(Graphics g){
g.setFont(f);
if (((x2-5) < x1 && x1 < (x2+5)) && ((y2-5) < y1 && y1 < (y2+5))){
g.setColor(Color.pink);
g.drawString("YOU WIN!", 10, 80);
}
for (int i3=0;i3<12;i3++){
if (((x3[i3]-20) < x1 && x1 < (x3[i3]+20)) && ((y3[i3]-20) < y1 && y1 < (y3[i3]+20))){
g.setColor(Color.red);
g. drawString("YOU LOSE!", 10, 400);
}
}
g.setColor(Color.blue);
g.fillOval(x1, y1, w, h);
g.setColor(Color.red);
g.drawOval(x2, y2, w, h);
for (int i2=0;i2<12;i2++){
g.drawOval(x3[i2], y3[i2], w3[i2], h3[i2]);
}
}
publicvoid keyTyped(KeyEvent e){}
publicvoid keyPressed(KeyEvent e){
key = e.getKeyCode();
if (key == 37){
x1 -= 5;
}elseif (key == 39){
x1 += 5;
}elseif (key == 38){
y1 -= 5;
}elseif (key == 40){
y1 += 5;
}
repaint();
}
publicvoid keyReleased(KeyEvent e){}
}

