
-----------------------------------
Neospud
Mon Nov 13, 2006 10:27 pm

shape drawing
-----------------------------------
I need some help with my code for a shape drawing class. This is what i have so farimport java.awt.*;

    public class circle implements shapeInt
   {
      private int xCordCenterPoint;
      private int yCordCenterPoint;
      private Color c;
      private int radius;
      private int xcords

please note that the circle is just one of my few examples.  My the contains method at the bottom.  As part of the assignment given i'm not allowed to use the polygon class to draw the polygon.  Any idea on what to do.  I've been searching the awi ref online but i can't find what i need.  I need to do this contains method for a circle rectangle square and oval...if that makes a difference

-----------------------------------
Neospud
Mon Nov 13, 2006 10:32 pm


-----------------------------------
oh i guess i should mention that this class goes to a GUI that actually creates the shape

-----------------------------------
ericfourfour
Tue Nov 14, 2006 1:25 am


-----------------------------------
I think you should look into java.awt.geom before you reinvent the entire wheel. I would just have a single method to render the shape though. It would take the graphics and the shape. Then simply overload that method for all of the shapes you will use.

For example, if you had a line, the method would look like this:
public static void draw (Graphics g, Line2D line)
{
    g.drawLine ((int) line.getX1 (), (int) line.getY1 (), (int) line.getX2 (), (int) line.getY2 ());
}

Then you could call it in your paint method with:
draw (graphics, line);

On another note. Indent your code.

-----------------------------------
gsquare567
Wed Nov 15, 2006 2:16 pm


-----------------------------------
seriously dude clean up ur code... classes start with capitals, not "circle" but "Circle". also use indents, they make it so much easier to read.

-----------------------------------
wtd
Wed Nov 15, 2006 5:17 pm


-----------------------------------
Additionally...

public static void draw (Graphics g, Line2D line)
{
    g.drawLine ((int) line.getX1 (), (int) line.getY1 (), (int) line.getX2 (), (int) line.getY2 ());
}

Should look like this:

public static void draw(Graphics g, Line2D line) {
    g.drawLine((int)line.getX1(), (int)line.getY1(), (int)line.getX2(), (int)line.getY2());
}

Also, Java automatically coerces native types like ints and floats.  I'm guessing that your getter methods return double or float.  Don't typecast.  Let Java do that for you.

So now...

public static void draw(Graphics g, Line2D line) {
    g.drawLine(line.getX1(), line.getY1(), line.getX2(), line.getY2());
}

-----------------------------------
ericfourfour
Thu Nov 16, 2006 1:32 am


-----------------------------------
They return double or float depending on the one created and it is from java.awt.geom.Line2D so technically the getter methods are not mine. What is wrong with putting the curly bracket down a line? It compiles fine for me. Also, I get an error if don't cast them to an integer.

-----------------------------------
[Gandalf]
Thu Nov 16, 2006 8:30 pm


-----------------------------------
What is wrong with putting the curly bracket down a line? It compiles fine for me.
There is nothing wrong with it, really.  It's just that Java convention is to put the opening brace (bracket) on the same line, and it's a good idea to try to follow it.  Of course it compiles fine, Java ignores whitespace from your source code.

Also, I get an error if don't cast them to an integer.
This is almost certainly because you are using an older version of Java.  Sadly, many facilities don't bother updating, so you're most likely stuck with it.

-----------------------------------
ericfourfour
Thu Nov 16, 2006 9:16 pm


-----------------------------------
Well, I am pretty sure my Java is up to date. [url=http://img300.imageshack.us/my.php?image=happynowur6.png]Check  for yourself.

The reason I put my curly bracket down a line is because that is the way I do it in c++ and the school's (sorry excuse for) Java does it too.

-----------------------------------
wtd
Thu Nov 16, 2006 9:54 pm


-----------------------------------
The reason I put my curly bracket down a line is because that is the way I do it in c++ and the school's (sorry excuse for) Java does it too.

Don't let RTP format your code.

-----------------------------------
ericfourfour
Thu Nov 16, 2006 10:04 pm


-----------------------------------
Don't let RTP format your code.
Tell that to my teacher. It is how he wants it formatted. However, I can change the style on any projects at home but it is just going to take a while to get used to.

By-the-way, do you have any idea why it makes me cast the numbers to integers? The image shows the version of Java, the IDE version, the error, and a bunch of other stuff.

-----------------------------------
wtd
Thu Nov 16, 2006 11:08 pm


-----------------------------------
By-the-way, do you have any idea why it makes me cast the numbers to integers?

Apparently I was wrong.
