Guest User

Untitled

a guest
Aug 12th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. Moving arrays from a resultSet
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.*;
  9.  
  10. import java.sql.Statement;
  11.  
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.util.ArrayList;
  18.  
  19. public class mainStat extends JPanel {
  20.  
  21.  
  22. BufferedImage img;
  23. Double[] eleArray;
  24. private Image bg;
  25. private Image pic;
  26. int alt_1 = normalise(eleArray[0]);
  27. int alt_2 = normalise(eleArray[1]);
  28. int alt_3 = normalise(eleArray[2]);
  29. int alt_4 = normalise(eleArray[3]);
  30. int alt_5 = normalise(eleArray[4]);
  31. int alt_6 = normalise(eleArray[5]);
  32. int alt_7 = normalise(eleArray[6]);
  33. int alt_8 = normalise(eleArray[7]);
  34. int alt_9 = normalise(eleArray[8]);
  35. int alt_0 = normalise(eleArray[9]);
  36. private double shop;
  37. private double bike_shop;
  38. private double accomadation;
  39. int alt = 70;
  40. JLabel label1;
  41. private String display_distance;
  42. static String[] eleResults;
  43.  
  44. Connection con;
  45. Statement stmt;
  46. ResultSet rs;
  47. static double eleOne;
  48. static double eleTwo;
  49. static double eleThree;
  50. static double eleFour;
  51. static double eleFive;
  52. static double eleSix;
  53. static double eleSeven;
  54. static double eleEight;
  55. static double eleNine;
  56. static double eleZero;
  57.  
  58.  
  59. //add in constructor method calling paint(Graphics g)
  60. public mainStat(String distance){
  61. display_distance = distance;
  62.  
  63.  
  64. label1 = new JLabel();
  65.  
  66. setBorder(
  67. BorderFactory.createCompoundBorder(
  68. BorderFactory.createCompoundBorder(
  69. BorderFactory.createTitledBorder("Geo-strip"),
  70. BorderFactory.createEmptyBorder(5,5,5,5)),
  71. getBorder()));
  72.  
  73. add(label1);
  74.  
  75.  
  76. }
  77.  
  78. public void paintComponent(Graphics g){
  79. super.paintComponent(g);
  80.  
  81. //main GPS strip
  82. try {
  83. img = ImageIO.read(new File("C:\test\geo_strip_box_ex.png"));
  84.  
  85. bg = ImageIO.read(new File("C:\test\geo_strip_main_ex.png"));
  86. } catch (IOException e) {
  87. }
  88.  
  89. // Primary indicator box
  90. g.setColor(Color.WHITE);
  91. g.fillRect(20, 45, 75, 60);
  92. g.drawImage(img, 10, 25, null);
  93. Font font = new Font("Serif", Font.PLAIN, 25);
  94. g.setColor(Color.BLACK);
  95. g.setFont(font);
  96. g.drawString(display_distance, 16, 80);
  97.  
  98.  
  99. // Main Geo-data
  100. g.drawImage(bg, 10, 25, null);
  101.  
  102. // Altitude data
  103. // Point 1
  104. g.setColor(Color.RED);
  105. g.fillOval(105, alt_1, 10, 10);
  106.  
  107. // Point 2
  108. g.setColor(Color.RED);
  109. g.fillOval(153, alt_2, 10, 10);
  110.  
  111. // Point 3
  112. g.setColor(Color.RED);
  113. g.fillOval(201, alt_3, 10, 10);
  114.  
  115. // Point 4
  116. g.setColor(Color.RED);
  117. g.fillOval(249, alt_4, 10, 10);
  118.  
  119. // Point 5
  120. g.setColor(Color.RED);
  121. g.fillOval(297, alt_5, 10, 10);
  122.  
  123. // Point 6
  124. g.setColor(Color.RED);
  125. g.fillOval(345, alt_6, 10, 10);
  126.  
  127. // Point 7
  128. g.setColor(Color.RED);
  129. g.fillOval(394, alt_7, 10, 10);
  130.  
  131. // Point 8
  132. g.setColor(Color.RED);
  133. g.fillOval(442, alt_8, 10, 10);
  134.  
  135. // Point 9
  136. g.setColor(Color.RED);
  137. g.fillOval(490, alt_9, 10, 10);
  138.  
  139. // Point 10
  140. g.setColor(Color.RED);
  141. g.fillOval(540, alt_0, 10, 10);
  142.  
  143. // Performance blocks that are build out of an array
  144. //first block
  145. g.setColor(Color.BLUE);
  146. g.fillRect(108, 144, 108, 12);
  147.  
  148. //second block
  149. g.setColor(Color.GREEN);
  150. g.fillRect(218, 144, 108, 12);
  151.  
  152. //third block
  153. g.setColor(Color.ORANGE);
  154. g.fillRect(329, 144, 108, 12);
  155.  
  156. //forth block
  157. g.setColor(Color.RED);
  158. g.fillRect(439, 144, 108, 12);
  159. }
  160. private int normalise(double altitude){
  161. //Build the array once correct this part will be changed to allow data base connectivity
  162.  
  163. ;
  164. //Next stage is to find the max values within the array
  165. double max = eleArray[0];
  166. for (int i = 0; i < 10; i++)
  167. if (eleArray[i]> max) max = eleArray[i];
  168.  
  169.  
  170. //Replicate the double loop control for the min values
  171. double min = eleArray[0];
  172. for (int i = 0; i < 10; i++)
  173. if (eleArray[i]< min) min = eleArray[i];
  174.  
  175. //Now set the normalised scale; first with the minimum and then the maximum
  176. double minNalt = 50;
  177. double maxNalt = 120;
  178.  
  179. double result = maxNalt+(altitude-min)*(minNalt-maxNalt)/(max-min);
  180. int aInt = (int)result;
  181. return aInt;
  182. }
  183. public void Result() {
  184.  
  185. String query = "SELECT trkpt_ele FROM routes where trkseg_name = '2011-06-14'";
  186. try
  187. {
  188. // Connect to the database
  189. // Similar to php first I need to create a variable to connect to the MySQL database.
  190. String url = "jdbc:mysql://localhost:3306/bicycleinformationschema";
  191.  
  192. // Now we need to include username and pass and test mysql database connection
  193. Connection con = DriverManager.getConnection(url,"root", "kop98mnbvcxz");
  194.  
  195. Statement st = con.createStatement();
  196. ResultSet rs = st.executeQuery(query);
  197. ArrayList <Double> eleResults = new ArrayList<Double>();
  198.  
  199. while (rs.next())
  200.  
  201. eleResults.add(rs.getDouble(1));
  202. Double[] eleArray = eleResults.toArray(new Double[0]);
  203.  
  204.  
  205. }
  206. catch (SQLException ex)
  207. {
  208. System.err.println(ex.getMessage());
  209. }
  210.  
  211. }
  212. }
  213.  
  214. ResultSet rs = stmt.executeQuery("...");
  215. List<MyDomainClass> results = new ArrayList<MyDomainClass>();
  216.  
  217. while (rs.next()) {
  218. MyDomainClass obj;
  219. // create obj by querying rs for this row
  220.  
  221. results.add(obj);
  222. }
Add Comment
Please, Sign In to add comment