Re: Erasing a String produced using drawString()
On Feb 23, 5:07 am, Nigel Wade <n...@ion.le.ac.uk> wrote:
BlackJackal wrote:
This is what I have so far
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JBlueGray extends JApplet implements ActionListener
{
String fname = "Robert";
String lname = "McDougal";
JButton pushme = new JButton("Click Me!");
Font littlefont = new Font("Helvetica", Font.PLAIN, 12);
Font bigfont = new Font("Helvetica", Font.PLAIN, 22);
int x = 20, y = 50, width, count = 0, height;
public void init()
{
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(pushme);
pushme.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == pushme) {
Graphics gr = getGraphics();
if (count == 1) {
gr.setFont(littlefont);
gr.setColor(Color.BLUE);
width = gr.getFontMetrics().stringWidth(fname);
height = gr.getFontMetrics().getHeight();
gr.drawString(fname, x, y);
gr.drawString(lname, x + (2 + width), y);
}
else if(count == 2) {
width += gr.getFontMetrics().stringWidth(lname);
gr.setFont(bigfont);
gr.setColor(Color.DARK_GRAY);
gr.fillRect(x, y, width + 4, height);
gr.drawString(fname, x, y);
gr.setFont(littlefont);
gr.setColor(Color.BLUE);
gr.drawString(lname,
gr.getFontMetrics().stringWidth(fname) + 2, y);
pushme.setEnabled(false);
}
}
}
}
Does the homework specify that you use Graphics drawing methods?
Surely it would be much simpler to use JComponents, and alter their visibility
and attributes.
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : n...@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555- Hide quoted text -
- Show quoted text -
The chapter that we just read is concerning JApplets and the Paint()
method. The last chapter we learned about init() Start() Stop() and
Destroy() along with JLabels JButtons, etc. The instructions for the
exact problem are as follows:
Write a JApplet that displays your name in blue the first time the
user clicks a JButton. The second time the user clicks the JButton,
make the first name seem to disappear (Hint: Redraw it using the
background color) At the same time, draw your first name again in a
alarger font in dark gray. Save the JApplet as JBlueGray.
The hint seemed to tell me to use clearRect() method used earlier in
the chapter but it didn't seem to do the trick. Below is a list of
the methods that I have learned in the last two chapters.
Chapter 9
------------------------------------------------------
Container
init()
JLabel
add()
getContentPane()
Font class
setFont()
JTextField
setText()
getText()
requestFocus()
setEditable()
JButton
setLabel()
addActionListener()
actionPerformed(ActionEvent e)
validate()
remove()
start()
stop()
destroy()
setLocation() - even though i can't seem to get this to work
setEnabled()
Chapter 10
---------------------------------------------------------
paint()
repaint()
drawString()
setFont()
drawLine()
drawRect()
fillRect()
clearRect()
drawRoundRect()
drawOval()
fillOval()
drawArc()
fillArc()
draw3DRect()
fill3DRect()
drawPolygon()
fillPolygon()
addPoint()
copyArea()
getAllFonts()
getDefaultToolkit()
getScreenResolution()
getScreenSize()
getFontMetrics()
stringWidth()