Java.awt.* Specifically Graphics Class
Hello,
So I am teaching a class and trying to use a simple setColor() function, however the method is apparently undefined in the Graphics class. Does anyone know why? I have copied and pasted the code.
Using Eclipse
Using JRE 7
import java.applet.*;
import java.awt.*;
import java.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
public class Graphics extends JApplet
{
public void paint (Graphics page)
{
final int MID = 130;
final int TOP = 50;
setBackground (Color.cyan);
page.setFont(new Font("TimesRoman", Font.PLAIN, 14));
page.setColor();
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (260, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.red);
page.fillOval (MID-3, TOP+50, 6, 6); // button
page.fillOval (MID-3, TOP+60, 6, 6); // button
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5); // left eye
page.fillOval (MID+5, TOP+10, 5, 5); // right eye
page.drawArc (MID-10, TOP+20, 20, 10, 10, 160); // frown
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect (MID-15, TOP-20, 30, 25); // top of hat
page.drawString ("John Lewis", 20, 20); // artist
}
Re: Java.awt.* Specifically Graphics Class
Hint: What function are you trying to use? How many arguments does it take? How many are you supplying?
Re: Java.awt.* Specifically Graphics Class
The exact error for the correct setColor()'s is this "The method setColor(Color) is undefined for the type Graphics" I thought it passed a Color type.
Re: Java.awt.* Specifically Graphics Class
I understand the first setColor() with no parameter is wrong, haha
Re: Java.awt.* Specifically Graphics Class
Ah, I see another problem. You're naming your class Graphics, which is hiding java.awt.Graphics. That's one reason reusing existing class names is a big no-no.
Re: Java.awt.* Specifically Graphics Class
That was it! Thank you so much. It was driving me nuts. I can't believe I named a new class the same as an existing one. Lesson learned there! The Graphics class is still not popping up as a suggestion when I start with java.awt.... but it let me put the Graphics class there manually. Anyway, thanks again! I was going crazy! haha