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){}

}

[6362 byte] By [dswetta] at [2007-11-26 12:19:53]
# 1

1) Swing related questions should be posted in the Swing forum.

2) As a general rule you never need to override paint(). (You did it incorrectly here because you didn't use super.paint() which is causing your problem). So fixing this problem, it should work, but in not the correct design.

3) to do custom painting extend JComponent or JPanel and override the paintCompoenent(..) method. Then you add the component to the content pane just like any other component. Search the Swing forum for my "FaceComponent" example.

> int x1=(int)(450*Math.random()), y1=(int)(450*Math.random()), x2=(int)(450*Math.random()), y2=(int)(Math.random()), n=20;

Code each statement on a separate line. Its easier to read and maintain.

camickra at 2007-7-7 15:09:13 > top of Java-index,Archived Forums,Socket Programming...
# 2

Ok, so I will create a component using the code below, but where in my code do I put this block?

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container content = getContentPane();

JPanel jp = new JPanel() {

public void paintComponent(Graphics g) {

g.setColor(Color.white);

g.fillRect(0,0,getWidth(),getHeight());

g.setColor(Color.black);

g.drawArc(0,0,getWidth(),getHeight(),0,360);

}

};

content.add(jp, BorderLayout.CENTER);

setSize(300, 300);

setVisible(true);

dswetta at 2007-7-7 15:09:13 > top of Java-index,Archived Forums,Socket Programming...
# 3
tutorial or demo or sample or anything?
dswetta at 2007-7-7 15:09:13 > top of Java-index,Archived Forums,Socket Programming...
# 4
> tutorial or demo or sample or anything? [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Manager[/url]. You use a layout manager to display the components the way you want then to be displayed.
camickra at 2007-7-7 15:09:13 > top of Java-index,Archived Forums,Socket Programming...