Stupid Java Coding Problem

Started by ~*Ishida-Uryuu*~, March 28, 2009, 08:38:57 AM

Previous topic - Next topic

~*Ishida-Uryuu*~

I'm a very very basic beginner at Java, and I'm trying to do this assignment out of my textbook.

I wrote this:

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

public class GUIWindow {

public static void main(String[] args) {
JFrame theGUI = new JFrame();
theGUI.setTitle("GUI Example");
String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
if (inputStr == null) return;
int rows = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Number of columns", "5");
if (inputStr == null) return;
int cols = Integer.parseInt(inputStr);
theGUI.setSize(cols * 50, rows * 50);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = theGUI.getContentPane();
pane.setLayout(new GridLayout(rows, cols));
Random gen = new Random();
for (int i = 1; i <= rows * cols; i++) {
int red = gen.nextInt(256);
int green = gen.nextInt(256);
int blue = gen.nextInt(256);
Color backColor = new Color(red, green, blue);
ColorPanel panel = new ColorPanel(backColor);
pane.add(panel);
}

theGUI.setVisible(true);

}

}


But the stupid compiler keeps giving me this error:

I:\java\GUIWindow.java:27: cannot find symbol
symbol  : class ColorPanel
location: class GUIWindow
ColorPanel panel = new ColorPanel(backColor);
^
I:\java\GUIWindow.java:27: cannot find symbol
symbol  : class ColorPanel
location: class GUIWindow
ColorPanel panel = new ColorPanel(backColor);
                       ^
2 errors

Tool completed with exit code 1


What the hell did I do wrong?!

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

DavidGee

Well there is no accessible class named ColorPanel. I think you want to replace

ColorPanel panel = new ColorPanel(backColor);

with

Panel panel = new Panel();
panel.setBackground(backColor);


Either that, or import the package where the ColorPanel class is defined.

~*Ishida-Uryuu*~

Any idea which package that might be?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)

DavidGee

If you're really supposed to use "ColorPanel," then my guess is that you're supposed to be learning inheritance and code ColorPanel yourself as a subclass of Panel. This should be correct:

class ColorPanel extends Panel
{
     ColorPanel(Color color)
     {
          setBackground(color);
     }
}