Wednesday, March 2, 2016

World Map Data On MouseMove Event

This program displays the number of countries by continents. The mouseMoved() function serves as a selector. When user's mouse is positioned over a "continent" on the map, the corresponding data will be displayed. The process is performed based on the different colors of the continents. Class JLayeredPane is used instead of JPanel. With the help of the class, the textarea that holds the data will look like a popup window.


  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
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class ContinentData extends JFrame{
    private JLayeredPane layeredPane=null;
    private BufferedImage bufimage=null;
    private ImageIcon icon=null;
    private JLabel label=null;
    private JTextArea tarea=null;
    
    public static void main(String[] args) throws Exception{
        ContinentData ap=new ContinentData();
    }
    
    public ContinentData(){
        super("Continent Data");
        setSize(450,300);
        
        setLayout(new BorderLayout());
        
        try{
            bufimage=ImageIO.read(new File("continent.png"));
        }catch(Exception e){
        }
        icon=new ImageIcon(bufimage);
        
        label=new JLabel();
        label.addMouseListener(new MouseHandler());
        label.addMouseMotionListener(new MouseHandler());
        label.setIcon(icon);
        label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
        
        tarea=new JTextArea();
        tarea.setEditable(false);
        tarea.setBounds(0,0,120,50);
        
        layeredPane=new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(450,300));
        layeredPane.add(label,0);
        layeredPane.add(tarea,1);
        add(BorderLayout.CENTER,layeredPane);
        
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class MouseHandler implements MouseMotionListener, MouseListener{
        public void mouseMoved(MouseEvent e){
            int i=0;
            try{
                i=bufimage.getRGB(e.getX(),e.getY());
            }catch(Exception ex){
                
            }
            switch(i){
                case -16036463:
                    tarea.setText("Asia"+"\n"+"Countries: 44");
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                case -2069500:
                    tarea.setText("Europe"+"\n"+"Countries: 47");
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                case -2290923:
                    tarea.setText("Africa"+"\n"+"Countries: 54");
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                case -15197407:
                    tarea.setText("Oceania"+"\n"+"Countries: 14");                                        
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                case -8998137:
                    tarea.setText("North America"+"\n"+"Countries: 23");
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                case -3745531:
                    tarea.setText("South America"+"\n"+"Countries: 12");                                                                         
                    tarea.setLocation(e.getX()+20,e.getY());
                    layeredPane.moveToFront(tarea);
                    break;
                default:
                    layeredPane.moveToBack(tarea);
                    tarea.setText("");
                    break;
            }
        }
        public void mouseDragged(MouseEvent e){

        }
        public void mouseEntered(MouseEvent e){
            
        }
        public void mouseExited(MouseEvent e){
            
        }
        public void mouseReleased(MouseEvent e){

        }
        public void mousePressed(MouseEvent e){

        }
        public void mouseClicked(MouseEvent e){
            //System.out.println(bufimage.getRGB(e.getX(),e.getY()));
        }
    }
}


Data Source: http://www.worldatlas.com/cntycont.htm

No comments:

Post a Comment