Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.awt.event.ItemListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.ImageIcon;
- import javax.swing.*;
- import java.awt.*;
- public class AWG_Widget extends JPanel {
- public static final int mapWidth = 300;
- public static final int mapHeight = 300;
- BorderLayout layout = new BorderLayout();
- JPanel northPanel, centerPanel;
- JButton bt_GetMap;
- //Holds the map
- JLabel map;
- ImageIcon mapIcon;
- // Combo Box for the types of maps: roadmap, satellite, terrain
- String[] mapTypesStringArray = {"roadmap","satellite","terrain"};
- String selectedMapType = "satellite";
- JComboBox mapTypesComboBox = new JComboBox(mapTypesStringArray);
- public AWG_Widget(){
- // Set layout
- setLayout(layout);
- // Sets a border around the pane
- this.setBorder(BorderFactory.createEtchedBorder());
- // inits panels
- northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
- centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
- // Creates components
- try {
- createMap();
- }
- catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- bt_GetMap = new JButton("Get Map!");
- // Add components to panels
- try{
- createMap();
- }
- catch(IOException e){
- System.out.println("Bad output");
- }
- northPanel.add(mapTypesComboBox);
- northPanel.add(bt_GetMap);
- // Add panels to layout
- add(northPanel, BorderLayout.NORTH);
- add(centerPanel, BorderLayout.CENTER);
- // Add Listeners
- mapTypesComboBox.addItemListener(new ComboBoxItemListener());
- bt_GetMap.addActionListener(new ButtonListener());
- }
- public void createMap() throws IOException{
- String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?center=10,-11.998672&zoom=6&size=612x612&scale=5&maptype=" + selectedMapType + "";
- String destinationFile = "image.jpg";
- String str = destinationFile;
- URL url = new URL(imageUrl);
- InputStream is = url.openStream();
- OutputStream os = new FileOutputStream(destinationFile);
- byte[] b = new byte[2048];
- int length;
- while ((length = is.read(b)) != -1) {
- os.write(b, 0, length);
- }
- is.close();
- os.close();
- mapIcon = new ImageIcon((new ImageIcon("image.jpg")).getImage().getScaledInstance(mapWidth, mapHeight,
- java.awt.Image.SCALE_SMOOTH));
- map = new JLabel(mapIcon);
- centerPanel.add(map);
- }
- // Item Listener Class
- class ComboBoxItemListener implements ItemListener{
- @Override
- public void itemStateChanged(ItemEvent e){
- if(e.getStateChange() == ItemEvent.SELECTED){
- if(e.getItem().equals("roadmap")){
- selectedMapType = "roadmap";
- }
- if(e.getItem().equals("satellite")){
- selectedMapType = "satellite";
- }
- if(e.getItem().equals("terrain")){
- selectedMapType = "terrain";
- }
- }
- }
- }
- class ButtonListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- try{
- centerPanel.remove(map);
- remove(centerPanel);
- repaint();
- revalidate();
- createMap();
- add(centerPanel, BorderLayout.CENTER);
- revalidate();
- repaint();
- }
- catch(IOException ex){
- System.out.println("Bad map");
- }
- catch(InterruptedException ex){
- System.out.println("Bad map");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement