mirror of
https://github.com/nasa/trick.git
synced 2024-12-18 20:57:55 +00:00
Modified display for SIM_wheelbot (#1541)
* modified Java display for SIM_wheelbot so when RUN_mouse is used, images of waypoints stop being drawn when mouse arrives at waypoint. * removed spaces at beginning of each line in SIM_Wheelbot/models/graphics/src/trick/EVDisplay.java
This commit is contained in:
parent
cca7191129
commit
06a1d2bf2e
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Trick
|
* Trick
|
||||||
* 2016 (c) National Aeronautics and Space Administration (NASA)
|
* 2016 (c) National Aeronautics and Space Administration (NASA)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package trick;
|
package trick;
|
||||||
|
|
||||||
@ -23,22 +23,27 @@ import javax.swing.ImageIcon;
|
|||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.FontMetrics;
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author penn
|
* @author penn
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Feature {
|
class Feature {
|
||||||
|
|
||||||
public double north, west;
|
public double north, west;
|
||||||
public double heading;
|
public double heading;
|
||||||
public BufferedImage bufImage;
|
public BufferedImage bufImage;
|
||||||
|
public String img_file;
|
||||||
|
|
||||||
public Feature(double N, double W, double H, String imageFile) {
|
public Feature(double N, double W, double H, String imageFile) {
|
||||||
north = N;
|
north = N;
|
||||||
west = W;
|
west = W;
|
||||||
heading = H;
|
heading = H;
|
||||||
|
img_file = imageFile;
|
||||||
|
|
||||||
Image img = new ImageIcon(imageFile).getImage();
|
Image img = new ImageIcon(imageFile).getImage();
|
||||||
bufImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
|
bufImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
|
||||||
@ -53,6 +58,17 @@ class Feature {
|
|||||||
west = W;
|
west = W;
|
||||||
heading = H;
|
heading = H;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isReached(double N, double W){
|
||||||
|
// Assuming a tolerance of 0.1 for comparison
|
||||||
|
double tolerance = 0.1;
|
||||||
|
|
||||||
|
// Check if the distance between the given N and W values
|
||||||
|
// and the current north and west values of the waypoint
|
||||||
|
// is within the tolerance.
|
||||||
|
double distance = Math.sqrt(Math.pow(N-north,2) + Math.pow(W-west,2));
|
||||||
|
return distance <= tolerance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArenaMap extends JPanel {
|
class ArenaMap extends JPanel {
|
||||||
@ -60,6 +76,10 @@ class ArenaMap extends JPanel {
|
|||||||
private List<Feature> featureList;
|
private List<Feature> featureList;
|
||||||
private double metersPerPixel;
|
private double metersPerPixel;
|
||||||
private Color groundColor;
|
private Color groundColor;
|
||||||
|
private List<Integer> reachedWaypoints = new ArrayList<>();
|
||||||
|
private int waypointIndex = -1;
|
||||||
|
private boolean lastWaypointReached = false;
|
||||||
|
private boolean mouse = false;
|
||||||
|
|
||||||
public ArenaMap(List<Feature> flist, double mapScale) {
|
public ArenaMap(List<Feature> flist, double mapScale) {
|
||||||
featureList = flist;
|
featureList = flist;
|
||||||
@ -68,6 +88,16 @@ class ArenaMap extends JPanel {
|
|||||||
groundColor = new Color(255,255,255);
|
groundColor = new Color(255,255,255);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateReachedWaypoint(int waypointIndex){
|
||||||
|
this.waypointIndex = waypointIndex;
|
||||||
|
reachedWaypoints.add(waypointIndex);
|
||||||
|
|
||||||
|
// Check if the last waypoint has been reached
|
||||||
|
if(waypointIndex == featureList.size() - 2){
|
||||||
|
lastWaypointReached = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SetScale (double mapScale) {
|
public void SetScale (double mapScale) {
|
||||||
if (mapScale < 0.00001) {
|
if (mapScale < 0.00001) {
|
||||||
metersPerPixel = 0.00001;
|
metersPerPixel = 0.00001;
|
||||||
@ -95,6 +125,32 @@ class ArenaMap extends JPanel {
|
|||||||
|
|
||||||
Feature feature = featureList.get(ii);
|
Feature feature = featureList.get(ii);
|
||||||
|
|
||||||
|
if(reachedWaypoints.contains(ii)&&(feature.img_file.equals("images/cheese_64x64.png")||feature.img_file.equals("images/strawberry_64x64.png"))){
|
||||||
|
mouse = true;
|
||||||
|
continue; // Skip drawing reached waypoints, but keep drawing vehicle -- only applies to mouse version of sim so snacks disappear.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the message if the last waypoint is reached -- only for RUN_mouse for now.
|
||||||
|
if(lastWaypointReached && mouse){
|
||||||
|
Font font = new Font("Comic Sans MS", Font.BOLD, 35); //Choose desired font and size
|
||||||
|
g2d.setFont(font);
|
||||||
|
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||||
|
String message = "Snacks successfully eaten";
|
||||||
|
int messageWidth = fontMetrics.stringWidth(message);
|
||||||
|
int messageHeight = fontMetrics.getHeight();
|
||||||
|
int x = (getWidth()-messageWidth)/2;
|
||||||
|
int y = (getHeight() - messageHeight)/2 + fontMetrics.getAscent();
|
||||||
|
Rectangle messageRect = new Rectangle(x, y - fontMetrics.getAscent(), messageWidth, messageHeight);
|
||||||
|
|
||||||
|
//Draw a background rectangle for the message
|
||||||
|
g2d.setColor(Color.WHITE);
|
||||||
|
g2d.fill(messageRect);
|
||||||
|
|
||||||
|
//Draw the message text
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
g2d.drawString(message, x,y);
|
||||||
|
}
|
||||||
|
|
||||||
int featureX = (int) -(feature.west / metersPerPixel);
|
int featureX = (int) -(feature.west / metersPerPixel);
|
||||||
int featureY = (int) -(feature.north / metersPerPixel);
|
int featureY = (int) -(feature.north / metersPerPixel);
|
||||||
|
|
||||||
@ -122,7 +178,7 @@ class ArenaMap extends JPanel {
|
|||||||
|
|
||||||
public class EVDisplay extends JFrame {
|
public class EVDisplay extends JFrame {
|
||||||
|
|
||||||
private ArenaMap arenaMap;
|
private static ArenaMap arenaMap;
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private DataOutputStream out;
|
private DataOutputStream out;
|
||||||
|
|
||||||
@ -252,6 +308,24 @@ public class EVDisplay extends JFrame {
|
|||||||
double H = Double.parseDouble( field[3] );
|
double H = Double.parseDouble( field[3] );
|
||||||
vehicle.setState(N,W,H);
|
vehicle.setState(N,W,H);
|
||||||
|
|
||||||
|
//Find the index of the reached waypoint
|
||||||
|
int reachedWaypointIndex =-1;
|
||||||
|
for(int i = 0; i < featureList.size(); i++){
|
||||||
|
Feature feature = featureList.get(i);
|
||||||
|
if(feature instanceof Feature){
|
||||||
|
Feature waypoint = (Feature) feature;
|
||||||
|
if(waypoint.isReached(N,W)){
|
||||||
|
reachedWaypointIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//After the vehicle reaches a waypoint
|
||||||
|
if(reachedWaypointIndex != -1){
|
||||||
|
arenaMap.updateReachedWaypoint(reachedWaypointIndex);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException | NullPointerException e ) {
|
} catch (IOException | NullPointerException e ) {
|
||||||
go = false;
|
go = false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user