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 11 of 11

Thread: java grid and colors/graphics

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question java grid and colors/graphics

    okay...
    i have a working code (from WikiHOW- this is only a test, so i will be making extreme alteration to this code, but i needed a base to at least start...) about creating a grid:
    but when i try to alter it to have colors instead of numbers, it gets confused

    i know i need to
    import java.awt.color.*;
    import java.awt.Color;
    But eclipse keeps telling me that i cant put the color "BLUE" into the code
    import javax.swing.JFrame; //imports JFrame library
    import javax.swing.JButton; //imports JButton library
    import java.awt.GridLayout; //imports GridLayout library
     
    //Add import java.awt.color.*;
    //import java.awt.Color;
     
     
    public class grid_test_01{
            JFrame frame=new JFrame(); //creates frame
            JButton[][] grid; //names the grid of buttons
     
            public grid_test_01(int width, int length){ //constructor
                    frame.setLayout(new GridLayout(width,length)); //set layout
                    grid=new JButton[width][length]; //allocate the size of grid
                    for(int y=0; y<length; y++){ 
                            for(int x=0; x<width; x++){
                                    grid[x][y]=new JButton("("+x+","+y+")"); //creates new button <-- this is where i try to change to grid[x][y]=new color.BLUE, but i get errors   
                                    frame.add(grid[x][y]); //adds button to grid
                            }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); //makes frame visible
            }
            public static void main(String[] args) {
                    new grid_test_01(25,25);//makes new Grid size (x,y)
            }
    }

    my eventual need for this code is to create a grid filled with random graphics that are edge dependent (at (0,0) type 1,2,3,4 etc can be chosen, but at (0,1) if type 1 is at (0,0) then only type 1 or 2 could be chosen etc)


    Oh, if i posted in the wrong forum, sorry, i couldn't really find one that matched my question 100%....
    Last edited by ajmukon; January 24th, 2011 at 01:43 PM.


  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: java grid and colors/graphics

    What exactly is the error you're getting? Copy and paste the whole stack trace here.

    What exactly is the line you're trying to run when you get the error?

    I'm not exactly sure what you're trying to do. Are you trying to have the JButtons display a color? Or the name of a Color? Something else?
    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
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    QUOTE=KevinWorkman;25353]What exactly is the error you're getting? Copy and paste the whole stack trace here.

    What exactly is the line you're trying to run when you get the error?

    I'm not exactly sure what you're trying to do. Are you trying to have the JButtons display a color? Or the name of a Color? Something else?[/QUOTE]
    import javax.swing.JFrame; //imports JFrame library
    import javax.swing.JButton; //imports JButton library
    import java.awt.GridLayout; //imports GridLayout library
    import java.awt.color.*;
    import java.awt.Color;
     
     
    public class grid_test_01{
            JFrame frame=new JFrame(); //creates frame
            JButton[][] grid; //names the grid of buttons
     
            public grid_test_01(int width, int length){ //constructor
                    frame.setLayout(new GridLayout(width,length)); //set layout
                    grid=new color[width][length]; //allocate the size of grid
                    for(int y=0; y<length; y++){
                            for(int x=0; x<width; x++){
                                    grid[x][y]=new color.BLUE
                                    frame.add(grid[x][y]); //adds button to grid
                            }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); //makes frame visible
            }
            public static void main(String[] args) {
                    new grid_test_01(25,25);//makes new Grid size (x,y)
            }
    }

    my limited understanding of JAVA tells me this should "work" (granted, i only have programmed C++ for basic math/ non GUI text based algorithms)

    My Goal:
    I would like the grid to display a graphic, but a basic color would work (blue, green, etc)
    I can adapt that to do what i want.

    My ultimate goal would be to create a grid X by Y in size that creates a random "map" for a simple 2D android game i would like to try to create
    Last edited by ajmukon; January 24th, 2011 at 04:54 PM.
    trying to make a JAVA based Andriod Game

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: java grid and colors/graphics

    Grid is a 2D array of JButtons. You assigning a 2D array of color objects to it. This will cause a compiler error.

    You import Color and color. I'm pretty sure you want to use Color (you are using color in your code). There are errors here too.

    What I think you are trying to do, is create a new 2D array of JButtons, then inside the for loops, create a new JButton, and set the background color of that JButton.

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

    ajmukon (January 24th, 2011)

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    Quote Originally Posted by DavidFongs View Post
    Grid is a 2D array of JButtons. You assigning a 2D array of color objects to it. This will cause a compiler error.

    You import Color and color. I'm pretty sure you want to use Color (you are using color in your code). There are errors here too.

    What I think you are trying to do, is create a new 2D array of JButtons, then inside the for loops, create a new JButton, and set the background color of that JButton.
    infinitely more helpful than searching for" java colors tutorial"

    whats the difference between Color and color? Since i don't know the difference, i imported them both.

    i understand the for do loop, so it would be:
    import javax.swing.JFrame; //imports JFrame library
    import javax.swing.JButton; //imports JButton library
    import java.awt.GridLayout; //imports GridLayout library
    import java.awt.Color;
     
     
    public class grid_test_01{
            JFrame frame=new JFrame(); //creates frame
            JButton[][] grid; //names the grid of buttons
     
            public grid_test_01(int width, int length){ //constructor
                    frame.setLayout(new GridLayout(width,length)); //set layout
                    grid=new JButton[width][length]; //allocate the size of grid
                    for(int y=0; y<length; y++){
                            for(int x=0; x<width; x++){
                                    grid[x][y]=new JButton(Color= BLUE)//<--CHANGE!
                                    frame.add(grid[x][y]); //adds button to grid
                            }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); //makes frame visible
            }
            public static void main(String[] args) {
                    new grid_test_01(25,25);//makes new Grid size (x,y)
            }
    }
    trying to make a JAVA based Andriod Game

  7. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: java grid and colors/graphics

    you need to change grid = new color[width][length] so that it is creating a 2d array of JButtons. Right now it is trying to create a 2d array of colors

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    Quote Originally Posted by DavidFongs View Post
    you need to change grid = new color[width][length] so that it is creating a 2d array of JButtons. Right now it is trying to create a 2d array of colors
    Quote Originally Posted by ajmukon View Post
    import javax.swing.JFrame; //imports JFrame library
    import javax.swing.JButton; //imports JButton library
    import java.awt.GridLayout; //imports GridLayout library
    import java.awt.Color;
     
     
    public class grid_test_01{
            JFrame frame=new JFrame(); //creates frame
            JButton[][] grid; //names the grid of buttons
     
            public grid_test_01(int width, int length){ //constructor
                    frame.setLayout(new GridLayout(width,length)); //set layout
                    grid=new Color[width][length]; //allocate the size of grid//<-- CHANGED
                    for(int y=0; y<length; y++){
                            for(int x=0; x<width; x++){
                                    grid[x][y]=new JButton(Color= BLUE)//<--CHANGED!
                                    frame.add(grid[x][y]); //adds button to grid
                            }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); //makes frame visible
            }
            public static void main(String[] args) {
                    new grid_test_01(25,25);//makes new Grid size (x,y)
            }
    }
    is this correct?
    my message needs to be 10 characters long and i cant think of anything to say...
    I have yet to run this code in eclipse, but i will try after i am sure it is 80% of the way there

    i will run this code soon.. and post back with any errors that come up
    Last edited by ajmukon; January 24th, 2011 at 09:37 PM.
    trying to make a JAVA based Andriod Game

  9. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    import javax.swing.JFrame; //imports JFrame library
    import javax.swing.JButton; //imports JButton library
    import java.awt.GridLayout; //imports GridLayout library
    import java.awt.Color;
     
     
    public class grid_test_01{
            JFrame frame=new JFrame(); //creates frame
            Color[][] grid; //names the grid of buttons //AUTO CHANGE JButton[][] grid -->Color[][] grid
     
            public grid_test_01(int width, int length){ //constructor
                    frame.setLayout(new GridLayout(width,length)); //set layout
                    grid=new Color[width][length]; //allocate the size of grid
                    for(int y=0; y<length; y++){
                            for(int x=0; x<width; x++){
                                    grid[x][y]=new JButton(Color= BLUE)
                                    frame.add(grid[x][y]);//  frame.add brocken
                            }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); //makes frame visible
            }
            public static void main(String[] args) {
                    new grid_test_01(25,25);//makes new Grid size (x,y)
            }
    }
    Last edited by ajmukon; January 24th, 2011 at 09:44 PM. Reason: Ever got that feeeling you forgot something? like a /tag?
    trying to make a JAVA based Andriod Game

  10. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    the code will not run, comments above note were the problems are.
    trying to make a JAVA based Andriod Game

  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: java grid and colors/graphics

    Quote Originally Posted by ajmukon View Post
    is this correct?
    my message needs to be 10 characters long and i cant think of anything to say...
    I have yet to run this code in eclipse, but i will try after i am sure it is 80% of the way there
    That's not really a great way to program. You should test each small step before moving on to the next one. Waiting until you have a bunch of different pieces added will only cause you to deal with multiple errors at once, which is what you're doing.

    Honestly, what you're doing is overkill. I'd recommend you first go back through the basic tutorials, as you're making syntax errors that don't make any sense. For example, what method takes an assignment statement as an argument? What constructor of JButton takes a Color argument? Why are you using JButtons for this anyway? Read through this: Trail: Learning the Java Language (The Java™ Tutorials)

    When you have a handle on the basic syntax, write some simple test programs to practice. You can probably even start on the logic of whatever you're trying to do, without worrying about the GUI part of it.

    Only after you have a firm grasp on that, then you should worry about the GUI. Read through the Swing tutorials: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    And for what you're doing, I'd recommend the painting tutorials: Painting in AWT and Swing

    Programming is hard. You can't just expect to dive into GUI programming without a firm grasp of the basics first. You probably don't like that answer, but it's the correct one.
    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. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ajmukon (January 25th, 2011)

  13. #11
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java grid and colors/graphics

    Quote Originally Posted by KevinWorkman View Post
    That's not really a great way to program. You should test each small step before moving on to the next one. Waiting until you have a bunch of different pieces added will only cause you to deal with multiple errors at once, which is what you're doing.

    Honestly, what you're doing is overkill. I'd recommend you first go back through the basic tutorials, as you're making syntax errors that don't make any sense. For example, what method takes an assignment statement as an argument? What constructor of JButton takes a Color argument? Why are you using JButtons for this anyway? Read through this: Trail: Learning the Java Language (The Java™ Tutorials)

    When you have a handle on the basic syntax, write some simple test programs to practice. You can probably even start on the logic of whatever you're trying to do, without worrying about the GUI part of it.

    Only after you have a firm grasp on that, then you should worry about the GUI. Read through the Swing tutorials: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    And for what you're doing, I'd recommend the painting tutorials: Painting in AWT and Swing

    Programming is hard. You can't just expect to dive into GUI programming without a firm grasp of the basics first. You probably don't like that answer, but it's the correct one.
    impressively enough, i have some grasp of the basics, at least in C++, and i have been told that java isn't all that different (if/then, for/do, do/while are the same)

    but thanks for the links, i will read up on them!... I tried searching for "JAVA tutorials", and got nowhere real fast...

    and thank you for pointing out my exact errors.
    trying to make a JAVA based Andriod Game

Similar Threads

  1. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  2. JTable prepareRenderer changes all cell colors
    By BrettStuart in forum AWT / Java Swing
    Replies: 9
    Last Post: July 21st, 2010, 02:51 AM
  3. Perlin Noise (and other Java 2d graphics questions)
    By helloworld922 in forum AWT / Java Swing
    Replies: 3
    Last Post: June 11th, 2010, 07:42 PM
  4. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 05:16 PM
  5. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM