Click to See Complete Forum and Search --> : Applet Not Clearing


Cheater
04-12-2007, 06:48 PM
I'm trying to learn how to program in java. I made an applet, and was told by someone to change all my menus and everything to J_____ (JMenu, JApplet, etc). After I made the change, my applet started acting differently. Now, when the redraw method is called, it draws any changes right over what was already being displayed instead of clearing and then drawing. I think this might have to do with making the switch to swing like I was told. I know nothing about the difference between swing and without it. Any help would be appreciated. Here is my script:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ColoredHelloWorldApplet2
extends JApplet implements ActionListener, KeyListener, MouseListener, FocusListener {
int colorNum;
int bgColor;
int msgNum;
int txtNum;
String msg;
String txt;
Font textFont;
Panel buttonPanel;
TextArea ta;
public void init() {
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenuBar mbar = new JMenuBar();
JMenuItem fMenuNew = new JMenuItem("New");
fileMenu.add(fMenuNew);
fMenuNew.addActionListener(this);
JMenuItem fMenuExit = new JMenuItem("Exit");
fileMenu.add(fMenuExit);
fMenuExit.addActionListener(this);
mbar.add(fileMenu);
setJMenuBar(mbar);

Button bttn = new Button("Change Color");
Button bttn2 = new Button("Change BG");
Button bttn3 = new Button("Change MSG");
Button bttn4 = new Button("Change TXT");
ta = new TextArea("",5,35,TextArea.SCROLLBARS_NONE);
setLayout(new BorderLayout());
buttonPanel = new Panel();
add("North", buttonPanel);
addKeyListener(this);
addFocusListener(this);
addMouseListener(this);
bgColor = 1;
//setBackground(Color.lightGray);
colorNum = 1;
msgNum = 1;
txtNum = 1;
textFont = new Font("Serif",Font.BOLD,24);

bttn.addActionListener(this);
buttonPanel.add(bttn);

bttn2.addActionListener(this);
buttonPanel.add(bttn2);

bttn3.addActionListener(this);
buttonPanel.add(bttn3);

bttn4.addActionListener(this);
buttonPanel.add(bttn4);

buttonPanel.add(ta);
}
public void paint(Graphics g) {
switch (colorNum) {
case 1:
g.setColor(Color.red);
break;
case 2:
g.setColor(Color.blue);
break;
case 3:
g.setColor(Color.green);
break;
case 4:
g.setColor(Color.black);
break;
case 5:
g.setColor(Color.white);
break;
}
switch (bgColor) {
case 1:
setBackground(Color.blue);
break;
case 2:
setBackground(Color.green);
break;
case 3:
setBackground(Color.black);
break;
case 4:
setBackground(Color.white);
break;
case 5:
setBackground(Color.red);
break;
}
switch (msgNum) {
case 1:
msg = "Hello World!";
break;
case 2:
msg = "Hello Cheater!";
break;
case 3:
msg = "Hello Me!";
break;
case 4:
msg = "I Rule!";
break;
case 5:
msg = "You Rule!";
break;
}
switch (txtNum) {
case 1:
txt = "Do you want to hear a joke?";
break;
case 2:
txt = "Why did the chicken cross the road?";
break;
case 3:
txt = "To get to the other side!";
break;
case 4:
txt = "Wasn't that funny?";
break;
case 5:
txt = "I know you thought it was funny.";
break;
}
g.setFont(textFont);
g.drawString(msg, 20,150);
ta.setText(txt);
}
public void focusGained(FocusEvent evt) {

}
public void focusLost(FocusEvent evt) {

}
public void actionPerformed(ActionEvent evt) { //Button Pressed
String command = evt.getActionCommand();
if (command.equals("Change Color")) {
if (colorNum == 1)
colorNum = 2;
else if (colorNum == 2)
colorNum = 3;
else if (colorNum == 3)
colorNum = 4;
else if (colorNum == 4)
colorNum = 5;
else
colorNum = 1;
repaint();
}
else if (command.equals("Change BG")) {
if (bgColor == 1)
bgColor = 2;
else if (bgColor == 2)
bgColor = 3;
else if (bgColor == 3)
bgColor = 4;
else if (bgColor == 4)
bgColor = 5;
else
bgColor = 1;
repaint();
}
else if (command.equals("Change MSG")) {
if (msgNum == 1)
msgNum = 2;
else if (msgNum == 2)
msgNum = 3;
else if (msgNum == 3)
msgNum = 4;
else if (msgNum == 4)
msgNum = 5;
else
msgNum = 1;
repaint();
}
else if (command.equals("Change TXT")) {
if (txtNum == 1)
txtNum = 2;
else if (txtNum == 2)
txtNum = 3;
else if (txtNum == 3)
txtNum = 4;
else if (txtNum == 4)
txtNum = 5;
else
txtNum = 1;
repaint();
}
}
public void keyPressed(KeyEvent evt) {

}
public void keyReleased(KeyEvent evt) {

}
public void keyTyped(KeyEvent evt) {
char ch = evt.getKeyChar();
if ((ch == 'b') || (ch == 'B')) {
if (bgColor == 1)
bgColor = 2;
else if (bgColor == 2)
bgColor = 3;
else if (bgColor == 3)
bgColor = 4;
else if (bgColor == 4)
bgColor = 5;
else
bgColor = 1;
repaint();
}
else if ((ch == 't') || (ch == 'T')) {
if (colorNum == 1)
colorNum = 2;
else if (colorNum == 2)
colorNum = 3;
else if (colorNum == 3)
colorNum = 4;
else if (colorNum == 4)
colorNum = 5;
else
colorNum = 1;
repaint();
}
else if ((ch == 'm') || (ch == 'M')) {
if (msgNum == 1)
msgNum = 2;
else if (msgNum == 2)
msgNum = 3;
else if (msgNum == 3)
msgNum = 4;
else if (msgNum == 4)
msgNum = 5;
else
msgNum = 1;
repaint();
}
else if ((ch == 'x') || (ch == 'X')) {
if (txtNum == 1)
txtNum = 2;
else if (txtNum == 2)
txtNum = 3;
else if (txtNum == 3)
txtNum = 4;
else if (txtNum == 4)
txtNum = 5;
else
txtNum = 1;
repaint();
}
}
public void mousePressed(MouseEvent evt) {
requestFocus();
}
public void mouseEntered(MouseEvent evt) {

}
public void mouseExited(MouseEvent evt) {

}
public void mouseReleased(MouseEvent evt) {

}
public void mouseClicked(MouseEvent evt) {

}
}

agent_x91
04-18-2007, 08:48 AM
I don't really have time to go through all the code and figure what's wrong, try to find the lines indicated by any errors you're having and and show us those lines specifically. If no actual exceptions are being thrown, just give us the relevant draw methods which aren't doing what you want them to.

By the way, I hope your programming skills are better than your sense of humour ;)



switch (txtNum) {
case 1:
txt = "Do you want to hear a joke?";
break;
case 2:
txt = "Why did the chicken cross the road?";
break;
case 3:
txt = "To get to the other side!";
break;
case 4:
txt = "Wasn't that funny?";
break;
case 5:
txt = "I know you thought it was funny.";
break;
}

Cheater
04-18-2007, 09:42 PM
There are no errors. The script compiles fine. The problem is that it draws the message to the screen. At this point, it is meant to clear the screen and print a new message. However, it is only printing the new message right over the old one (the two messages overlap). Yeah, I know the joke is lame.