Jump to content

How do I make irregular shaped panels? (U.S. states on a map)

- - - - -

  • Please log in to reply
4 replies to this topic

#1
snapple232

snapple232

    Newbie

  • Members
  • Pip
  • 5 posts
I'm making a game that involves the 50 states of the U.S. I want it so that if a player clicks on a point inside the boundaries of a state, that state is selected. How would I go about doing this? All the states are obviously different shapes and sizes, so is there a way to recognize the boundaries of each one?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You'd need to provide all the coordinates so java can draw lines between the points to create a state...
That's an awful lot of work. I think you're better of drawing an image of the US.

Getting to know what state is clicked on might be hard. The easiest solution that comes up to me is:
Make sure the map of the US has every state in a different color.

When you register a click on the map, You can easily get the x and y coordinates of the mouse from the mouseEvent in your mouseClicked(MouseEvent e) method.
Now, Java has a Robot class. It can control the mouse and keyboard to let the computer control it etc. But more important, it has a method getPixelColor(int x, int y).
Then you have the color.. and since each color represents a state it's not too dificult to know which state is clicked. And if the color is black. The user cilcked on a borderline.

The colors don't have to differ much, you can make 50 types of yellow, and it will/should still work.

#3
snapple232

snapple232

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for the response. That's a very creative solution, I like it. Only problem though is that this is a political election game. I need the state colors to be red/blue/purple. Unless theres a way to make the colors vary so slightly that it's imperceptible?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

Unless theres a way to make the colors vary so slightly that it's imperceptible?
Bingo! :D
I made about 1/3th of the states red, all next to eachother:[ATTACH]3415[/ATTACH]
Believe it or not but it are all different colors.

Because i was actually a bit surprised myself to see no difference at all, i decided to make a test app to see if java could see a difference and it does!

These are the RGB values it sees when i click once on every red state:

254, 0, 0

255, 0, 0

255, 1, 0

255, 0, 1

254, 0, 1

254, 1, 1

252, 0, 0

255, 3, 0

254, 1, 0

253, 0, 0

255, 1, 1

255, 0, 3

255, 2, 0

255, 0, 2


test code:

public class UsaPanel extends JPanel{

    Image img;


    public UsaPanel(){

        super();

    }


    public void loadImage(){

        try {

            img = ImageIO.read(new File("bigImagebmp.bmp"));

            setPreferredSize(new Dimension(img.getWidth(this), img.getHeight(this)));

        } catch (IOException ex) {

            Logger.getLogger(UsaPanel.class.getName()).log(Level.SEVERE, null, ex);

        }

    }


    @Override

    public void paint(Graphics g) {

        g.drawImage(img, 0, 0, this);

    }

}



public class UsaFrame extends JFrame{

    private UsaPanel usaPanel;

    Robot robot=null;


    public UsaFrame(){

        super();

        try {

            robot = new Robot();

        } catch (AWTException ex) {

            Logger.getLogger(UsaFrame.class.getName()).log(Level.SEVERE, null, ex);

        }

        usaPanel = new UsaPanel();

        usaPanel.loadImage();


        add(usaPanel);

        addMouseListener(new MouseListener(){


            public void mouseClicked(MouseEvent e) {

                Color c = robot.getPixelColor(e.getXOnScreen(), e.getYOnScreen());

                System.out.println(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());

            }


            public void mouseEntered(MouseEvent e) {

            }


            public void mouseExited(MouseEvent e) {

            }


            public void mousePressed(MouseEvent e) {

            }


            public void mouseReleased(MouseEvent e) {

            }


        });

        setSize(800,600);

        setVisible(true);

    }

    public static void main(String[] args){

        UsaFrame frame = new UsaFrame();

    }

}



#5
snapple232

snapple232

    Newbie

  • Members
  • Pip
  • 5 posts
Very nice!! I was about to just make the game text-based, but this definitely solves it. Thanks for the help :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users