Results 1 to 8 of 8
  1. #1
    typh0n is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default Help Needed, simple jframe display

    I have a file with the drawing instructions, a file with the component draw instructions, and a view file(jframe)

    What im having trouble with is, coloring.. haha.. I can't change the flag colors specifically per flag, i can just change it in the drawing instructions file, therefor it changes the colors for all 3 flags, im wondering how i can edit the colors in the component draw file, here are my files below.

    Any help is apreciated !!

    //// DRAWING INSTRUCTIONS

    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.Color;



    public class Flag
    {
    public Flag(int x, int y)
    {
    xLeft = x;
    yTop = y;
    }

    public void draw(Graphics g)
    {
    Graphics2D g2= (Graphics2D) g;

    /*Flag Drawing Instructions*/

    Rectangle Left = new Rectangle(xLeft +4,yTop + 0, 40, 66);


    Rectangle Middle = new Rectangle(xLeft + 44,yTop + 0, 40, 66);


    Rectangle Right = new Rectangle(xLeft + 84,yTop + 0, 40, 66);

    g2.setColor(Color.BLUE);
    g2.fill(Left);


    g2.setColor(Color.RED);
    g2.fill(Right);

    g2.setColor(Color.WHITE);
    g2.fill(Middle);

    g2.draw(Left);
    g2.draw(Right);
    g2.draw(Middle);


    }
    public static int WIDTH = 200;
    public static int HEIGHT = 300;
    private int xLeft;
    private int yTop;

    }



    ////// COMPONENT (Where to put the drawing)

    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    import java.awt.Color;

    public class FlagComponent extends JComponent
    {


    public void paintComponent(Graphics g)
    {
    Graphics2D g2= (Graphics2D) g;

    Flag Flag1 = new Flag(4,100);


    Flag Flag2 = new Flag(4,15);


    Flag Flag3 = new Flag(4,185);

    Flag1.draw(g2);
    Flag2.draw(g2);
    Flag3.draw(g2);



    }
    }



    ////// VIEWER


    import javax.swing.JFrame;

    public class FlagViewer
    {
    public static void main(String[] args)
    {
    JFrame frame = new JFrame();

    final int FRAME_WIDTH = 150;
    final int FRAME_HEIGHT = 300;

    frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
    frame.setTitle("Flags");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    FlagComponent component = new FlagComponent();
    frame.add(component);

    frame.setVisible(true);

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So, currently the Colors are hard-coded into the Flag class, and you are trying to make them a changeable state of the class, correct? So how do you add a state to a system in general? You add a State variable. How to change it? Several ways, but the easiest is to just pass the desired values as parameters of the constructor.

    In other words, why not have Color variables in Flag, and change these with constructor parameters. If you want to change these dynamically, then you'll also want setters.

    Oh, also, if you do post your code, please use code tags so that your code will be well-formatted and readable. To do this, either highlight your code block and press the "code" button at the top above the message block or place the tag [code] at the top of your block of code and the tag [/code] at the bottom.

    Good luck.

  3. #3
    typh0n is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Thank's for you're help, but i don't quite understand .. I'm in first year programming, and have been way too busy with calculus to really put time into programming yet, even though im doing a bachelor of science in computer science ! This weekend im going to catch up 100% in programming.

    So , I should define the colors in Flag.java, and in FlagComponent i should call upon them? How exactly would i go about doing this ? If you don't mind... :(, I really apreciate the help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry to be blunt, but your lack of studying is not our problem, and you'll be better off just not mentioning it. My suggestions are nothing more than basic Java -- adding fields to classes, adding parameters to constructors. You really need to study the basics on this as nothing I can say will be any better than your texts or the tutorials, and I'm just a volunteer here (we all are). I suggest you hit your books, then try a solution. If it doesn't work, then keep trying as this is how you will learn best. If you have specific question regarding your code, then post your code and your questions. Still, you've a bit of work to do.

    Again, good luck.
    Last edited by Fubarable; 10-03-2008 at 12:55 AM.

  5. #5
    typh0n is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    I don't know how much more specific i could have been .. I'm just wondering what specifically i have to do to my FlagComponent (#2) to be able to change the colors of the flags i have drawn in my #1 file.

    I have hit the books for this, and have exausted alot of online resources, and have been working on this java file for the past three hours, and have gotten stuck at this point.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    And I have told you how to do this, but you don't understand it because you do not yet understand the basic concepts. Only you can correct this, and this will take study. A little less posting and more studying will go a long way to reaching your goal. Of course you know that we will not write your code for you.

    Again, much luck.

  7. #7
    typh0n is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Sorry you took so much offense to my comment about my 'lack of studying' ... I have found my answer via another forum, with might i say, slightly more helpful and less 'GO STUDY' comments.. Thank's for you're time.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Oh, and there is nothing you can do in FlagComponent to change the colors until you make the changes to the Flag class: add some Color variables, change the constructor and /or add setter methods (public void setColorX(Color cX)) methods. Only then can you change the colors.

Similar Threads

  1. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 07:02 PM
  2. Replies: 0
    Last Post: 06-28-2008, 09:26 PM
  3. How to display scrolling text and image on a JFrame
    By Abhi_vk in forum AWT / Swing
    Replies: 1
    Last Post: 06-20-2008, 10:19 PM
  4. Simple JFrame Problem
    By DC1 in forum New To Java
    Replies: 4
    Last Post: 06-06-2008, 07:09 AM
  5. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •