3

The idea of the program is that I have some buttons and an icon SOMEWHERE on the frame. I want the buttons to change the color. I'm only worried about making all the elements show up right now. If I comment out lines 11-13, I see "hello," printed out, with a red circle on top of it. Otherwise, I just have the button "red" without "hello" or my red circle. So here's my code:

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

public class ButtonTester 

{
    public static void main (String[] args) 
    {  
        JFrame frame = new ButtonFrame(); 
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton redButton = new JButton("Red");
        frame.add(redButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true); 
    }
}

class ButtonFrame extends JFrame 
{  
    public static final int DEFAULT_WIDTH = 300;  
    public static final int DEFAULT_HEIGHT = 200;  

    public ButtonFrame() 
    {   
        setTitle("Hello"); 
       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
       ButtonPanel panel = new ButtonPanel(); 
       add(panel); 
    } 
} 

class ButtonPanel extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    {   
        super.paintComponent(g); 
        Graphics2D g2 = (Graphics2D) g;  
        g2.drawString("Hello !", 100, 100);
        Icon ico = new ColorIcon(32);
        ico.paintIcon(null, g, 75, 75);
    } 
} 

I'm 90% sure the problem is lines 11-13, but I'm not sure what to change to make everything visible.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
GrinReaper
  • 93
  • 2
  • 9
  • um... which lines are 11-13? Please understand that we have no idea about where your code begins, including package statements etc. – Hovercraft Full Of Eels Mar 03 '13 at 00:34
  • Oops, sorry. I didn't realize that formatted code doesn't show line numbers. Basically, I was referring to the lines containing frame.setLayout... until frame.add(redButton); – GrinReaper Mar 03 '13 at 01:00

1 Answers1

4

Your problem is that your ButtonPanel's size is 0. Have it override getPreferredSize() and you will see what I mean:

class ButtonPanel extends JPanel {
   private static final int PREF_W = 150;
   private static final int PREF_H = PREF_W;

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.drawString("Hello !", 100, 100);
      // !! Icon ico = new ColorIcon(32);
      // Icon ico = new ImageIcon();
      // ico.paintIcon(null, g, 75, 75);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}

Also as an unrelated aside, why are you creating an Icon inside of the paintComponent method? This doesn't make sense to me and would only serve to needlessly slow your graphics down.

Edit
You state:

Ok, I see the difference after overriding getPreferredSize() But what would be the "better" or "correct" way to create the icon? I'm just trying to follow the directions for an exercise out of a Java textbook: Exercise 4.14. Write a program that shows a frame with three buttons labeled "Red", "Green", and "Blue", and a label containing an icon showing a circle that is initially red. As the user clicks the buttons, the fill color of the circle should change. When you change the color, you need to invoke the repaint method on the label. The call to repaint ensures that the paintIcon method is called so that the icon can be repainted with the new color.

You need to think on this a different way. Myself I'd create three ImageIcons one for a blue circle, one for red, and one for green. I'd then display the ImageIcon in a JLabel on my JFrame. I'd change the color by simply swapping the label's icons via its setIcon(...) method. I wouldn't worry about futzing with paintComponent(...) but rather would try to solve this in as simple a fashion as possible.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Ok, I see the difference after overriding getPreferredSize() But what would be the "better" or "correct" way to create the icon? I'm just trying to follow the directions for an exercise out of a Java textbook: Exercise 4.14. Write a program that shows a frame with three buttons labeled "Red", "Green", and "Blue", and a label containing an icon showing a circle that is initially red. As the user clicks the buttons, the fill color of the circle should change. When you change the color, you need to invoke the repaint method on the label. The call to repaint ensures that the paintIcon – GrinReaper Mar 03 '13 at 01:12
  • ..method is called so that the icon can be repainted with the new color. – GrinReaper Mar 03 '13 at 01:14
  • Basically, the idea is to make three separate icon objects that are already colored, and just swap them as needed? I think I'll have to switch from JPanel to JLabel, it looks like it's what the author of the problem wanted... – GrinReaper Mar 03 '13 at 01:21
  • 2
    @GrinReaper: **EXACTLY**. The goal is to follow the **KISS** rule: "keep it simple stupid". – Hovercraft Full Of Eels Mar 03 '13 at 01:22