Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Literally, what's wrong with my code? Separating X and Y coordinates.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Literally, what's wrong with my code? Separating X and Y coordinates.

    // your code goes here
    public static void main(String[] args)
    static void drawFlag(Graphics2D g){
      int w=300; int h=208;
      int [] ods=new int[]{
         281,144,288,143,287,152,295,158,297,166,292,177,
         291,188,296,203,307,211,322,208,341,199,362,203,
         360,193,372,191,376,187,391,196,401,193,415,194,
         425,183,426,174,440,161,451,173,452,189,460,204,
         468,208,461,222,448,234,443,241,445,252,452,252,
         457,237,473,237,492,238,492,247,488,256,480,262,
         492,259,498,248,512,246,542,257,550,264,558,262,
         563,272,563,281,567,284,581,279,596,284,616,289,
         638,288,665,306,677,317,693,329,715,336,725,349,
         728,371,723,393,706,413,695,424,680,442,668,461,
         656,466,648,476,650,489,650,508,646,531,640,551,
         630,566,631,578,617,594,607,609,607,622,592,629,
         588,634,588,637,576,638,558,634,550,639,541,641,
         533,646,530,651,520,651,501,664,491,674,481,681,
         477,698,480,711,476,727,463,739,455,757,438,782,
         425,789,415,804,408,817,402,818,400,807,407,799,
         400,792,395,787,375,774,372,767,365,772,352,757,
         342,757,346,749,365,728,386,711,397,703,400,687,
         395,678,386,676,391,657,386,646,373,649,367,621,
         353,617,332,618,333,598,327,581,337,551,332,538,
         322,531,322,517,291,517,286,498,290,494,287,477,
         232,451,201,406,177,412,166,426,155,426,143,436,
         123,432,117,434,110,427,111,407,101,417, 83,417,
          81,408, 67,407, 71,399, 51,376, 55,363, 67,358,
          65,349, 67,333,100,314,115,318,128,262,123,248,
         113,243,116,231,131,228,126,221,120,221,120,212,
         148,204,152,211,162,201,168,214,180,223,196,219,
         220,208,231,198,226,192,215,177,208,163,225,168,
         240,172,248,162,268,156,281,144};
    }
    int len = ods.length;
    int[] x = new int [len/2];
    int[] y = new int [len/2];
     
    int cx = 0, cy = 0;	
     
    for (int i = 0; i<len; i++) {
    if (i%2==0) {
    x[cx++] = ods[i];
    } else {
    y[cy++] = ods[i];
    }
    }

    Doing a practice exercise on my way to learning Java but I'm stumped. I believe I have one too many or too little { brackets since I never seem to get that right first time. Any help is welcome, even a pointer in the right direction would be appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    What error do you get?

    Go through and find each opening bracket. Where does it end? Go through and find each closing bracket. Where does it begin? Using proper indentation would help with that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    Raster.java:48: illegal start of type
    for (int i = 0; i expected
    for (int i = 0; i expected
    for (int i = 0; i

    Is the error I get.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    Quote Originally Posted by liamb109 View Post
    Raster.java:48: illegal start of type
    for (int i = 0; i expected
    for (int i = 0; i expected
    for (int i = 0; i

    Is the error I get.
    Ah, okay. And what did you find when you fixed the indentation and matched up the curly brackets?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    liamb109 (October 8th, 2012)

  6. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    I got that part working. But now I'm having further trouble with the full program that I wasn't displaying earlier because it wasn't relevant. I keep getting the error .class expected on ods=new int[]{ and I fixed this before when it did it with len but I can't remember how. And while I'm at it, is there anything else wrong with this part that I should look at?
    static void drawFlag(Graphics2D g){
      int w=300; int h=208;
     g.setRenderingHint(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);
      int w=300; int h=208;
    for (int i=0;i<13;i++)
      {
        g.setColor((i%2==0)?Color.black:Color.white);
        g.fillRect(0,i*16,300,208);
      }
    g.setColor(new Color(222,24,24));
    g.fillRect(0,0,115,80);
     
    Polygon brazil = new Polygon(
      int [] ods=new int[]{
         281,144,288,143,287,152,295,158,297,166,292,177,
         291,188,296,203,307,211,322,208,341,199,362,203,
         360,193,372,191,376,187,391,196,401,193,415,194,
         425,183,426,174,440,161,451,173,452,189,460,204,
         468,208,461,222,448,234,443,241,445,252,452,252,
         457,237,473,237,492,238,492,247,488,256,480,262,
         492,259,498,248,512,246,542,257,550,264,558,262,
         563,272,563,281,567,284,581,279,596,284,616,289,
         638,288,665,306,677,317,693,329,715,336,725,349,
         728,371,723,393,706,413,695,424,680,442,668,461,
         656,466,648,476,650,489,650,508,646,531,640,551,
         630,566,631,578,617,594,607,609,607,622,592,629,
         588,634,588,637,576,638,558,634,550,639,541,641,
         533,646,530,651,520,651,501,664,491,674,481,681,
         477,698,480,711,476,727,463,739,455,757,438,782,
         425,789,415,804,408,817,402,818,400,807,407,799,
         400,792,395,787,375,774,372,767,365,772,352,757,
         342,757,346,749,365,728,386,711,397,703,400,687,
         395,678,386,676,391,657,386,646,373,649,367,621,
         353,617,332,618,333,598,327,581,337,551,332,538,
         322,531,322,517,291,517,286,498,290,494,287,477,
         232,451,201,406,177,412,166,426,155,426,143,436,
         123,432,117,434,110,427,111,407,101,417, 83,417,
          81,408, 67,407, 71,399, 51,376, 55,363, 67,358,
          65,349, 67,333,100,314,115,318,128,262,123,248,
         113,243,116,231,131,228,126,221,120,221,120,212,
         148,204,152,211,162,201,168,214,180,223,196,219,
         220,208,231,198,226,192,215,177,208,163,225,168,
         240,172,248,162,268,156,281,144};
     
    int len = ods.length;
    int[] x = new int [len/2];
    int[] y = new int [len/2];
     
    int cx = 0, cy = 0;	// counters for x- and y-co-ordinates
     
    for (int i = 0; i<len; i++) {
    if (i%2==0) {
    x[cx++] = ods[i];
    } else {
    y[cy++] = ods[i];
    }
    }
    {
    g.translate(-68,-40);
    g.setColor(Color.white);
     g.fillOval(100,50,60,60);
    g.setColor(new Color(8,8,107));
    g.scale(0.08,0.08);
    g.translate(1200,595);
    g.fillPolygon(brazil);
     
     
    }
    }

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    If you want help, you're going to have to fix your indentation and post the full text of any error you get. But look at the line where the error is. Is that valid syntax? Hint: is that line part of the line above it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    The full error code now is:
    Raster.java:25: '.class' expected
    ods=new int[]{
    ^
    1 error

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    That specific piece is a part of a long line of code that actually spans multiple lines. Take them all together- is that valid syntax? Again, fixing your indentation should help spot things like this.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    I solved the problem I had before. But now I'm getting the error message
    Raster.java:24: cannot find symbol
    symbol : variable ods
    location: class Raster
    ods=new int[]{
    ^
    Raster.java:23: internal error; cannot instantiate java.awt.Polygon. at java.awt.Polygon to ()
    Polygon brazil = new Polygon(
    ^
    Raster.java:56: cannot find symbol
    symbol : variable ods
    location: class Raster
    int len = ods.length;
    ^
    Raster.java:64: cannot find symbol
    symbol : variable ods
    location: class Raster
    x[cx++] = ods[i];
    ^
    Raster.java:66: cannot find symbol
    symbol : variable ods
    location: class Raster
    y[cy++] = ods[i];

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    You haven't fixed your problem, you just substituted it for more problems, and now you're working from code that we can't see, which makes it pretty hard to help you. Like I said, if you want help, you'll have to fix your indentation and take a closer look at that full line.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.io.File;
    import javax.imageio.ImageIO;
     
    public class Raster{
     
    static void drawFlag(Graphics2D g)
    {
     g.setRenderingHint(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);
        for (int i=0;i<13;i++)
      {
        g.setColor((i%2==0)?Color.black:Color.white);
        g.fillRect(0,i*16,300,208);
      }
    {
    g.setColor(new Color(222,24,24));
    g.fillRect(0,0,115,80);
    }
    int[] ods;
    {
       Polygon brazil = new Polygon(
     
    	 ods=new int[]{
         281,144,288,143,287,152,295,158,297,166,292,177,
         291,188,296,203,307,211,322,208,341,199,362,203,
         360,193,372,191,376,187,391,196,401,193,415,194,
         425,183,426,174,440,161,451,173,452,189,460,204,
         468,208,461,222,448,234,443,241,445,252,452,252,
         457,237,473,237,492,238,492,247,488,256,480,262,
         492,259,498,248,512,246,542,257,550,264,558,262,
         563,272,563,281,567,284,581,279,596,284,616,289,
         638,288,665,306,677,317,693,329,715,336,725,349,
         728,371,723,393,706,413,695,424,680,442,668,461,
         656,466,648,476,650,489,650,508,646,531,640,551,
         630,566,631,578,617,594,607,609,607,622,592,629,
         588,634,588,637,576,638,558,634,550,639,541,641,
         533,646,530,651,520,651,501,664,491,674,481,681,
         477,698,480,711,476,727,463,739,455,757,438,782,
         425,789,415,804,408,817,402,818,400,807,407,799,
         400,792,395,787,375,774,372,767,365,772,352,757,
         342,757,346,749,365,728,386,711,397,703,400,687,
         395,678,386,676,391,657,386,646,373,649,367,621,
         353,617,332,618,333,598,327,581,337,551,332,538,
         322,531,322,517,291,517,286,498,290,494,287,477,
         232,451,201,406,177,412,166,426,155,426,143,436,
         123,432,117,434,110,427,111,407,101,417, 83,417,
          81,408, 67,407, 71,399, 51,376, 55,363, 67,358,
          65,349, 67,333,100,314,115,318,128,262,123,248,
         113,243,116,231,131,228,126,221,120,221,120,212,
         148,204,152,211,162,201,168,214,180,223,196,219,
         220,208,231,198,226,192,215,177,208,163,225,168,
         240,172,248,162,268,156,281,144},ods, 14);
     
     
    int len = ods.length;
      int[] x = new int [len/2];
      int[] y = new int [len/2];
     
    int cx = 0, cy = 0;	// counters for x- and y-co-ordinates
     
    for (int i = 0; i<len; i++) {
      if (i%2==0) {
      x[cx++] = ods[i];
    } else {
      y[cy++] = ods[i];
      }
    }
    {
    g.translate(-68,-40);
    g.setColor(Color.white);
    g.fillOval(100,50,60,60);
    g.setColor(new Color(8,8,107));
    g.scale(0.08,0.08);
    g.translate(1200,595);
    g.fillPolygon(brazil);
    {
    	  Polygon star = new Polygon(
    	   new int[]{0,22,95,36,59,0,-59,-36,-95,-22},
    	   new int[]{-100,-31,-31,12,81,38,81,12,-31,-31},10);
    g.setColor(Color.yellow);
    g.translate(-150,150);
    g.scale(1.25,1.25);
    g.fillPolygon(star);
    g.translate(850,0);
    g.fillPolygon(star);
    g.translate(0,520);
    g.fillPolygon(star);
    g.translate(-850,0);
    g.fillPolygon(star);
     
    }
    }
    }
    }
     
     
      public static void main(String argv []){
        BufferedImage image = new BufferedImage(300,208, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        drawFlag(g);
        g.dispose();
        try {
            File out = new File("flag.png");
            ImageIO.write(image, "png", out);
        }catch(Exception e){
         System.err.println(e);
        }
      }
    }

    No errors anymore but the polygon "brazil" isn't displaying anything in the image and I have no idea where I've gone wrong there.

  13. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    Quote Originally Posted by KevinWorkman View Post
    Like I said, if you want help, you'll have to fix your indentation.
    Please fix the posted code so it is easier to read. If you need help look at the announcements page.

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Literally, what's wrong with my code? Separating X and Y coordinates.

    Quote Originally Posted by liamb109 View Post
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.io.File;
    import javax.imageio.ImageIO;
     
    public class Raster{
     
    static void drawFlag(Graphics2D g)
    {
     g.setRenderingHint(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);
        for (int i=0;i<13;i++)
      {
        g.setColor((i%2==0)?Color.black:Color.white);
        g.fillRect(0,i*16,300,208);
      }
    {
    g.setColor(new Color(222,24,24));
    g.fillRect(0,0,115,80);
    }
    int[] ods;
    {
       Polygon brazil = new Polygon(
     
    	 ods=new int[]{
         281,144,288,143,287,152,295,158,297,166,292,177,
         291,188,296,203,307,211,322,208,341,199,362,203,
         360,193,372,191,376,187,391,196,401,193,415,194,
         425,183,426,174,440,161,451,173,452,189,460,204,
         468,208,461,222,448,234,443,241,445,252,452,252,
         457,237,473,237,492,238,492,247,488,256,480,262,
         492,259,498,248,512,246,542,257,550,264,558,262,
         563,272,563,281,567,284,581,279,596,284,616,289,
         638,288,665,306,677,317,693,329,715,336,725,349,
         728,371,723,393,706,413,695,424,680,442,668,461,
         656,466,648,476,650,489,650,508,646,531,640,551,
         630,566,631,578,617,594,607,609,607,622,592,629,
         588,634,588,637,576,638,558,634,550,639,541,641,
         533,646,530,651,520,651,501,664,491,674,481,681,
         477,698,480,711,476,727,463,739,455,757,438,782,
         425,789,415,804,408,817,402,818,400,807,407,799,
         400,792,395,787,375,774,372,767,365,772,352,757,
         342,757,346,749,365,728,386,711,397,703,400,687,
         395,678,386,676,391,657,386,646,373,649,367,621,
         353,617,332,618,333,598,327,581,337,551,332,538,
         322,531,322,517,291,517,286,498,290,494,287,477,
         232,451,201,406,177,412,166,426,155,426,143,436,
         123,432,117,434,110,427,111,407,101,417, 83,417,
          81,408, 67,407, 71,399, 51,376, 55,363, 67,358,
          65,349, 67,333,100,314,115,318,128,262,123,248,
         113,243,116,231,131,228,126,221,120,221,120,212,
         148,204,152,211,162,201,168,214,180,223,196,219,
         220,208,231,198,226,192,215,177,208,163,225,168,
         240,172,248,162,268,156,281,144},ods, 14);
     
     
    int len = ods.length;
      int[] x = new int [len/2];
      int[] y = new int [len/2];
     
    int cx = 0, cy = 0;	// counters for x- and y-co-ordinates
     
    for (int i = 0; i<len; i++) {
      if (i%2==0) {
      x[cx++] = ods[i];
    } else {
      y[cy++] = ods[i];
      }
    }
    {
    g.translate(-68,-40);
    g.setColor(Color.white);
    g.fillOval(100,50,60,60);
    g.setColor(new Color(8,8,107));
    g.scale(0.08,0.08);
    g.translate(1200,595);
    g.fillPolygon(brazil);
    {
    	  Polygon star = new Polygon(
    	   new int[]{0,22,95,36,59,0,-59,-36,-95,-22},
    	   new int[]{-100,-31,-31,12,81,38,81,12,-31,-31},10);
    g.setColor(Color.yellow);
    g.translate(-150,150);
    g.scale(1.25,1.25);
    g.fillPolygon(star);
    g.translate(850,0);
    g.fillPolygon(star);
    g.translate(0,520);
    g.fillPolygon(star);
    g.translate(-850,0);
    g.fillPolygon(star);
     
    }
    }
    }
    }
     
     
      public static void main(String argv []){
        BufferedImage image = new BufferedImage(300,208, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        drawFlag(g);
        g.dispose();
        try {
            File out = new File("flag.png");
            ImageIO.write(image, "png", out);
        }catch(Exception e){
         System.err.println(e);
        }
      }
    }

    No errors anymore but the polygon "brazil" isn't displaying anything in the image and I have no idea where I've gone wrong there.
    Are you sure that code doesn't generate any errors? As I've suggested repeatedly, the key to figuring out your problem is to fix your indentation and take a closer look at the syntax you're using, which is being obscured by you trying to do too many things on a single line. Fix your indentation and take a look at that entire line, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is wrong with this code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 24th, 2012, 12:31 AM
  2. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 07:28 PM
  3. Separating Digits in a String
    By Staticity in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 06:39 AM
  4. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM
  5. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM