Example #1
0
  public void makeFrame() throws IOException {

    panel =
        new JPanel() {
          public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.RED);
            if (bufImage == null) {
              int w = this.getWidth();
              int h = this.getHeight();
              bufImage = new BufferedImage(800, 500, BufferedImage.TRANSLUCENT);
              Graphics2D gc = bufImage.createGraphics();
            }

            g2.drawImage(bufImage, null, 0, 0);
            if (state == State.DRAGGING) {
              g2.drawLine(start.x, start.y, end.x, end.y);
            }
          }

          public Dimension getPreferredSize() {
            return new Dimension(1300, 800);
          }
        };

    panel.addMouseListener(
        new MouseListener() {
          @Override
          public void mouseClicked(MouseEvent e) {

            x = e.getX();
            y = e.getY();
            label = new JLabel();

            String name = JOptionPane.showInputDialog("Enter the name of City");

            if (name.equals("Jodhpur")) {
              ImageIcon img = new ImageIcon("jodhpur.jpg");
              label.setIcon(img);
            } else if (name.equals("Pali")) {
              ImageIcon img = new ImageIcon("pali.jpg");
              label.setIcon(img);

            } else if (name.equals("Mahendipur Balajee")) {
              ImageIcon img = new ImageIcon("Mahendipur Balijee.jpg");
              label.setIcon(img);

            } else if (name.equals("Nadoti")) {
              ImageIcon img = new ImageIcon("Nadoti.jpg");
              label.setIcon(img);
            } else if (name.equals("Jaipur")) {
              ImageIcon img = new ImageIcon("jaipur.jpg");
              label.setIcon(img);
            } else if (name.equals("Ratanada Headoffice")) {
              ImageIcon img = new ImageIcon("Ratanada.jpg");
              label.setIcon(img);
            } else {
              ImageIcon img = new ImageIcon("Other.jpg");
              label.setIcon(img);
            }

            label.setLayout(new BorderLayout());
            panel.add(label);

            label.setBounds(x, y, 65, 60);
            label.setToolTipText(name);
          }

          @Override
          public void mousePressed(MouseEvent me) {}

          @Override
          public void mouseReleased(MouseEvent me) {

            state = State.IDLE;
          }

          @Override
          public void mouseEntered(MouseEvent me) {}

          @Override
          public void mouseExited(MouseEvent me) {}
        });

    panel.addMouseMotionListener(
        new MouseMotionListener() {

          @Override
          public void mouseDragged(MouseEvent me) {
            state = State.DRAGGING;
            end = me.getPoint();
            if (state == State.DRAGGING) {
              Graphics2D g2 = bufImage.createGraphics();
              g2.setColor(Color.red);
              g2.setStroke(new BasicStroke(2));
              g2.drawLine(start.x, start.y, end.x, end.y);
              panel.repaint();
            }
            start = end;
          }

          @Override
          public void mouseMoved(MouseEvent me) {

            start = me.getPoint();
          }
        });

    panel.setOpaque(true);
    // ^ out.close();
  }
Example #2
0
  static void buildGUI() {
    // Need this size to balance axes.
    frame.setSize(520, 690);
    frame.setTitle("DrawTool");
    frame.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });

    Container cPane = frame.getContentPane();

    // Status label on top. Unused for now.
    statusLabel.setOpaque(true);
    statusLabel.setBackground(Color.white);
    cPane.add(statusLabel, BorderLayout.NORTH);

    // Build the input/output elements at the bottom.
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.black));
    panel.setBackground(inputPanelColor);
    panel.setLayout(new GridLayout(2, 1));
    panel.add(outputLabel);
    JPanel bottomPanel = new JPanel();
    bottomPanel.setBackground(inputPanelColor);
    bottomPanel.add(inputField);
    bottomPanel.add(new JLabel("   "));
    JButton enterButton = new JButton("Enter");
    enterButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent a) {
            hasEntered = true;
          }
        });
    bottomPanel.add(enterButton);
    panel.add(bottomPanel);
    if (!sequencingOn) {
      cPane.add(panel, BorderLayout.SOUTH);
    }

    // Drawing in the center.
    drawArea = new DrawTool();
    if (sequencingOn) {
      frame.addKeyListener(
          new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
              handleKeyTyped(e);
            }
          });
    }
    cPane.add(drawArea, BorderLayout.CENTER);

    drawArea.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            handleMouseClick(e);
          }

          public void mouseReleased(MouseEvent e) {
            handleMouseReleased(e);
          }
        });

    drawArea.addMouseMotionListener(
        new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
            handleMouseDragged(e);
          }
        });
  }