Newbie: What's wrong with this simple code?

I'm working on learning Java one small step at a time and I've come upon the following problem:

I have four classes that do the work of defining a rectangle of different types (colored, etc.). The following is my code:

publicclass RectTest

{

publicstaticvoid main(String[] args)

{

Rect r1 =new Rect(1, 1, 4, 4);

Rect r2 =new Rect(2, 3, 5, 6);

Rect u = r1.union(r2);

Rect i = r2.intersection(r1);

ColoredRect cr1 =new ColoredRect(1, 1, 4, 4,"red","green");

cr1.draw();

if(u.isInside(r2.x1, r2.y1))

System.out.println("(" + r2.x1 +"," + r2.y1 +") is inside the union");

System.out.println(r1 +" union " + r2 +" = " + u);

System.out.println(r1 +" intersect " + r2 +" = " + i);

}

}

publicclass Rect

{

publicint x1, y1, x2, y2;

public Rect(int x1,int y1,int x2,int y2)

{

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

}

public Rect(int width,int height)

{

this(0,0,width, height);

}

public Rect()

{

this(0,0,0,0);

}

publicvoid move(int deltax,int deltay)

{

x1 += deltax; x2 += deltax;

y1 += deltay; y2 += deltay;

}

/** Test whether the specified point is inside the rectangle */

publicboolean isInside(int x,int y)

{

return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2));

}

/** Return the union of this rectangle with another. I.e. return the

* smallest rectangle that includes them both.

**/

public Rect union(Rect r)

{

returnnew Rect((this.x1 < r.x1) ? this.x1 : r.x1,

(this.y1 < r.y1) ? this.y1 : r.y1,

(this.x2 < r.x2) ? this.x2 : r.x2,

(this.y2 < r.y2) ? this.y2 : r.y2);

}

public Rect intersection(Rect r)

{

Rect result =new Rect((this.x1 > r.x1) ? this.x1 : r.x1,

(this.y1 > r.y1) ? this.y1 : r.y1,

(this.x2 < r.x2) ? this.x2 : r.x2,

(this.y2 < r.y2) ? this.y2 : r.y2);

if (result.x1 > result.x2)

{

result.x1 = result.x2 = 0;

}

if (result.y1 > result.y2)

{

result.y1 = result.y2 = 0;

}

return result;

}

public String toString()

{

return"[" + x1 +"," + y1 +"; " + x2 +"," + y2 +"]";

}

}

publicclass DrawableRectextends Rect

{

public DrawableRect(int x1,int y1,int x2,int y2)

{

super(x1,y1,x2,y2);

}

publicvoid draw(java.awt.Graphics g)

{

g.drawRect(x1, y1, (x2 - x1), (y2 - y1));

}

}

import java.awt.*;

publicclass ColoredRectextends DrawableRect

{

protected Color border, fill;

public ColoredRect(int x1,int y1,int x2,int y2, Color border, Color fill)

{

super(x1, y1, x2, y2);

this.border = border;

this.fill = fill;

}

publicvoid draw(Graphics g)

{

g.setColor(fill);

g.fillRect(x1, y1, (x2-x1), (y2-y1));

g.setColor(border);

g.drawRect(x1, y1, (x2-x1), (y2-y1));

}

}

When I attempt to complile RectTest.java I get an error on the line:

ColoredRect cr1 =new ColoredRect(1, 1, 4, 4,"red","green");

The error is "cannot resolve symbol" and I know that means RectTest can't locate ColoredRect. What's the problem and how do I make this work?

Thanks!

Mike

[7432 byte] By [igby] at [2007-9-26 1:42:05]
# 1
If all your files are in the same folder, try compiling them all at the same time with a wildcard:javac *.java
darrelln at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 2

> If all your files are in the same folder, try

> compiling them all at the same time with a wildcard:

> > javac *.java

I tried this and got the following back from Kawa:

RectTest.java:9: cannot resolve symbol

symbol : constructor ColoredRect (int,int,int,int,java.lang.String,java.lang.String)

location: class ColoredRect

ColoredRect cr1 = new ColoredRect(1, 1, 4, 4, "red", "green");

igby at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 3
The ColoredRect takes four ints and two Colors, but you are passing it four ints and two Strings. Convert the two String arguments to be Colors.
schapel at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 4
OK, the question becomes "how?" I'm trying to figure out how to pass the Color arguments but nothing I try seems to work. How would I do it?
igby at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 5
instead of just a color in quote's"red"pass the function this....new Color("red")if your learning java... get used to things like "new" and how classes work together.
Insodus at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 6
Or just use Color.RED, etc. That way, you won't be creating unnecessary objects.
schapel at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 7

OK, figured out the color issue (Color.red, etc.). Now I'm getting the following when I try to complile RectTest.java:

RectTest.java:12: cannot resolve symbol

symbol : method draw ()

location: class ColoredRect

cr1.draw();

^

The code for ColoredRect:

import java.awt.*;

public class ColoredRect extends DrawableRect

{

protected Color border, fill;

public ColoredRect(int x1, int y1, int x2, int y2, Color border, Color fill)

{

super(x1, y1, x2, y2);

this.border = border;

this.fill = fill;

}

public void draw(Graphics g)

{

g.setColor(fill);

g.fillRect(x1, y1, (x2-x1), (y2-y1));

g.setColor(border);

g.drawRect(x1, y1, (x2-x1), (y2-y1));

}

}

What is wrong now?

Thanks.

igby at 2007-6-29 2:34:28 > top of Java-index,Archived Forums,Java Programming...
# 8
like thisColoredRect cr1 = new ColoredRect(1, 1, 4, 4, Color.red, Color.green); NOT LIKE THISColoredRect cr1 = new ColoredRect(1, 1, 4, 4, "red", "green");
armandfcdx at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 9
your calling the method with no parameters, but the method requires you pass it a Graphics parameter
Insodus at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 10
I suggest buying a Java book, or learning how to program at all before freaking out on these message boards.
Insodus at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 11
you cant call draw(). you dont have a method draw(). you do have a method draw(Graphics g) that takes a graphics object, so when you want to draw, you need to pass a Graphics object.
armandfcdx at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 12

> I suggest buying a Java book, or learning how to

> program at all before freaking out on these message

> boards.

First, I know how to program. I've been programming in Visual Basic, SQL and various web scripting languages for many years now. I've been trying to grasp Java. I've been going through a Java book and I've been trying to learn more by testing my understanding of the concepts in the book on my own. That's why I'm here. To tap into the knowledge that is here in order to understand the language. I'm grateful for those who have taken the time to answer my (rather stupid) questions, but if you feel my questions are idiotic, why not just ignore them? I'm certain that you yourself have run into problems while trying to learn the language.

With that said, I'm trying to understand the example code.

Could someone explain to me how I would properly call the draw method passing a Graphics object as a parameter using the example code I've already posted?

Thanks.

igby at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 13
...and I'm not freaking out at all. I'm just anxious to figure out this code.
igby at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 14

I can't believe you've programmed much in Visual Basic if you ask the question "how I would properly call the draw method passing a Graphics object as a parameter using the example code I've already posted?" It has functions with parameters too. In Java:

draw(g);

where "g" refers to a Graphics object. Or perhaps you meant to ask how you would get or create the Graphics object to use as the parameter?

DrClap at 2007-6-29 2:34:29 > top of Java-index,Archived Forums,Java Programming...
# 15

> I can't believe you've programmed much in Visual Basic

> if you ask the question "how I would properly call the

> draw method passing a Graphics object as a parameter

> using the example code I've already posted?" It has

> functions with parameters too. In Java:

> > draw(g);

>

> where "g" refers to a Graphics object. Or perhaps you

> meant to ask how you would get or create the Graphics

> object to use as the parameter?

Exactly...that's what I meant to ask. Obviously I know how to pass parameters to methods, but what I can't get from this example code is where the Graphics object would come from or what type of Graphics object I would create to pass to the method.

igby at 2007-6-30 22:26:15 > top of Java-index,Archived Forums,Java Programming...
# 16

The problem you are having exceeds the bounds of the Java language. You need to consider "Where" you want to draw your ColoredRect. You have successfully constructed a ColoredRect, you just haven't constructed anywhere to draw it. You need to construct a Frame and then maybe add a panel to the frame and then draw your rectangle on that panel. Graphics programming is a layered approach and I suspect that you should spend some time in the tutorial sections of Sun's page instead of the forums. I suggest that you start with

http://java.sun.com/docs/books/tutorial/2d/index.html.

And, no offense, but Visual Basic doesn't count.

jgreen@atl.lmco.com at 2007-6-30 22:26:15 > top of Java-index,Archived Forums,Java Programming...
# 17

Ok. You dont yet have the understanding of how java graphics works. Below I threw together a very rough bit of code that should (ALONG WITH LOOKING AT AN INTRO JAVA BOOK) introduce you and give you the basics of java graphics. I DID NOT try to compile or run this, because I do not have the time to go through and write this program for you, eventhough it probably wouldnt take very long. But look what I did below, and then look at my explanation below that, then check out a java book, or online tutorial, and then I think you wont have any trouble getting this code to work.

public class RectTest{

public static void main(String[] args)

{

Rect r1 = new Rect(1, 1, 4, 4);

Rect r2 = new Rect(2, 3, 5, 6);

Rect u = r1.union(r2);

Rect i = r2.intersection(r1);

ColoredRect cr1 = new ColoredRect(1, 1, 4, 4, "red", "green");

MyFrame f = new MyFrame(cr1);

frame.show();

if(u.isInside(r2.x1, r2.y1)){

System.out.println("(" + r2.x1 + "," + r2.y1 + ") is inside the union");

System.out.println(r1 + " union " + r2 + " = " + u);

System.out.println(r1 + " intersect " + r2 + " = " + i);

}

}

}

class MyFrame extends JFrame()

{

public MyFrame(ColoredRect c)

{

Container contentPane = getContentPane();

contentPane.add(new MyPanel(c));

}

}

class MyPanel extends JPanel()

{

public myPanel()

{ myColoredRct = c;

}

public void paintComponent(Graphics g)

{ super.paintComponent(g);

myColoredRct.draw(g);

}

private ColoredRect myColoredRct;

}

Ok, what I do is in the main I construct a frame that takes a ColoredRect and then launch that frame. This brings up a window that will contain the drawing. In Java you DONT do the drawing right on the frame, YOU do id on a panel. So as you can see, I added a panel to my frame (to the contentPane of my frame). I passed that panel the ColoredRect, because I will be doing the drawing on the panel, so I need to know what to draw. Then in the paintComponent of my panel, which has a graphics object, I pass that graphics object to your draw method, and then it should draw what your method tells it onto the panel. Then you launch the frame which has a panel that has your ColoredRect drawn onto it.

Hope this helps...

If you need an online resource to help you learn this stuff, just ask. I know of a few good places where you can learn this stuff easily.

armandfcdx at 2007-6-30 22:26:15 > top of Java-index,Archived Forums,Java Programming...
# 18

> Ok. You dont yet have the understanding of how java

> graphics works. Below I threw together a very rough

> bit of code that should (ALONG WITH LOOKING AT AN

> INTRO JAVA BOOK) introduce you and give you the basics

> of java graphics. I DID NOT try to compile or run

> this, because I do not have the time to go through and

> write this program for you, eventhough it probably

> wouldnt take very long. But look what I did below,

> and then look at my explanation below that, then check

> out a java book, or online tutorial, and then I think

> you wont have any trouble getting this code to work.

>

>

> public class RectTest{

> public static void main(String[] args)

> {

> Rect r1 = new Rect(1, 1, 4, 4);

> Rect r2 = new Rect(2, 3, 5, 6);

> Rect u = r1.union(r2);

> Rect i = r2.intersection(r1);

> ColoredRect cr1 = new ColoredRect(1, 1, 4, 4, "red",

> , "green");

>

> MyFrame f = new MyFrame(cr1);

> frame.show();

>

> if(u.isInside(r2.x1, r2.y1)){

> System.out.println("(" + r2.x1 + "," + r2.y1 + ")

> ") is inside the union");

> System.out.println(r1 + " union " + r2 + " = " +

> + u);

> System.out.println(r1 + " intersect " + r2 + " = "

> " + i);

> }

> }

> }

>

> class MyFrame extends JFrame()

> {

> public MyFrame(ColoredRect c)

> {

> Container contentPane = getContentPane();

> contentPane.add(new MyPanel(c));

> }

> }

>

> class MyPanel extends JPanel()

> {

> public myPanel()

> { myColoredRct = c;

> }

>

> public void paintComponent(Graphics g)

> { super.paintComponent(g);

> myColoredRct.draw(g);

> }

>

> private ColoredRect myColoredRct;

> }

>

>

>

> Ok, what I do is in the main I construct a frame that

> takes a ColoredRect and then launch that frame. This

> brings up a window that will contain the drawing. In

> Java you DONT do the drawing right on the frame, YOU

> do id on a panel. So as you can see, I added a panel

> to my frame (to the contentPane of my frame). I

> passed that panel the ColoredRect, because I will be

> doing the drawing on the panel, so I need to know what

> to draw. Then in the paintComponent of my panel,

> which has a graphics object, I pass that graphics

> object to your draw method, and then it should draw

> what your method tells it onto the panel. Then you

> launch the frame which has a panel that has your

> ColoredRect drawn onto it.

>

> Hope this helps...

>

> If you need an online resource to help you learn this

> stuff, just ask. I know of a few good places where

> you can learn this stuff easily.

OK, I appreciate the example and I understand what you are doing there. I would love some other resources (other than the java.sun.com site) for learning this stuff. However, I don't understand why the example code I originally posted was constructed that way and perhaps I never will. At no point do they ever actually use the code.

Thanks for your explanation.

igby at 2007-6-30 22:26:15 > top of Java-index,Archived Forums,Java Programming...
# 19

well I know you said other than java.sun.com, but the following site is very useful.

here is a resource that I find very useful even now.

http://www.java.sun.com/docs/books/tutorial/index.html

Here is the part about using swing components. It even has an example you may want to click on called SimpleExample. You can download the code too.

http://www.java.sun.com/docs/books/tutorial/uiswing/mini/index.html

Okay, but if you dont want java.sun.com maybe you should check out

http://javaboutique.internet.com/tutorials/

I looked at one of those examples and it uses awt instead of swing. swing is a lot newer and better, but if you can use one, you can easily use the other. So just check out the examples here.

hope this helps.

armandfcdx at 2007-6-30 22:26:15 > top of Java-index,Archived Forums,Java Programming...