tags:

views:

51

answers:

2

I have a buttonlistener that removes the TextFields and the StartButton when it is clicked, but the last part of the code that tells it to run a method that is supposed to display the icon is screwing up only at the end, but is still deleting the TextFields and the JButton. Please help.

public class TypeInNames extends JApplet{

  JButton StartButton;
  JTextField name1, name2;
  String player1, player2;
  String reply;
  boolean test = false;
  ImageIcon myIcon;

  Container cp = getContentPane();

     public void init() 
     {
         setSize(350, 400);
         setLayout(null);

         cp.setBackground(Color.black);

         StartButton = new JButton("Start Game!");
         name1 = new JTextField("Player 1",35);
         name2 = new JTextField("Player 2",35);
         //(x, y, width, height);
         StartButton.setBounds(115,200,120,30);
         name1.setBounds(115,140,120,20);
         name2.setBounds(115,170,120,20);

         startGame();
     }

     public void startGame()
     {
         add(StartButton);
         add(name1);
         add(name2);

         StartButton.addActionListener(new ButtonListener());
     }

     public void game()
     {

     }

     public void endGame()
     {
         myIcon = new ImageIcon("portal-cake.jpg");
         test = true;
         repaint();
     }

     public void paintComponent(Graphics g) {
         super.paint(g);
         if(test)
             myIcon.paintIcon(this, g, 0, 0);
     }

     private class ButtonListener implements ActionListener{

         public void actionPerformed(ActionEvent event)
         {
             if (event.getSource() == StartButton)
             {
                 player1 = name1.getText();
                 player2 = name2.getText();
                 remove(StartButton);
                 remove(name1);
                 remove(name2);

                 endGame();
             }
         }

     }



 }
+1  A: 

Are you sure you meant to override paintComponenet and not paint? Considering you're calling super.paint(g), I'd look there first.

zigdon
Good catch on the errant super method, but "Swing programs should override paintComponent() instead of overriding paint()."—http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks
trashgod
+2  A: 

You don't have to override paintComponent() at all. Just use a JLabel and set the layout.

public void endGame() {
    myIcon = new ImageIcon("portal-cake.jpg");
    JLabel label = new JLabel(myIcon);
    this.setLayout(new GridLayout());
    this.add(label);
    this.validate();
}

Addendum: Here's an alternate approach to collecting startup information.

import java.awt.*;
import javax.swing.*;

public class TypeInNames extends JApplet {

    JTextField name1 = new JTextField("Player 1", 35);
    JTextField name2 = new JTextField("Player 2", 35);

    @Override
    public void init() {
        this.getContentPane().setBackground(Color.black);
        Icon myIcon = new ImageIcon("portal-cake.jpg");
        JLabel label = new JLabel(myIcon);
        this.add(label);
        startGame();
    }

    private void startGame() {
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("Player 1:"));
        panel.add(name1);
        panel.add(new JLabel("Player 2:"));
        panel.add(name2);
        int result = JOptionPane.showConfirmDialog(
            this, panel, "Click OK to Start",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println("Selected:"
                + " " + name1.getText()
                + " " + name2.getText());
        } else {
            System.out.println("Cancelled");
        }
    }
}
trashgod
thanks. the image still isn't showing up, though. do you think it might have something to do with having a solid black background in my applet?
yodasoja
@yodasoja: I don't think so, unless you're still calling `paint()` in `paintComponent()`. You should delete your `paintComponent()` method. If you can't delete it entirely, switch to `super.paintComponent(g)`.
trashgod
Oh, okay! I finally got it to work, thank you very much mr.trashgod
yodasoja