Search code examples
javaswingjpanelpaintcomponent

GUI not drawing on drawpanel?


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener{
    JFrame frame;

    public static void main (String[] args){
        SimpleGui3C gui = new SimpleGui3C();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Change colours");
        button.addActionListener(this);

        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.setSize(300,300);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event ){
        frame.repaint();
    }

}

drawpanel

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

public class MyDrawPanel extends JPanel{
    protected void paintComponenet(Graphics g){
        g.fillRect(0,0,this.getWidth(),this.getHeight())


        Graphics2D g2d = (Graphics2D) g;

        int red = (int) Math.random() * 256;
        int green = (int) Math.random() * 256;
        int blue = (int) Math.random() * 256;
        Color startColour = new Color(red,green,blue);

        red = (int) Math.random() * 256;
        green = (int) Math.random() * 256;
        blue = (int) Math.random() * 256;
        Color endColour = new Color(red,green,blue);


        GradientPaint gradient = new GradientPaint(70,70,startColour,150,150,endColour);
        g2d.setPaint(gradient);
        g2d.fillOval(70,70,100,100);
    }


}

I'm trying to learn about the GUI and I have some code that should draw a randomly coloured circle every time the button is pressed.

But at the moment the drawpanel always stays grey like so https://i.sstatic.net/hq0GN.png

Is there anything obvious that is stopping the code from working like it is intended to?

Now the circle is always black and never goes to a random colour.


Solution

  • You spell paintComponent wrong. Always precede this method with @Override

    So change this:

    public void paintComponenet(Graphics g) {
        g.fillRect(0, 0, this.getWidth(),this.getHeight());
    

    to this:

    @Override    // add so the compiler will warn you if you spell it wrong
    protected void paintComponent(Graphics g) {  // should be protected, not public
        super.paintComponent(g); // so that you erase old images
        g.fillRect(0, 0, this.getWidth(),this.getHeight());
    

    Edit
    You need to do the double math in parenthesis so the right value is cast to int since this:

    red = (int) Math.random() * 256;
    

    is equivalent to this

    red = ((int) (Math.random())) * 256;
    

    which equals this:

    red = 0 * 256;
    

    So instead, do this:

    red = (int) (Math.random() * 256);
    

    But more importantly you need to learn to debug. Print out your color values with printlns or use a debugger to try to isolate your error. Do this first before coming here. For example:

        red = (int) Math.random() * 256;
        green = (int) Math.random() * 256;
        blue = (int) Math.random() * 256;
    
        // this will show you what your values are
        // and key you in to where you should look:
        System.out.printf("Colors: [%d, %d, %d]%n", red, green, blue);
    
        Color endColour = new Color(red,green,blue);