Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 shape drawing
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Neospud




PostPosted: Mon Nov 13, 2006 10:27 pm   Post subject: shape drawing

I need some help with my code for a shape drawing class. This is what i have so far
Quote:
import java.awt.*;

public class circle implements shapeInt
{
private int xCordCenterPoint;
private int yCordCenterPoint;
private Color c;
private int radius;
private int xcords[];
private int ycords[];
private int fixerX, fixerY;

private final int NUM = 360;



public circle (int cx, int cy, Color color, int r)
{

xCordCenterPoint = cx;
yCordCenterPoint = cy;
c = color;
radius = r;

}

public void drawCenter (Graphics page, boolean showC)
{
if (showC == true)
{
page.setColor (Color.black);
String text = ("." + xCordCenterPoint + "," + yCordCenterPoint);
page.drawString (text, xCordCenterPoint, yCordCenterPoint);
}
}

public void draw(Graphics page, boolean fillShape)
{
xcords = new int [NUM];
ycords = new int [NUM];

page.setColor(c);

for (int i = 0; i < 360; i += 360 / NUM) {
xcords[i] = (int) (radius * Math.sin(i) + xCordCenterPoint);
ycords[i] = (int) (radius * Math.cos(i) + yCordCenterPoint);
}

if(fillShape == true)
page.fillPolygon(xcords, ycords, NUM);
else
page.drawPolygon(xcords, ycords, NUM);
}

public void resize (int xCoord, int yCoord)
{
fixerX = xCoord - xCordCenterPoint;
fixerY = yCoord - yCordCenterPoint;
radius = Math.abs(xCoord - xCordCenterPoint);
}

public void translate (int dx, int dy)
{
xCordCenterPoint = dx - fixerX;
yCordCenterPoint = dy - fixerY;
}

public boolean contains (int xCoord, int yCoord)
{

Polygon three = new Polygon(xcords, ycords, NUM);
boolean contain = three.contains(xCoord, yCoord);
fixerX = xCoord - xCordCenterPoint;
fixerY = yCoord - yCordCenterPoint;
return contain;
}




}


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
Sponsor
Sponsor
Sponsor
sponsor
Neospud




PostPosted: Mon Nov 13, 2006 10:32 pm   Post subject: (No subject)

oh i guess i should mention that this class goes to a GUI that actually creates the shape
ericfourfour




PostPosted: Tue Nov 14, 2006 1:25 am   Post subject: (No subject)

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:
Java:
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:
Java:
draw (graphics, line);


On another note. Indent your code.
gsquare567




PostPosted: Wed Nov 15, 2006 2:16 pm   Post subject: (No subject)

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




PostPosted: Wed Nov 15, 2006 5:17 pm   Post subject: (No subject)

Additionally...

code:
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:

code:
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...

code:
public static void draw(Graphics g, Line2D line) {
    g.drawLine(line.getX1(), line.getY1(), line.getX2(), line.getY2());
}
ericfourfour




PostPosted: Thu Nov 16, 2006 1:32 am   Post subject: (No subject)

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]




PostPosted: Thu Nov 16, 2006 8:30 pm   Post subject: (No subject)

ericfourfour wrote:
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.

ericfourfour wrote:
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




PostPosted: Thu Nov 16, 2006 9:16 pm   Post subject: (No subject)

Well, I am pretty sure my Java is up to date. 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 16, 2006 9:54 pm   Post subject: (No subject)

ericfourfour wrote:
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




PostPosted: Thu Nov 16, 2006 10:04 pm   Post subject: (No subject)

wtd wrote:
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




PostPosted: Thu Nov 16, 2006 11:08 pm   Post subject: (No subject)

ericfourfour wrote:
By-the-way, do you have any idea why it makes me cast the numbers to integers?


Apparently I was wrong.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: