paint method usage with

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);
}
}
}
 
K

Kevin Pors

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.

<snip code>

Just a remark:

I think it's better to do all the painting within the paint(Graphics g) method.
 
G

Gary Labowitz

Gary Conte said:
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);
}
}
}
You should do the paiting in the paint method using the Graphics given to
you by the JVM. Just do the repaint in the actionPerformed.
 
G

gerrymcc

Gary Conte said:
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);
}
}
}



I'm not a programmer. But this works; I guess you need to find out how
to translate it into Swing. I'd like to know how to do that too.


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class CreateGraphicsObject extends Applet implements
ActionListener
{
String myName = new String ("Carla");
Button clickButton = new Button("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);
add(clickButton);
clickButton.addActionListener(this);
}

public void paint(Graphics g)
{
g.setColor( Color.black);
g.fillRect(0, 0, 250, 250);

if (x <151){
g.setFont( smallFont);
g.setColor( Color.blue);
g.drawString(myName, x+=20, y+=20);
}
else{
clickButton.setEnabled(false);
g.setColor(Color.gray);
g.setFont(largeFont);
g.drawString(myName, x+=0, y+=0);
}
}

public void actionPerformed(ActionEvent e)
{
repaint();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top