2

I can't make the string show up when the "Enter message" button is clicked, and I can't center the box for some reason. When I click the Draw square it will draw the square and if I click "Draw it in color" it will draw it in color. However no matter what I do, the text will not appear. Here's my code:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;


public class Lab9 extends JApplet implements ActionListener, ItemListener {

 private JRadioButton square;
 private JRadioButton message;
 private JTextField messageField;
 private String messageEntered = "";
 private JComboBox location;
 private JPanel top;
 private JPanel bottom;
 private JCheckBox drawColor;
 private JButton draw;
 private int newWidth;
 private int newHeight;
 private static final String locations[] = {"Centered", "Random location"};
 private boolean drawSquare;
 private Color color;

@Override
public void init() {
    setLayout(new FlowLayout());
    top = new JPanel();
    bottom = new JPanel();

    square = new JRadioButton("Draw square");
    square.addItemListener(this);
    top.add(square);

    message = new JRadioButton("Enter your message:");
    message.addItemListener(this);
    top.add(message);

    messageField = new JTextField(20);
    messageField.addActionListener(this);
    top.add(messageField);

    //bottom panel
    bottom.add(new JLabel("Select where to draw:"));
    location = new JComboBox(locations);
    bottom.add(location);

    drawColor = new JCheckBox("Draw in color");
    drawColor.addItemListener(this);
    bottom.add(drawColor);

    draw = new JButton("Draw it!");
    draw.addActionListener(this);
    bottom.add(draw);

    add(top);
    add(bottom);

}

 @Override 
 public void paint(Graphics g) {
     super.paint(g); 

     g.setColor(color);

     if(drawSquare){

         g.fillRect(newWidth, newHeight, 200, 200);
     }

     else {
         g.setColor(color);
         g.drawString(messageEntered, newWidth, newHeight); // print out entered name 
     }
 }

@Override
public void actionPerformed(ActionEvent e) {

    repaint();
}

@Override
public void itemStateChanged(ItemEvent e) {
    int width = getWidth();
    int height = getHeight();
    Random rand = new Random();

    if(e.getSource() == location && location.getSelectedIndex() == 1) {

         newWidth = rand.nextInt(width) + 20;
         newHeight = rand.nextInt(height) + 20;
    }

    if (e.getSource() == location && location.getSelectedIndex() == 0) {
        newWidth = width/2-100;
        newHeight = height/2-100;
    }

    if(e.getSource() == square && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = true;
    }

    if(e.getSource() == message && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = false;
        messageEntered = messageField.getText();
    }

    if (e.getSource() == drawColor && e.getStateChange() == ItemEvent.SELECTED) {
        color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    }

    if(e.getSource() == drawColor && e.getStateChange() == ItemEvent.DESELECTED) {
        color = Color.BLACK;
    }


}

Suggestions?

  • 1
    What does "no matter what I do" mean? – asteri Oct 19 '12 at 16:44
  • 1
    Don't paint directly in the JApplet itself. Instead paint in a JPanel that is the applet's contentPane and use the `paintComponent(...)` method not the `paint(...)` method. Please read the painting with Swing tutorials for more on this. – Hovercraft Full Of Eels Oct 19 '12 at 16:47
  • No matter what options I select. –  Oct 19 '12 at 16:50
  • The `drawString()` method requires _baseline_ relative coordinates, as shown [here](http://stackoverflow.com/a/2658663/230513). – trashgod Oct 19 '12 at 17:14

1 Answers1

2

As shown here, the drawString() method requires baseline relative coordinates. This example may be helpful in understanding the geometry.

FontMetrics fm = g.getFontMetrics();
g.drawString(messageEntered, newWidth, newHeight + fm.getAscent());

As an aside, consider overriding paintComponent() in a JPanel and adding that to your applet.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also this [example](http://stackoverflow.com/a/5312702/230513) that centers a string. – trashgod Oct 19 '12 at 17:34
  • I'm not sure how I'd paint in the JPanel. I can't extend the JPanel class because it has to extend the JApplet class. –  Oct 19 '12 at 17:53
  • You add the `JPanel` to the `JApplet`; several examples are cited [here](http://stackoverflow.com/a/12449949/230513). Can you se you message text now? – trashgod Oct 19 '12 at 18:02
  • I think it has to do with the ItemListeners not working correctly. For some reason I can press both radio buttons at the same time and if I click the "Draw Square" button first and then click the "Draw message" button the message appears. –  Oct 19 '12 at 18:22
  • You can select one of the buttons to be the default. – trashgod Oct 19 '12 at 18:25