如何让自定义绘制的组件根据新数据进行更改?
How to get custom painted component to change according to new data?
我正在使用 netbeans 8.0 在 Java GUI 中开发 application/game。在这个游戏中你有一个非常简单的U.I。那是一张意大利半岛的地图(GIF 图像),里面有一个标签、一个开始按钮和几个用于调试的标签。
在这个游戏中你有意大利最大城市的所有坐标(纬度和经度),当有人点击开始按钮时会随机提取一个城市,所以城市的名字会出现在标签中并且用户必须猜测那个城市的位置。现在我解决了地理坐标和像素之间的所有转换问题,并且我的数据模型工作正常,因此您不必担心所有这些问题。
当用户点击地图猜测城市的位置时,我想用红点标记城市的正确位置,这就是我所有的问题......我会post 一些代码更清晰 ;)
private void lbMappaMouseClicked(java.awt.event.MouseEvent evt) {
//gets the pixels coordinates of the user click
mouseX = evt.getX();
mouseY = evt.getY();
//casting datas for coordinates
double cLon = 12.565006;
double cLat = 42.094436;
double w = 6.626604;
double e = 18.520248;
double n = 47.091932;
double s = 36.646879;
double kLat = 0.97;
double kLon = 1;
//earth radius
double R = 6371;
//formula to convert the coordinates of the click to geographical lon,
double mouseLon = cLon + (mouseX - dimX / 2) * (e - cLon) / kLon / dimX * 2;
//formula to convert the coordinates of the click to geo lat
double mouseLat = cLat + (mouseY - dimY / 2) * (s - cLat) / kLat / dimY * 2;
//this is the formula to find the pixel coordinates of the extracted city
comuneX = (int) (((longitudine - cLon) / (e - cLon) / kLon / dimX * 2) + dimX / 2);
comuneY = (int) (((latitudine - cLat) / (s - cLat) / kLat / dimY * 2) + dimY / 2);
//EVERY TIME I CALCULATE THIS STUFF ABOVE I WANTED TO DRAW A RED DOT/OVAL ON THE MAP
double lonA = Math.toRadians(mouseLon);
double latA = Math.toRadians(mouseLat);
double lonB = Math.toRadians(longitudine);
double latB = Math.toRadians(latitudine);
double distanza = R * Math.acos(Math.sin(latA) * Math.sin(latB) + Math.cos(latA) * Math.cos(latB) * Math.cos(lonA - lonB));
lonA = Math.toRadians(e);
latA = Math.toRadians(n);
lonB = Math.toRadians(w);
latB = Math.toRadians(s);
//distance between the correct position of the city and the user click
double distanzaMax = R * Math.acos(Math.sin(latA) * Math.sin(latB) + Math.cos(latA) * Math.cos(latB) * Math.cos(lonA - lonB));
lbDistanza.setText(Double.toString(distanza));
}
我的 class 扩展了 JFrame,我试图重写 paint 方法,但我发现它只在程序第一次呈现地图时有效。
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(comuneX, comuneY, comuneX, comuneY);
}
每次用户单击地图上的一个点时,如何调用此绘制方法?有没有其他选择?
感谢您的宝贵时间:)
尝试调用 JFrame 上的 validate() 和 repaint() 方法来重绘组件。
frame.getContentPane().validate();
frame.getContentPane().repaint();
我正在使用 netbeans 8.0 在 Java GUI 中开发 application/game。在这个游戏中你有一个非常简单的U.I。那是一张意大利半岛的地图(GIF 图像),里面有一个标签、一个开始按钮和几个用于调试的标签。
在这个游戏中你有意大利最大城市的所有坐标(纬度和经度),当有人点击开始按钮时会随机提取一个城市,所以城市的名字会出现在标签中并且用户必须猜测那个城市的位置。现在我解决了地理坐标和像素之间的所有转换问题,并且我的数据模型工作正常,因此您不必担心所有这些问题。
当用户点击地图猜测城市的位置时,我想用红点标记城市的正确位置,这就是我所有的问题......我会post 一些代码更清晰 ;)
private void lbMappaMouseClicked(java.awt.event.MouseEvent evt) {
//gets the pixels coordinates of the user click
mouseX = evt.getX();
mouseY = evt.getY();
//casting datas for coordinates
double cLon = 12.565006;
double cLat = 42.094436;
double w = 6.626604;
double e = 18.520248;
double n = 47.091932;
double s = 36.646879;
double kLat = 0.97;
double kLon = 1;
//earth radius
double R = 6371;
//formula to convert the coordinates of the click to geographical lon,
double mouseLon = cLon + (mouseX - dimX / 2) * (e - cLon) / kLon / dimX * 2;
//formula to convert the coordinates of the click to geo lat
double mouseLat = cLat + (mouseY - dimY / 2) * (s - cLat) / kLat / dimY * 2;
//this is the formula to find the pixel coordinates of the extracted city
comuneX = (int) (((longitudine - cLon) / (e - cLon) / kLon / dimX * 2) + dimX / 2);
comuneY = (int) (((latitudine - cLat) / (s - cLat) / kLat / dimY * 2) + dimY / 2);
//EVERY TIME I CALCULATE THIS STUFF ABOVE I WANTED TO DRAW A RED DOT/OVAL ON THE MAP
double lonA = Math.toRadians(mouseLon);
double latA = Math.toRadians(mouseLat);
double lonB = Math.toRadians(longitudine);
double latB = Math.toRadians(latitudine);
double distanza = R * Math.acos(Math.sin(latA) * Math.sin(latB) + Math.cos(latA) * Math.cos(latB) * Math.cos(lonA - lonB));
lonA = Math.toRadians(e);
latA = Math.toRadians(n);
lonB = Math.toRadians(w);
latB = Math.toRadians(s);
//distance between the correct position of the city and the user click
double distanzaMax = R * Math.acos(Math.sin(latA) * Math.sin(latB) + Math.cos(latA) * Math.cos(latB) * Math.cos(lonA - lonB));
lbDistanza.setText(Double.toString(distanza));
}
我的 class 扩展了 JFrame,我试图重写 paint 方法,但我发现它只在程序第一次呈现地图时有效。
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(comuneX, comuneY, comuneX, comuneY);
}
每次用户单击地图上的一个点时,如何调用此绘制方法?有没有其他选择? 感谢您的宝贵时间:)
尝试调用 JFrame 上的 validate() 和 repaint() 方法来重绘组件。
frame.getContentPane().validate();
frame.getContentPane().repaint();