Lawleyenda
Lawleyenda

Reputation: 75

Java: Graphics2D

import java.awt.*;
import javax.swing.*;
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 Colors");
    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();
}
}

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

class MyDrawPanel extends JPanel {

public void paintComponet(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

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

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

    GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70, 70, 100, 100);

}
}

So I have been trying to solve this problem for 2 hours straight and do not seem to be able to problem solve. This "program" is supposed to have an oval and a button come up at the bottom of the screen that allows me to randomize the color of the oval.I am using netbeans and whenever I click run I get this: screenshotDoes anyone have any solutions to fix my problems? Sorry for wasting your time if it is a stupid question.

Upvotes: 2

Views: 269

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Spelling matters: paintComponet != paintComponent

Always preface your overridden methods with @Override. If you'd have done this, you the compiler would warned you about your error.

So change this:

public void paintComponet(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

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

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

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

to this:

@Override // don't forget this
protected void paintComponent(Graphics g) { // spelling matters. Also make it protected

    // !!!! don't forget this!
    super.paintComponent(g); // to have the JPanel do housekeeping painting

    Graphics2D g2d = (Graphics2D) g;

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

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

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

Upvotes: 3

Related Questions