Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default HELP. Getting an error returning a 2d array

    im a sophomore in highschool taking java, and for a lab we need to create a 2d array that holds values of a few graphic elements, black square, red squares etc etc. Im storing the values as ints and then in my driver class, im converting the ints to colors with a switch statement. Im running into an issue though with returning my 2d array. Here is the code, and the error is " cannot find symbol - variable screen. ( Im using bluej)

    Java Code:
    /**
     * Write a description of class GraphicArray here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    
    public class GraphicArray
    {
    
    public GraphicArray()
    {
    int Blank = 0;
    int Black = 1;
    int White = 2;
    int Red = 3;
    int xloc = 0;
    int yloc = 0;
    int screen[] [] = new int[8] [8] ;
    int pos = 0;
    for( int i = 0; i <= 7; i++)
    {
    screen [i] [i] = pos;
    pos+=50;
    }
    
    screen[2] [3] = Red;
    
    if( screen[2] [3] == Red)
    System.out.println (" a red square ");
    
    
    }
    
    public int getpix (int x, int y)
    {
    return screen [x] [y];
    }
    }
    Last edited by Norm; 02-11-2012 at 07:41 PM. Reason: added code tags

  2. #2
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    try replacing "int screen[] [] = new int[8] [8] ;" by
    "private int screen[] [] = new int[8] [8] ;"

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    error is " cannot find symbol - variable screen
    Please post the full text of the compiler's error message. It has important information that you have left off the post.

    It looks like you have defined your variables inside of the constructor. That means they are out of scope for the rest of the class.
    Move the variable definitions outside of the constructor for those variables that need to be used else where in your code.
    Last edited by Norm; 02-11-2012 at 07:43 PM.

  4. #4
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    Yep, i declared the array in the getter method and it worked.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    You should define the array outside of the methods.
    You can give it a value inside of any method.

  6. #6
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    Thank you so much!!! In my driver program I was able to get simple squares to show up. I made a loop so i could run through the values of the array and print all the squares at the defined location. I ran into a small issue. The second I made my loop, no graphic elements would show up, none at all. even if i made a 50, 50, 50, 50 square with red as the color, nothing would show up when i ran the applet. please help!


    //************************************************** ******************
    // Snowman.java Author: Lewis/Loftus
    //
    // Demonstrates basic drawing methods and the use of color.
    //************************************************** ******************

    import javax.swing.JApplet;
    import java.awt.*;
    import java.util.Random;

    public class girthgraphics extends JApplet
    {

    GraphicArray array = new GraphicArray();//-----------------------------------------------------------------
    // Draws a checker board and sets up values of spot locations
    //-----------------------------------------------------------------
    public void paint (Graphics page)
    {
    int x = 0;
    int y = 0;
    int xloc = x * 50;
    int yloc = y * 50;

    for( int i = 0; i <= 8; i++)
    {
    x++;
    y++;

    switch(array.getpix(x, y))
    {
    case 1:
    page.setColor(Color.black);
    page.fillRect(xloc, yloc, 50, 50);
    break;
    case 2:
    page.setColor(Color.white);
    page.fillRect(xloc, yloc, 50, 50);
    break;
    case 3:
    page.setColor(Color.red);
    page.fillRect(xloc, yloc, 50, 50);
    break;
    }
    repaint();
    }



    }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    One problem you have is you left out the default: case in the switch statement. You should print out a message with the switch value so you can see what the problem is.

  8. #8
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    I did that, and still nothing. Its not the switch statement because before i put the loop, i just manual entered values for x and y and i was able to get elements to show up. When i put in my loop nothing shows. Even if i just make a giant circle outside of the loop, it won't show either. I have to put an element in front of the for loop, then it shows up. If its in it or after it, it wont show.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    Is the paint method being called? Add a println to the paint method to show that it is called.

    Also print out the values of xloc and yloc to see where you are drawing.

  10. #10
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    sorry, really stupid question, how do you println in a graphics page?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    Debugging println output goes to the console.
    In a browser, the output will go to the browser's java console.

  12. #12
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    Sigh, Well i have a default statement and that's not being executed. I changed my switch statements to if statements, and that still wasn't fixing it. i have a graphic element that's a a 50 by 50 blue square located at 50, 50 to do my debugging. I can get it to show up inside the loop, but not in the switch statement. Oh and a quick loop question, if you have a loop inside a loop. the external loop goes once, then the internal loop goes for the desired length, then the external goes once and then the internal loop runs all the way through, and the process repeats until the external loop requirements is full filled right?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    the external goes once and then the internal loop runs all the way
    the code inside of the external loop is executed statement by statement until it reaches the inner loop, Then the code in the inner loop is executed statement by statement until the inner loop ends.Then the code in the outer loop continues executing statement by statement until the end of the loop and then it starts at the top of the loop again.

    What is printed out when you add println statements to your code? the print outs will show where the execution goes. If you do not see print outs, then the code was not executed. Add more printlns until you see them and understand how the code is executing.

  14. #14
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    whats the exact syntax of adding printlns to a graphics page? is it the same as a system.out.println? or is it something different. sorry for the basic questions

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    System.out.println()s do NOT print on a graphics page. They go to the console.
    I was being lazy and not typing: System.out.

  16. #16
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    Im still so confused, could you provide a code example ?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    I can't believe you have never coded a System.out.println() statement??? Here is a sample:

    System.out.println("some message here" + aVariable here);

  18. #18
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    No no i have, just never in a graphics page. Im confused on how i use a system.out.println in a graphics page, it gives the error "Package system does not exist"

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: HELP. Getting an error returning a 2d array

    Java is case sensitive. There is no package named system. Its name is System.
    Do you see any lower case s on any of my postings?

  20. #20
    Orangepillow is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: HELP. Getting an error returning a 2d array

    wow, im stupid, ya that makes a lot more sense. How do i access my java console from the compiler, im using bluej

Page 1 of 2 12 LastLast

Similar Threads

  1. Returning Array Row
    By Zocheyado in forum New To Java
    Replies: 2
    Last Post: 04-15-2011, 02:23 AM
  2. returning double array
    By Billaguana in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 03:59 AM
  3. returning array
    By aizen92 in forum New To Java
    Replies: 4
    Last Post: 01-08-2011, 03:10 PM
  4. Syntax error when returning an array
    By wantakill in forum New To Java
    Replies: 2
    Last Post: 09-03-2010, 06:39 PM
  5. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:43 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
  •