Passing Graphics as a paramater; or other alternatives

Hullo,

I'm working on an End-of-Year programming assignment for my Java I class. It's suppose to be simply from what we've learned this year, which excludes passing Graphics and other alternatives than an applet and console window.

What I'm currently trying to do, is create a file and class that constructs an image like, g.drawRect();, and g.fillOval();. Then I have another file with the paint method, constructing an object of the class. I've tried every way of passing and not passing and naming of Graphics.

I read that you cannot pass Graphics, because it's passing an complex object, or it's being instantiated, or blah blah blah, it just cannot be done.

So, just curious if there were any alternatives, such as using Swing/JFC/JFrame to setup the window, but still being able to use my usual code for creating graphics. (g.drawRect(). g.setColor())

It makes me mad, because how are we to create a sophisticated program when we cannot even using graphics in a proper OOP context.

Unfortunately for this project we have to use graphics, so...

[1099 byte] By [Adamskia] at [2007-11-27 4:21:33]
# 1

> I read that you cannot pass Graphics, because

> it's passing an complex object, or it's being instantiated,

> or blah blah blah, it just cannot be done.

That's nonsense. Where did you read it?

Are you saying that the assignment specification doesn't allow you to?

jsalonena at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 2
I read it here.And allow me to do what?
Adamskia at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 3

> I read it here.

If you read it here, you either misunderstood what was said or you were reading a forum entry from the wrong person. You can pass Graphics objects. It's done all the time.

> And allow me to do what?

He was asking whether you meant that your programming assignment didn't allow you to pass Graphics objects for some reason.

paulcwa at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 4
Two people have said I can, but not how.And yes, it is allowed to pass Graphics, just no one knows how apparently.
Adamskia at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 5

Here's my error:

PlanetEdge.java:15: cannot find symbol

symbol : method Team(int,java.awt.Graphics)

location: class PlanetEdge

Team(1, g);

Here's my code:

PlanetEdge.java

/**

*PlanetEdge

*Paul Adamski

*

*PlanetEdge.java

*/

import java.awt.*;

import java.applet.*;

public class PlanetEdge extends Applet

{

public void paint(Graphics g)

{

Team(1, g);

}

}

Team.java

/**

*PlanetEdge

*Paul Adamski

*

*Team.java

*/

import java.awt.*;

import java.applet.*;

public class Team

{

public Team(int teamChoice, Graphics g)

{

g.drawRect(10,10,50,50);

}

}

Adamskia at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 6
Maybe you meant:new Team(1, g);What is the point of passing an int to the constructor if you aren't going to use it?
CaptainMorgan08a at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 7
You fixed it!Dukes to you.And I'm not using it now. In case you couldn't tell, I'm setting up the basic framework of the program.
Adamskia at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 8

Also, you probably shouldn't do any drawing in the constructor. Something like this would be better.

public class RedRectangle

{

int width;

int height;

public RedRectangle(int w, int h)

{

width = w;

height = h;

}

public void draw(Graphics g)

{

g.setColor(Color.red);

g.fillRect(0, 0, width, height);

}

}

This is just an example.

CaptainMorgan08a at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...
# 9

> Two people have said I can, but not how.

>

> And yes, it is allowed to pass Graphics, just no one

> knows how apparently.

Don't be such a jerk.

Anyway, how much hand-holding do you need? Don't you know how to pass an object to a method as an argument?Didn't you do exactly that in the code you posted? And didn't that post show that you had an unrelated problem?

paulcwa at 2007-7-12 9:28:41 > top of Java-index,Java Essentials,New To Java...