/*
 * MoveText.java
 *
 * Copyright 2013 Ajay Bhatia <ajay@dumb-box>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

public class MoveText extends JFrame implements MouseMotionListener {
    private ExtendedLabel lblMessage;
  
    public MoveText() {
        lblMessage = new ExtendedLabel(JOptionPane.showInputDialog(null, "Enter some text", "Enter your text here"));
              
        add(lblMessage);
      
        lblMessage.addMouseMotionListener(this);
    }
  
    public void mouseMoved(MouseEvent e) {
              
    }
  
    public void mouseDragged(MouseEvent e) {
        lblMessage.moveText(e.getX(), e.getY());
    }
  
    private class ExtendedLabel extends JLabel {
        private String text;
        private int x;
        private int y;
      
        ExtendedLabel(String text) {
            this.text = text;
            this.x = 10;
            this.y = 10;
        }
  
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(text, x, y);
        }
  
        public void moveText(int x, int y) {
            this.x = x;
            this.y = y;
            repaint();
        }
    }
  
    public static void main (String args[]) {
        MoveText frame = new MoveText();
        frame.setExtendedState(MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments