我正在尝试制作一个教育游戏,孩子们会问一个省的位置,就像下面的链接一样。我遇到了一个问题,我不知道如何告诉程序他是否打出了正确的形状。 如果你们能给我一些关于如何解决这个问题的建议,我将非常感激。
http://online.seterra.net/en/vgp/3006
这是我到目前为止所做的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class CPTprograss2 extends JPanel
{
static Polygon[] polygons;
Polygon selected;
static String province = "";
public CPTprograss2 ()
{
polygons = new Polygon [13];
int[] bcx = {95, 78, 108, 26, 11, 16, 41};
int[] bcy = {267, 204, 141, 87, 161, 208, 241};
polygons [0] = new Polygon (bcx, bcy, 7);
int[] abx = {96, 124, 157, 110, 81};
int[] aby = {258, 277, 160, 142, 196};
polygons [1] = new Polygon (abx, aby, 5);
int[] skx = {126, 185, 201, 160};
int[] sky = {277, 290, 174, 160};
polygons [2] = new Polygon (skx, sky, 4);
int[] mnx = {187, 232, 232, 275, 253, 243, 206};
int[] mny = {291, 294, 258, 215, 195, 180, 175};
polygons [3] = new Polygon (mnx, mny, 7);
int[] onx = {233, 265, 287, 310, 343, 337, 329, 355, 391, 363, 348, 341, 321, 299, 280, 235};
int[] ony = {295, 309, 301, 327, 333, 344, 373, 359, 328, 324, 316, 274, 256, 231, 215, 259};
polygons [4] = new Polygon (onx, ony, 16);
int[] qbx = {349, 374, 412, 425, 431, 445, 470, 488, 432, 422, 413, 433, 409, 396, 382, 380, 357, 334, 343, 340, 361, 341, 346, 344, 347};
int[] qby = {319, 329, 323, 295, 285, 279, 259, 219, 240, 236, 210, 201, 161, 174, 167, 155, 145, 148, 174, 187, 214, 234, 266, 274, 313};
polygons [5] = new Polygon (qbx, qby, 25);
int[] nflx = {414, 420, 437, 490, 492, 538, 492, 445};
int[] nfly = {213, 232, 238, 217, 266, 251, 201, 186};
polygons [6] = new Polygon (nflx, nfly, 8);
int[] peix = {461, 474, 475, 464};
int[] peiy = {290, 287, 292, 293};
polygons [7] = new Polygon (peix, peiy, 4);
int[] nox = {424, 432, 454, 457, 464, 459, 445, 436, 436, 428};
int[] noy = {299, 285, 285, 291, 298, 307, 316, 303, 298, 298};
polygons [8] = new Polygon (nox, noy, 10);
int[] nbx = {455, 462, 472, 488, 493, 488, 486, 481, 468, 469, 463, 457};
int[] nby = {325, 328, 312, 297, 287, 282, 290, 296, 300, 308, 312, 320};
polygons [9] = new Polygon (nbx, nby, 12);
int[] ykx = {18, 39, 72, 89, 94, 84, 82, 89, 86, 92, 95, 90, 101, 88, 19};
int[] yky = {73, 97, 119, 129, 121, 113, 90, 73, 61, 51, 40, 34, 19, 8, 71};
polygons [10] = new Polygon (ykx, yky, 15);
int[] nwtx = {90, 112, 163, 205, 214, 172, 146, 151, 176, 208, 221, 219, 195, 184, 177, 153, 146, 140, 119, 104, 92, 96, 94, 89, 90, 86, 85, 87, 95, 93};
int[] nwty = {129, 146, 161, 172, 122, 102, 58, 50, 52, 60, 37, 26, 18, 20, 36, 42, 38, 28, 22, 20, 35, 45, 53, 58, 75, 85, 103, 114, 120, 126};
polygons [11] = new Polygon (nwtx, nwty, 30);
int[] nnx = {205, 244, 255, 265, 259, 290, 301, 305, 314, 316, 331, 346, 361, 389, 395, 393, 372, 392, 392, 384, 375, 361, 350, 338, 317, 299, 287, 259, 242, 220, 208, 155, 147, 172, 216, 207};
int[] nny = {173, 178, 154, 143, 134, 139, 142, 151, 148, 135, 124, 119, 131, 137, 126, 114, 97, 98, 89, 81, 75, 63, 52, 46, 37, 28, 28, 24, 27, 39, 60, 50, 56, 100, 121, 171};
polygons [12] = new Polygon (nnx, nny, 36);
}
public static void main (String[] args)
{
int max = 13;
int drawNum[] = new int [max];
drawNum [0] = (int) (Math.random () * max) + 0;
while (drawNum [1] == drawNum [0])
{
drawNum [1] = (int) (Math.random () * max) + 0;
}
while ((drawNum [2] == drawNum [0]) || (drawNum [2] == drawNum [1]))
{
drawNum [2] = (int) (Math.random () * max) + 0;
}
while ((drawNum [3] == drawNum [0]) || (drawNum [3] == drawNum [1]) || (drawNum [3] == drawNum [2]))
{
drawNum [3] = (int) (Math.random () * max) + 0;
}
while ((drawNum [4] == drawNum [0]) ||
(drawNum [4] == drawNum [1]) ||
(drawNum [4] == drawNum [2]) ||
(drawNum [4] == drawNum [3]))
{
drawNum [4] = (int) (Math.random () * max) + 0;
}
while ((drawNum [5] == drawNum [0]) ||
(drawNum [5] == drawNum [1]) ||
(drawNum [5] == drawNum [2]) ||
(drawNum [5] == drawNum [3]) ||
(drawNum [5] == drawNum [4]))
{
drawNum [5] = (int) (Math.random () * max) + 0;
}
while ((drawNum [6] == drawNum [0]) || (drawNum [6] == drawNum [1]) || (drawNum [6] == drawNum [2]) || (drawNum [6] == drawNum [3]) ||
drawNum [6] == drawNum [4] || drawNum [6] == drawNum [5])
{
drawNum [6] = (int) (Math.random () * max) + 0;
}
while ((drawNum [7] == drawNum [0]) || (drawNum [7] == drawNum [1]) || (drawNum [7] == drawNum [2]) || (drawNum [7] == drawNum [3]) ||
drawNum [7] == drawNum [4] || drawNum [7] == drawNum [5] || drawNum [7] == drawNum [6])
{
drawNum [7] = (int) (Math.random () * max) + 0;
}
while ((drawNum [8] == drawNum [0]) || (drawNum [8] == drawNum [1]) || (drawNum [8] == drawNum [2]) || (drawNum [8] == drawNum [3]) ||
drawNum [8] == drawNum [4] || drawNum [8] == drawNum [5] || drawNum [8] == drawNum [6] || drawNum [8] == drawNum [7])
{
drawNum [8] = (int) (Math.random () * max) + 0;
}
while ((drawNum [9] == drawNum [0]) || (drawNum [9] == drawNum [1]) || (drawNum [9] == drawNum [2]) || (drawNum [9] == drawNum [3]) || drawNum [9] == drawNum [4] ||
drawNum [9] == drawNum [5] || drawNum [9] == drawNum [6] || drawNum [9] == drawNum [7] || drawNum [9] == drawNum [10])
{
drawNum [9] = (int) (Math.random () * max) + 0;
}
while ((drawNum [10] == drawNum [0]) || (drawNum [10] == drawNum [1]) || (drawNum [10] == drawNum [2]) || (drawNum [10] == drawNum [3]) || drawNum [10] == drawNum [4] ||
drawNum [10] == drawNum [5] || drawNum [10] == drawNum [6] || drawNum [10] == drawNum [7] || drawNum [10] == drawNum [8] || drawNum [10] == drawNum [9])
{
drawNum [10] = (int) (Math.random () * max) + 0;
}
while ((drawNum [11] == drawNum [0]) || (drawNum [11] == drawNum [1]) || (drawNum [11] == drawNum [2]) || (drawNum [11] == drawNum [3]) || drawNum [11] == drawNum [4] ||
drawNum [11] == drawNum [5] || drawNum [11] == drawNum [6] || drawNum [11] == drawNum [7] || drawNum [11] == drawNum [8] || drawNum [11] == drawNum [9] || drawNum [11] == drawNum [10])
{
drawNum [11] = (int) (Math.random () * max) + 0;
}
while ((drawNum [12] == drawNum [0]) || (drawNum [12] == drawNum [1]) || (drawNum [12] == drawNum [2]) || (drawNum [12] == drawNum [3]) || drawNum [12] == drawNum [4] ||
drawNum [12] == drawNum [5] || drawNum [12] == drawNum [6] || drawNum [12] == drawNum [7] || drawNum [12] == drawNum [8] || drawNum [12] == drawNum [9] || drawNum [12] == drawNum [11] || drawNum [12] == drawNum [11])
{
drawNum [12] = (int) (Math.random () * max) + 0;
}
String state[] = new String [13];
state [0] = "British Columbia";
state [1] = "Alberta";
state [2] = "Saskatchewan";
state [3] = "Manitoba";
state [4] = "Ontario";
state [5] = "Quebec";
state [6] = "Newfoundland and Labrador";
state [7] = "Prince Edward Island";
state [8] = "New Brunswick";
state [9] = "Nova Scotia";
state [10] = "Yukon";
state [11] = "Northwest Territories";
state [12] = "Nunavut";
Selecting coordinates = new Selecting ();
Selector selector = new Selector (coordinates);
coordinates.addMouseListener (selector);
JFrame f = new JFrame ("Name the Province");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
for (int v = 0 ; v < 13 ; v++)
{
JTextArea txt = new JTextArea (15, 20);
txt.setText ("Where is " + state [drawNum [v]] + "?");
//System.out.println(state[drawNum[v]]);
txt.setEditable (false);
//txt.setLineWrap (true);
txt.setBounds (30, 400, 300, 60);
f.getContentPane ().add (txt);
if (state [drawNum [v]].equals (state[drawNum[v]]))
{
System.out.println ("correct");
}
}
f.getContentPane ().add (coordinates);
f.setSize (560, 600);
f.setLocation (100, 100);
f.setVisible (true);
}
protected void draw (Graphics g)
{
//super.paintComponent (g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int j = 0 ; j < polygons.length ; j++)
{
Color color = Color.blue;
if (polygons [j] == selected)
color = Color.red;
g2.setPaint (color);
g2.draw (polygons [j]);
}
}
Selecting selecting;
public void Selector (Selecting s)
{
selecting = s;
}
public void mousePressed (MouseEvent e)
{
Point p = e.getPoint ();
Polygon[] polygons = selecting.polygons;
for (int j = 0 ; j < polygons.length ; j++)
{
if (polygons [0].contains (p))
{
//selecting.setSelection (polygons [0]);
//System.out.println ("British Columbia");
province = "British Columbia";
break;
}
else if (polygons [1].contains (p))
{
//System.out.println ("Alberta");
province = "Alberta";
break;
}
else if (polygons [2].contains (p))
{
//System.out.println ("Saskatchewan");
province = "Saskatchewan";
break;
}
else if (polygons [3].contains (p))
{
//System.out.println ("Manitoba");
province = "Manitoba";
break;
}
else if (polygons [4].contains (p))
{
//System.out.println ("Ontario");
province = "Ontario";
break;
}
else if (polygons [5].contains (p))
{
//System.out.println ("Quebec");
province = "Quebec";
break;
}
else if (polygons [6].contains (p))
{
//System.out.println ("Newfoundland and Labrador");
province = "Newfoundland and Labrador";
break;
}
else if (polygons [7].contains (p))
{
//System.out.println ("Prince Edward Island");
province = "Prince Edward Island";
break;
}
else if (polygons [8].contains (p))
{
//System.out.println ("New Brunswick");
province = "New Brunswick";
break;
}
else if (polygons [9].contains (p))
{
//System.out.println ("Nova Scotia");
province = "Nova Scotia";
break;
}
else if (polygons [10].contains (p))
{
//System.out.println ("Yukon");
province = "Yukon";
break;
}
else if (polygons [11].contains (p))
{
//System.out.println ("Northwest Territories");
province = "Northwest Territories";
break;
}
else if (polygons [12].contains (p))
{
//System.out.println ("Nunavut");
province = "Nunavut";
break;
}
else
{
}
}
}
public void mouseReleased (MouseEvent e)
{
}
}
答案 0 :(得分:0)
此链接有您的答案,只需稍加修改:about drawing a Polygon in java
在paintComponent方法中(可能在您的Selecting类中),使用循环绘制所有多边形。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
for(Polygon poly : polygons) {
g.drawPolygon(poly);
}
}
在mouseClicked处理程序中(可能在你的Selector中),使用循环:
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
for (int i = 0; i < polygons.length; i++) {
if (polygons[i].contains(me.getPoint())) {
System.out.println("Clicked polygon " + i);
break;
}
}
}