1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.JPanel;
public class HorizonInstrument extends JPanel {
//Graphic object
private Graphics2D g2d;
//Images
private Image background = null;
private Image mobile = null;
//Angles for the mobile image
private float rollAct = 0;
private float rollMin = -180;
private float rollMax = 180;
private float pitchAct = 0;
private float pitchMin = -180;
private float pitchMax = 180;
//Path of the images
private String path = "";
private String defaultPath = "";
//End of the images names
private String backgroundString = "Bg.png";
private String mobileString = "Mb.png";
//Check if there is images available
private boolean check = false;
public HorizonInstrument(String planeName) {
//Set the size of the panel
this.setSize(200, 200);
//Initialize the paths
path = "images" + java.io.File.separatorChar + planeName + java.io.File.separatorChar + "Hor";
defaultPath = "images" + java.io.File.separatorChar + "Default" + java.io.File.separatorChar + "Hor";
//Open the plane images as file (just for the next function)
File test1 = new File(path + backgroundString);
File test2 = new File(path + mobileString);
//Check if the images exists
check = test1.exists() && test2.exists();
//If they exists
if(check) {
//Load the images
background = Toolkit.getDefaultToolkit().createImage(path + backgroundString);
mobile = Toolkit.getDefaultToolkit().createImage(path + mobileString);
}
//If they do not exists
else {
//Open the default images as file (just for the next function)
File test3 = new File(defaultPath + backgroundString);
File test4 = new File(defaultPath + mobileString);
//Check if the images exists
check = test3.exists() && test4.exists();
//If they do exists
if(check) {
//Load the default images
background = Toolkit.getDefaultToolkit().createImage(defaultPath + backgroundString);
mobile = Toolkit.getDefaultToolkit().createImage(defaultPath + mobileString);
}
}
}
@Override
public void paintComponent(Graphics g){
//Load the Graphics objects as a Graphics2D object
g2d = (Graphics2D) g;
//If images available
if (check) {
//Set the background color
g2d.setColor(Color.getHSBColor(0, 0, 23));
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
//g2d.translate(height, height);
//Rotate it with an angle in gradians
g2d.rotate(rollAct * 0.0174532925, this.getWidth()/2, this.getHeight()/2);
//Draw the mobile image
g2d.drawImage(mobile, 0, 0, this.getWidth(), this.getHeight(), this);
//g2d.translate(-pitchAct, -pitchAct);
g2d.rotate(-rollAct * 0.0174532925, this.getWidth()/2, this.getHeight()/2);
//Draw the background image
g2d.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this);
}
//If images are not available
else {
//Set the color to red
g2d.setColor(Color.RED);
//Write an error
g2d.drawString("File(s) not found", this.getWidth()/4, this.getHeight()/4);
}
}
public void changeAngle(float roll, float pitch) {
//If images are available
if (check) {
//If roll possible
if ( roll<rollMax && roll>rollMin ) {
//Set roll
rollAct = roll;
}
if ( pitch<pitchMax && pitch>pitchMin ) {
pitchAct = pitch;
}
//Repaint the image
this.repaint();
}
}
} |
Partager