Results 1 to 13 of 13

Thread: Java Graphics

  1. #1
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default Java Graphics

    Ok so I have an assignment for my class, I'm very new to Java and I'm stumped. I need to use the .drawString method at the end of this file to print out an array that I have the user input information for but I cannot for the life of me get it to work. My line of code ff.drawLine(numPlay[i], 10, 10); doesnt read the numPlay array in the for statement at the top of the code. How do i get it to recognize this array and draw the information I need? Thank you so much for the help!

    P.S. I am a VERY new beginner, I know that my code may not be very efficient and could be done better, just need this one question answered. Here is the code:

    //Michael Keaton
    //File NFLGameday6.java
    import jpb.*;
    import java.awt.*;

    public class NFLGameDay6 {
    public static void main (String [] args){

    //Construct Team
    SimpleIO.prompt("How many players are on the team: ");
    String userInputA = SimpleIO.readLine().trim();
    int numberOfPlayersA = Integer.parseInt(userInputA);

    //Prompt user to enter players into the Team
    String[] numPlay = new String[numberOfPlayersA];
    for (int i=0; i < numberOfPlayersA; i++){
    SimpleIO.prompt("Enter the name of Player #" + (i +1) + ": ");
    numPlay[i] = SimpleIO.readLine().trim();
    }
    DrawableFrame df = new DrawableFrame ("NFL Gameday 6");
    df.show();
    df.setSize(200*5,100*5);

    Graphics ff = df.getGraphicsContext();
    ff.setColor(Color.white);
    ff.setColor(Color.green);
    ff.fillRect(10,10,140*5,80*5);

    ff.setColor(Color.white);
    ff.drawRect(20,20*5,135*5,40*5);

    ff.drawLine(20*5,100,20*5,300);
    ff.drawLine(120*5,100,120*5,300);
    ff.drawLine(30*5,100,30*5,300);
    ff.drawLine(40*5,100,40*5,300);
    ff.drawLine(50*5,100,50*5,300);
    ff.drawLine(60*5,100,60*5,300);
    ff.drawLine(70*5,100,70*5,300);
    ff.drawLine(80*5,100,80*5,300);
    ff.drawLine(90*5,100,90*5,300);
    ff.drawLine(100*5,100,100*5,300);
    ff.drawLine(110*5,100,110*5,300);

    ff.drawString("Goal",90,90);
    ff.drawString("Goal",90,320);

    ff.drawString("10",29*5,90);
    ff.drawString("10",29*5,320);

    ff.drawString("20",39*5,90);
    ff.drawString("20",39*5,320);

    ff.drawString("30",49*5,90);
    ff.drawString("30",49*5,320);

    ff.drawString("40",59*5,90);
    ff.drawString("40",59*5,320);

    ff.drawString("50",69*5,90);
    ff.drawString("50",69*5,320);

    ff.drawString("40",79*5,90);
    ff.drawString("40",79*5,320);

    ff.drawString("30",89*5,90);
    ff.drawString("30",89*5,320);

    ff.drawString("20",99*5,90);
    ff.drawString("20",99*5,320);

    ff.drawString("10",109*5,90);
    ff.drawString("10",109*5,320);

    ff.drawString("Goal",118*5,90);
    ff.drawString("Goal",118*5,320);

    ff.drawString("ENDZONE",29,23*5);
    ff.drawString("ENDZONE",29,59*5);
    ff.drawString("ENDZONE",123*5,23*5);
    ff.drawString("ENDZONE",123*5,59*5);

    int[] xOuter = {800,851,902,902,851,800,749,749};
    int[] yOuter = {10,10,61,112,163,163,112,61};
    int[] xInner = {805,846,897,897,846,805,754,754};
    int[] yInner = {15,15,66,107,158,158,107,66};
    ff.setColor(Color.black);
    ff.drawPolygon (xOuter, yOuter, xOuter.length);

    ff.setColor(Color.red);
    ff.fillPolygon(xInner, yInner, xInner.length);

    ff.setColor(Color.white);
    ff.setFont (new Font ("SansSerif", Font.BOLD, 80));

    ff.drawString(numPlay[i],10,10);

    df.repaint();

    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When you post code put [code] at the start of the code and [/code] at the end. I'll do that now so I can read the code.

    Java Code:
    //Michael Keaton
    //File NFLGameday6.java
    import jpb.*;
    import java.awt.*;
    
    public class NFLGameDay6 {
       public static void main (String [] args){
    
             //Construct Team
          SimpleIO.prompt("How many players are on the team: ");
          String userInputA = SimpleIO.readLine().trim();
          int numberOfPlayersA = Integer.parseInt(userInputA);
    
             //Prompt user to enter players into the Team
          String[] numPlay = new String[numberOfPlayersA];
          for (int i=0; i < numberOfPlayersA; i++){
             SimpleIO.prompt("Enter the name of Player #" + (i +1) + ": ");
             numPlay[i] = SimpleIO.readLine().trim();
          }
          DrawableFrame df = new DrawableFrame ("NFL Gameday 6");
          df.show();
          df.setSize(200*5,100*5);
    
          Graphics ff = df.getGraphicsContext();
          ff.setColor(Color.white);
          ff.setColor(Color.green);
          ff.fillRect(10,10,140*5,80*5);
    
          ff.setColor(Color.white);
          ff.drawRect(20,20*5,135*5,40*5);
    
          ff.drawLine(20*5,100,20*5,300);
          ff.drawLine(120*5,100,120*5,300);
          ff.drawLine(30*5,100,30*5,300);
          ff.drawLine(40*5,100,40*5,300);
          ff.drawLine(50*5,100,50*5,300);
          ff.drawLine(60*5,100,60*5,300);
          ff.drawLine(70*5,100,70*5,300);
          ff.drawLine(80*5,100,80*5,300);
          ff.drawLine(90*5,100,90*5,300);
          ff.drawLine(100*5,100,100*5,300);
          ff.drawLine(110*5,100,110*5,300);
    
          ff.drawString("Goal",90,90);
          ff.drawString("Goal",90,320);
    
          ff.drawString("10",29*5,90);
          ff.drawString("10",29*5,320);
    
          ff.drawString("20",39*5,90);
          ff.drawString("20",39*5,320);
    
          ff.drawString("30",49*5,90);
          ff.drawString("30",49*5,320);
    
          ff.drawString("40",59*5,90);
          ff.drawString("40",59*5,320);
    
          ff.drawString("50",69*5,90);
          ff.drawString("50",69*5,320);
    
          ff.drawString("40",79*5,90);
          ff.drawString("40",79*5,320);
    
          ff.drawString("30",89*5,90);
          ff.drawString("30",89*5,320);
    
          ff.drawString("20",99*5,90);
          ff.drawString("20",99*5,320);
    
          ff.drawString("10",109*5,90);
          ff.drawString("10",109*5,320);
    
          ff.drawString("Goal",118*5,90);
          ff.drawString("Goal",118*5,320);
    
          ff.drawString("ENDZONE",29,23*5);
          ff.drawString("ENDZONE",29,59*5);
          ff.drawString("ENDZONE",123*5,23*5);
          ff.drawString("ENDZONE",123*5,59*5);
    
          int[] xOuter = {800,851,902,902,851,800,749,749};
          int[] yOuter = {10,10,61,112,163,163,112,61};
          int[] xInner = {805,846,897,897,846,805,754,754};
          int[] yInner = {15,15,66,107,158,158,107,66};
          ff.setColor(Color.black);
          ff.drawPolygon (xOuter, yOuter, xOuter.length);
    
          ff.setColor(Color.red);
          ff.fillPolygon(xInner, yInner, xInner.length);
    
          ff.setColor(Color.white);
          ff.setFont (new Font ("SansSerif", Font.BOLD, 80));
    
          ff.drawString(numPlay[i],10,10);
    
          df.repaint();
       }
    }

  3. #3
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Thank you! I was unaware of that. Any help on the question?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    My line of code ff.drawLine(numPlay[i], 10, 10); doesnt...

    Does the code compile? If it does not and there is a compiler message that you can't understand copy and post the entire message. And indicate which line of your code it is referring to. (very likely this one.)

  5. #5
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    here is the error message i get when compiling:

    File: /Users/users/Desktop/Java/NFLGameDay6.java [line: 95]
    Error: /Users/users/Desktop/Java/NFLGameDay6.java:95: cannot find symbol
    symbol : variable i
    location: class NFLGameDay6

  6. #6
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    I initialized the array outside of the for statement brackets, so why wont the ff.drawString code read the array contents?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Read your compile error. Which symbol was not found? Where is int i declared? Where does it go out of scope?

    db

  8. #8
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    well if i am reading it right, the symbol [i] isn't declared. it goes out of scope within the for statement. However i do need to draw the items in the array, how exactly do i make the array readable outside of the for statement?

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by keaton1ao View Post
    well if i am reading it right, the symbol [i] isn't declared. it goes out of scope within the for statement. However i do need to draw the items in the array, how exactly do i make the array readable outside of the for statement?

    for test purposes i used ff.drawString(numPlay[1],30,60); and i entered the number of player 2 but the name of the player is shown with a size of 80. don't know how you want to print all names with this size in the frame.

  10. #10
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    yeah the set font wasnt right and I fixed the position and size of the font. Were you able to get it to compile though? Because it still won't compile for me?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by keaton1ao View Post
    well if i am reading it right, the symbol [i] isn't declared. it goes out of scope within the for statement. However i do need to draw the items in the array, how exactly do i make the array readable outside of the for statement?

    The array is readable. It is the variable i that the compiler was complaining about.

    If you want to access some particular array element (that you know exists) use its index value instead of i. If you want to deal with all of the array elements, use another for loop.

  12. #12
    keaton1ao is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    ok. thank you for the help. Just for my understanding and learning knowledge, why would another for statement fix the issue with the array elements? and what exactly should that for statement look like?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    why would another for statement fix the issue with the array elements?
    Well, strictly speaking, the compiler is saying "cannot find symbol" when it hits a variable (i) that has gone out of scope. And that's correct, useful, behaviour that's not to be fixed. Rather you do what you want a different way.

    Assuming you want to deal with all of the array elements (for example to print their values), you could use a for loop like the following example:

    Java Code:
    for (int i=0; i < numberOfPlayersA; i++){
        //SimpleIO.prompt("Enter the name of Player #" + (i +1) + ": ");
        //numPlay[i] = SimpleIO.readLine().trim();
    
        // do whatever you like with numPlay[i]
    }

    Within this for loop another variable i is declared and allowed to range over the index values. Within this new loop it is in scope and can be used to validly reference numPlay[0], numPlay[1] etc.

Similar Threads

  1. java graphics within main method
    By jforce93 in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 11:33 PM
  2. Replies: 2
    Last Post: 01-16-2010, 05:50 PM
  3. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 07:04 AM
  4. Java Graphics
    By svaidya in forum New To Java
    Replies: 8
    Last Post: 12-07-2009, 06:11 PM
  5. Want to learn Java Graphics
    By loggen in forum New To Java
    Replies: 7
    Last Post: 01-03-2009, 05:15 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
  •