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

Thread: Assign variable to coordinate pair for GUI

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assign variable to coordinate pair for GUI

    I have a GUI grid of squares that I prompt the user for the size of (2^n). Then I ask for the coordinates to put a white square or 'hole' in the grid. I am having trouble putting the user inputted 'hole' into my grid and coloring it white.

    My program prompts the user for a power of 2 grid, and displays it. But I've been trying for a couple hours to try and put the 'hole' into my grid and can't seem to get it.
    How how do I take these coordinates and place the specified coordinates as a white square?

    Here is my code:
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    class trominoWarZ{
            private int currentNum;
            private int[][] grid;
        public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in);
     
            System.out.println("What size board do you want?");
            double pw = input.nextDouble();
     
            System.out.println("Coordinates of missing square?");
            int x = input.nextInt();
            int y = input.nextInt(); 
     
            myHole hole = new myHole(x,y);     //WANT TO SET x,y coord to WHITE 
     
            super.drawHole(g,x,y);      
     
            myPan panel = new myPan(pw);      //this is how it reads size of board
     
            JFrame application = new JFrame();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            application.add(panel);           
            application.setSize(400, 400);
            application.setVisible(true); 
        }
    }
     
    class myPan extends JPanel{
     
        public double pow;
        public double hc;
     
        public static void drawHole(Graphics g, int x, int y) {    
        g.setColor(Color.WHITE);
        g.fillRect(g,x,y);
     
        }
        public myPan(double p){
            pow = p;
        }
     
        public myHole(int h){
            hc = h;                     //I will have the var hc to use for the hole
            drawHole(hc);
        }    
        public void paintComponent(Graphics g){
     
            super.paintComponent(g);   
            double num = Math.pow(2,pow);
            double across;
            double up;
            if(hc % 2 == 0){ //is a square
                across = Math.hc(num,0.5);
                up = across;
            }
            else{
                double x = Math.floor(hc/2);
                double y = x + 1;
                across = Math.hc(2,x);
                up = Math.hc(2,y);
            }
     
            if(pow % 2 == 0){ //is a square
                across = Math.pow(num,0.5);
                up = across;
            }
            else{
                double x = Math.floor(pow/2);
                double y = x + 1;
                across = Math.pow(2,x);
                up = Math.pow(2,y);
            }
            double wid = 400/across; //width of one
            double hi = 400/up; //height of one
            double nowX = 0;
            double nowY = 0;
            for(int i = 0; i < up; i++){ //top to bottom
                nowX = 0;
                for(int j = 0; j < across; j++){
                    //System.out.print("*");
                    g.setColor(Color.BLACK);
                    g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                    nowX = nowX + wid;
                }
                nowY = nowY + hi;
                //System.out.print("\n");
            }
     
            //grid or whatever = g.fill
        }
        }
     
     
     
    //     public static void tromino(int size, int x, int y) {
    //             int actualsize = 1;
    //             while (actualsize < size) actualsize*=2;
    // }
    Last edited by jcfor3ver; November 10th, 2013 at 04:26 AM. Reason: posting code properly for forum?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assign variable to coordinate pair for GUI

    Please read the Announcement topic at the top of the sub-forum to learn how to post your code correctly along with other useful info for new forum members. I'll be looking at your code while you fix your post.

    Edit:

    I recommend you do something like:

    For user desired grid of N x N
    Create a JFrame to be the top-level container
    Use GridLayout of N x N as the JFrame's layout manager
    Create an N x N array in which to store JPanels
    Set the default background for the N x N JPanels to something other than white
    Fill the N x N array with JPanels
    Fill the grids in the JFrame with JPanels from the N x N array
    Set the background of the JPanel at ( x, y ) white.

    Good luck!

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assign variable to coordinate pair for GUI

    Hey Greg, I did not see the Announcement topic, but did edit my question with a link to my code, is that ok for the forum? And thanks for any help you can provide, I updated my code in my link, but it's still not working correctly.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assign variable to coordinate pair for GUI

    Here's the Announcement topic.

    Including your code directly in your post is preferred. Many will not follow external links to see code. There's too much spam, and code stored elsewhere may change or disappear which is not useful to others reading the forum.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assign variable to coordinate pair for GUI

    I edited my code to fit the standard posting. Anyway, in my code I already have the user desired grid of N x N, already have the JFrame to be the top-level container. But then you say to set the default background for the N x N JPanels to something other than white?

    Would that be like:

    panel.setBackground(Color.BLACK);

    Could you provide any code to help? Not asking you to solve it, I am just a complete newb and am still getting even the most basic Swing concepts down.
    I guess I do not understand from my code how to change it to use GridLayout of N x N as the layout manager, and then go forth from that. I am just hesitant to change it too much since it took me a while to get it to where it was (again I am a newb).

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Assign variable to coordinate pair for GUI

    A "newb" (newbie, etc., etc. - all terms I despise, because they're meant as an excuse) should be willing and eager to change his/her code for the better. Hopefully, big changes result in big improvements or provide big lessons, which are not always the same thing. Apparently, your code does not do what you want it to, so changes are inevitable.

    Review the Java Tutorial Lessons on LayoutManagers. In fact, if you're new to Swing, the entire Swing tutorial should be useful, but ignore the bias towards using the Netbeans GUI Builder and first learn to code graphical interfaces by hand. If you prefer videos, the New Boston series is highly rated, but I have neither the patience nor bandwidth to try them myself.

    You've made other comments in your code and post that are confusing:

    I prompt the user for the size of (2^n) . . . and . . . My program prompts the user for a power of 2 grid
    I think you mean you prompt the user for one side of a square grid, or I do not understand what you're trying to do.
    if(hc % 2 == 0){ //is a square
    That's not a valid check for a perfect square.
        public static void drawHole(Graphics g, int x, int y)
        {    
            g.setColor(Color.WHITE);
            g.fillRect(g,x,y);
        }
    This code suggests you were intending to fill a rectangle in a single JPanel at the coordinates ( x, y ). That is possible, but not in this way. If you'd like to pursue this approach further, make that clear.

    There are several syntax or technical errors throughout your code. Surely you're getting errors when you try to compile. You should deal with those as best you can and then post those you'd like help with.

Similar Threads

  1. how to pair bluetooth devices....
    By kumar.dinesh7 in forum Java SE APIs
    Replies: 0
    Last Post: March 11th, 2013, 02:39 AM
  2. How to find Object in coordinate system
    By Eldarmk2 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 9th, 2012, 11:09 PM
  3. Clicking a coordinate on my GUI (Not Local Mouse Or Normal Robot.movemouse!)
    By testingjava in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 30th, 2012, 06:28 AM
  4. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  5. Another pair of eyes.
    By Ripcurl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2010, 10:49 AM

Tags for this Thread