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

Username:   Password: 
 RegisterRegister   
 help - moving a flag
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
whoareyou




PostPosted: Sun May 01, 2011 1:44 pm   Post subject: help - moving a flag

I've created a program that will draw a Guyana flag. I've implemented variables so that changing and moving around the flag would be much easier (i.e change the variables at the beginning of the program to change the entire flag). The current origin position is at (0,0). Now if I wanted to change the origin position, everything gets misplaced. If i change the maximum width and maximum height, the works and everything is still in proportion. What I can't figure out is why the flag gets messed up when I change the origin.

Java:

// The "GuyanaFlag" class.
import java.awt.*;
import hsa.Console;

public class GuyanaFlag
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        //Background
        int bw = c.maxx ();
        int bh = c.maxy ();

        //White triangle
        int wside = 20;

        //Yellow Triangle
        int originx = 0;
        int originy = 0;


        //black triangle
        int tspaces = 50;

        //red triangle
        int ospaces = 30;

        //SIZING FACTOR
        double k = 1;
       
        //Apply the sizing factor
       
        k = k*1;
       
        bw = (int) Math.ceil (Math.round (k*bw));
        bh = (int) Math.ceil (Math.round (k*bh));
        wside = (int) Math.ceil (Math.round (k*wside));
        tspaces = (int) Math.ceil (Math.round (k*tspaces));
        ospaces = (int) Math.ceil (Math.round (k*ospaces));





        c.setColor (Color.green);
        c.fillRect (originx, originy, bw, bh);

        c.setColor (Color.white);

        for (int i = bh ; i >= originy ; i--)
        {

            c.drawLine (bw, bh / 2, originx, i + wside);
            c.drawLine (bw, bh / 2, originx, i - wside);

        }

        c.setColor (Color.yellow);

        c.drawLine (originx, originy, bw - tspaces, bh / 2);
        c.drawLine (bw - tspaces, bh / 2, originx, bh);

        for (int i = bh ; i >= originy ; i--)
        {

            c.drawLine (bw - tspaces, bh / 2, originx, i);

        }

        c.setColor (Color.black);

        c.drawLine (originx, originy, bw / 3 + tspaces, bh / 2);
        c.drawLine (bw / 3 + tspaces, bh / 2, originx, bh);

        for (int i = bh ; i >= originy ; i--)
        {

            c.drawLine (bw / 3 + tspaces, bh / 2, originx, i);

        }

        c.setColor (Color.red);
        c.drawLine (originx, ospaces, bw / 3, bh / 2);
        c.drawLine (bw / 3, bh / 2, originx, bh - ospaces);

        for (int i = bh - ospaces ; i >= ospaces ; i--)
        {

            c.drawLine (bw / 3, bh / 2, originx, i);

        }

        c.setColor (Color.black);
        c.drawRect (originx, originy, bw, bh);

    } // main method
} // GuyanaFlag class
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sun May 01, 2011 11:35 pm   Post subject: RE:help - moving a flag

Er. Might I suggest using: fillPolygon ( http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html#fillPolygon(int[], int[], int) ) for triangles.

Also you didn't use the offset variables in every single draw function. It looks like you just replaced all the 0s with your origin variables. Remember that every single point is being translated.

You need to start off with drawRect(offsetX, offsetY, ...) for everything instead of assuming it starts at (0,0).

Advanced Concepts
If you understand OOP, then you might want to attempt at creating a flag object, even attempt to extend the Rectangle class (as it already has x,y,w,h but overriding it's methods might be a pain). Each flag could then have it's own list of shapes that would be drawn on top of it when it's draw method is called. If you're interested in this method just ask and I'll go into greater detail.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: