Advertisement
mrjank

Untitled

Oct 29th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.URL;
  6. import java.awt.event.ItemListener;
  7. import java.awt.event.ItemEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import javax.swing.ImageIcon;
  11.  
  12. import javax.swing.*;
  13. import java.awt.*;
  14.  
  15. public class AWG_Widget extends JPanel {
  16.  
  17. public static final int mapWidth = 300;
  18. public static final int mapHeight = 300;
  19.  
  20. BorderLayout layout = new BorderLayout();
  21. JPanel northPanel, centerPanel;
  22. JButton bt_GetMap;
  23. //Holds the map
  24. JLabel map;
  25. ImageIcon mapIcon;
  26. // Combo Box for the types of maps: roadmap, satellite, terrain
  27. String[] mapTypesStringArray = {"roadmap","satellite","terrain"};
  28. String selectedMapType = "satellite";
  29. JComboBox mapTypesComboBox = new JComboBox(mapTypesStringArray);
  30.  
  31.  
  32. public AWG_Widget(){
  33. // Set layout
  34. setLayout(layout);
  35. // Sets a border around the pane
  36. this.setBorder(BorderFactory.createEtchedBorder());
  37.  
  38. // inits panels
  39. northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  40. centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  41.  
  42. // Creates components
  43. try {
  44. createMap();
  45. }
  46. catch (IOException e) {
  47. e.printStackTrace();
  48. System.exit(1);
  49. }
  50. bt_GetMap = new JButton("Get Map!");
  51.  
  52.  
  53.  
  54. // Add components to panels
  55. try{
  56. createMap();
  57.  
  58. }
  59. catch(IOException e){
  60. System.out.println("Bad output");
  61. }
  62.  
  63. northPanel.add(mapTypesComboBox);
  64. northPanel.add(bt_GetMap);
  65.  
  66.  
  67. // Add panels to layout
  68. add(northPanel, BorderLayout.NORTH);
  69. add(centerPanel, BorderLayout.CENTER);
  70.  
  71. // Add Listeners
  72. mapTypesComboBox.addItemListener(new ComboBoxItemListener());
  73. bt_GetMap.addActionListener(new ButtonListener());
  74. }
  75.  
  76. public void createMap() throws IOException{
  77. String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?center=10,-11.998672&zoom=6&size=612x612&scale=5&maptype=" + selectedMapType + "";
  78. String destinationFile = "image.jpg";
  79. String str = destinationFile;
  80. URL url = new URL(imageUrl);
  81. InputStream is = url.openStream();
  82. OutputStream os = new FileOutputStream(destinationFile);
  83. byte[] b = new byte[2048];
  84. int length;
  85.  
  86. while ((length = is.read(b)) != -1) {
  87. os.write(b, 0, length);
  88. }
  89.  
  90. is.close();
  91. os.close();
  92.  
  93. mapIcon = new ImageIcon((new ImageIcon("image.jpg")).getImage().getScaledInstance(mapWidth, mapHeight,
  94. java.awt.Image.SCALE_SMOOTH));
  95. map = new JLabel(mapIcon);
  96.  
  97. centerPanel.add(map);
  98.  
  99. }
  100.  
  101. // Item Listener Class
  102. class ComboBoxItemListener implements ItemListener{
  103. @Override
  104. public void itemStateChanged(ItemEvent e){
  105.  
  106. if(e.getStateChange() == ItemEvent.SELECTED){
  107.  
  108. if(e.getItem().equals("roadmap")){
  109. selectedMapType = "roadmap";
  110.  
  111. }
  112. if(e.getItem().equals("satellite")){
  113. selectedMapType = "satellite";
  114.  
  115. }
  116. if(e.getItem().equals("terrain")){
  117. selectedMapType = "terrain";
  118.  
  119.  
  120. }
  121. }
  122. }
  123. }
  124. class ButtonListener implements ActionListener {
  125. @Override
  126. public void actionPerformed(ActionEvent e) {
  127. try{
  128.  
  129. centerPanel.remove(map);
  130. remove(centerPanel);
  131. repaint();
  132. revalidate();
  133.  
  134. createMap();
  135.  
  136.  
  137. add(centerPanel, BorderLayout.CENTER);
  138. revalidate();
  139. repaint();
  140. }
  141. catch(IOException ex){
  142. System.out.println("Bad map");
  143. }
  144. catch(InterruptedException ex){
  145. System.out.println("Bad map");
  146. }
  147.  
  148.  
  149. }
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement