G
Gary Conte
For school I need to create a an applet that when I click on a button once,
it will show text. When I click on it a second time, it should clear text
and repaint the screen with the new text. I have tried doing this with the
paint/repaint method, but the only time the text repaints the screen is when
I maximize or minimize the screen.
Can you please tell me what I am doing wrong.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCreateGraphicsObject extends JApplet implements ActionListener
{
String myName = new String ("Carla");
JButton clickButton = new JButton("Click It");
Font smallFont = new Font("Arial", Font.BOLD, 12);
Font largeFont = new Font("Arial", Font.BOLD, 24);
int x =150, y =150;
public void init()
{
setBackground(Color.black);
Container con = getContentPane();
con.setLayout(new FlowLayout() );
con.add(clickButton);
clickButton.addActionListener(this);
}
public void paint(Graphics g)
{
clickButton.repaint();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
Graphics pen = getGraphics();
if (source == clickButton)
{
clickButton.repaint();
pen.setFont(smallFont);
pen.setColor(Color.blue);
}
if (x <151)
{
pen.drawString(myName, x+=20, y+=20);
}
else
{
clickButton.setEnabled(false);
pen.setColor(Color.gray);
pen.setFont(largeFont);
pen.drawString(myName, x+=0, y+=0);
}
}
}
it will show text. When I click on it a second time, it should clear text
and repaint the screen with the new text. I have tried doing this with the
paint/repaint method, but the only time the text repaints the screen is when
I maximize or minimize the screen.
Can you please tell me what I am doing wrong.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCreateGraphicsObject extends JApplet implements ActionListener
{
String myName = new String ("Carla");
JButton clickButton = new JButton("Click It");
Font smallFont = new Font("Arial", Font.BOLD, 12);
Font largeFont = new Font("Arial", Font.BOLD, 24);
int x =150, y =150;
public void init()
{
setBackground(Color.black);
Container con = getContentPane();
con.setLayout(new FlowLayout() );
con.add(clickButton);
clickButton.addActionListener(this);
}
public void paint(Graphics g)
{
clickButton.repaint();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
Graphics pen = getGraphics();
if (source == clickButton)
{
clickButton.repaint();
pen.setFont(smallFont);
pen.setColor(Color.blue);
}
if (x <151)
{
pen.drawString(myName, x+=20, y+=20);
}
else
{
clickButton.setEnabled(false);
pen.setColor(Color.gray);
pen.setFont(largeFont);
pen.drawString(myName, x+=0, y+=0);
}
}
}