Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JColorChooser

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JColorChooser

    I can't seem to get the circle to change color after the user picks a new color from the JColorChooser. any ideas?

     
    package bouncer;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Bouncer extends JComponent {
     
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 300;
     
        private static Color circleColor = (Color.RED);
     
        public void paintComponent(Graphics g) {
            Graphics g2 = (Graphics) g;
     
            g2.setColor(circleColor);
            g2.fillOval(130, 120, 10, 10);
     
        }
     
        public static void main(String[] args) {
     
            JFrame frame = new JFrame("Bouncing Ball");
     
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
            JButton button = new JButton("Choose Color");
     
            class AddColor implements ActionListener {
                @Override
                public void actionPerformed(ActionEvent event) {
                    circleColor = JColorChooser.showDialog(null, "pick your color", circleColor);
                }
            }
     
            ActionListener listener = new AddColor();
            button.addActionListener(listener);
     
            JPanel panel = new JPanel();
     
            panel.add(button);
     
            frame.add(new Bouncer());
            frame.add(panel, BorderLayout.SOUTH);
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,351
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: JColorChooser

    After you do something that you want to cause the paintComponent() method to be called, call the repaint() method to tell the JVM to call paintComponent()
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    thatguy (February 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JColorChooser

    Thank you for the help. but now i'm confused on why this works. I called repaint which calls the paintComponent method but repaint is in the paintComponent method? so how does that work?

        public void paintComponent(Graphics g) {
            Graphics g2 = (Graphics) g;
     
            g2.setColor(circleColor);
            g2.fillOval(130, 120, 10, 10);
            repaint();
        }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,351
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: JColorChooser

    Calling the repaint() method in the paintComponent() method doesn't make sense. I think it could cause a tight loop. The repaint() method tells the JVM to call the painting methods.
    Call the repaint() method at the time and place that you have changed what is going to be painted. That would be where you have set the new color to be used.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JColorChooser

    I agree and that is what I did at first I put the repaint() right after the color change like this

     
     
            class AddColor implements ActionListener {
                @Override
                public void actionPerformed(ActionEvent event) {
                    circleColor = JColorChooser.showDialog(null, "pick your color", circleColor);
                    repaint();
                }
            }

    but I got an error saying, "non-static method repaint() cannot be referenced from a static context"

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,351
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: JColorChooser

    You need to use a reference to the component class that contains the repaint() method to call it:
    refToClassWithRepaint.repaint();

    The AddColor class does not have a repaint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JColorChooser

    I really appreciate all your help, thank you.

    but now when I choose a color it doesn't change the color of the circle.
     
            class AddColor implements ActionListener {
                @Override
                public void actionPerformed(ActionEvent event) {
                    circleColor = JColorChooser.showDialog(null, "pick your color", circleColor);
                    Bouncer b = new Bouncer();
                    b.repaint();
                }
            }


    --- Update ---

    could this have something to do with it?

     public Bouncer() {
     
            class MouseClickListener implements MouseListener {
                @Override
                public void mouseClicked(MouseEvent e) {
                    float r = (float) Math.random();
                    float g = (float) Math.random();
                    float b = (float) Math.random();
     
                    circleColor = new Color(r, g, b);
                    repaint();
                }
                @Override
                public void mousePressed(MouseEvent e) {}
                @Override
                public void mouseReleased(MouseEvent e) {}
                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {}   
            }
            this.addMouseListener(new MouseClickListener());
        }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,351
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: JColorChooser

    The new object b that is created is not the one being shown. You need to get access to the component that is currently being shown.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    thatguy (February 26th, 2013)

  11. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JColorChooser

    Thanks again for all your help.