You are to design and code a Java application which allows the user to move words about a container. There are a number of ways to implement this problem, and the following is a suggestion of a recommended way. Create two classes: a class BoxWord, which represents the moveable word objects, and a JFrame driver subclasclass. I construct the word object with the following parameters: a reference to the parent, a String (the actual word), a color, a font, a width and a height. You may not need all those for a basic working program. The object should delegate a MouseMotionListener and MouseListener(interfaces) to pick up a mousePressed (word select), mouseReleased (word deselect), and mouseDragged(word moved) events. To move the words you can use the setLocation method. You can use the Component method getLocation method to find out where the word is currently located. I use an extended java.awt.Canvas to create my BoxWord class, but a JLabel might work as well.
The motion is accomplished by setting the x-coordinate location of the word to the current word x-coord location, plus the x-coord of the mouseDragged event, minus the the x-coord of the original selection location (note that a selection of a word can occur anywhere in that object). The y-coord is done similarly. The driver program has an array of String, as well as an array of BoxWord. As it loops through the array of String, it will construct the word objects and place them randomly in the driver container. The layout manager should be null, i.e. no layout manger. To have the objects apprear on the GUI you will use the setBounds method.
Add a JMeun with options to add a word JOptionPane window input. Also a double click will pop up a JOptionPane window asking if the word should be removed from the GUI. The pictures displayed below show how the WordDrag application proceeds and how its words can be moved, deleted, and added.
WordDrag 2014 WordDrag 2014 WordDrag 2014 Select Option Select Option Select Option 3 2. Java Drag Java Java Drag Sad Cloudy Drag Sad Cloudy Red Red Java Foot 2EE Very Java Foot Do Java Foot Wa Great J2EE W& Great W& Great Hands ThroughWSAD 5.1 Face Wor Skippy Hands Through Hands Through WSAD 5.1 WSAD 5.1 Face Word Skippy Face Word Skippy Run Run Enter you word ? New Word Entry Happines Word Remove Dialog Reoe Drag? OK Cancel OK Cancel Wor g 2014 Select Option Happiness Sad Java Foot 2EE Wa Great Hands WSAD 5.1 Face Word Skippy
Expert Answer
//BoxWord.java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class BoxWord extends JLabel implements MouseMotionListener, MouseListener {
int mouseX;
int mouseY;
int randX, randY;
Random rand = new Random();
BoxWord(String name, Color c, Font font, int width, int height) {
randX=rand.nextInt(1000);
randY=rand.nextInt(400);
setText(name);
setOpaque(true);
setFont(font);
setBounds(randX,randY,width, height);
setBackground(c);
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
if(JOptionPane.showConfirmDialog(null, “Remove Drag?”, “Word Remove Dialog”,JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
setVisible(false);
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
this.setBackground(Color.YELLOW);
}
@Override
public void mouseReleased(MouseEvent e) {
this.setBackground(Color.PINK);
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX += e.getX();
mouseY += e.getY();
this.setLocation(mouseX, mouseY);
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
}
============================================================================
// WordDrag,java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class WordDrag {
JPanel panel = new JPanel();
ArrayList<BoxWord> label;
ArrayList<String> label_names;
int width, height;
JFrame frame;
JMenuBar menuBar;
JMenuItem addWord;
JMenuItem quit;
public WordDrag() {
frame = new JFrame();
panel.setLayout(null);
panel.setBackground(Color.GRAY);
//Creating menu bar
menuBar = new JMenuBar();
JMenu menu = new JMenu(“Select Option”);
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
addWord = new JMenuItem(“Add Word”, KeyEvent.VK_A);
quit = new JMenuItem(“Quit”, KeyEvent.VK_A);
MenuItemListener menuItemListener = new MenuItemListener();
addWord.addActionListener(menuItemListener);
quit.addActionListener(menuItemListener);
menu.add(addWord);
menu.add(quit);
frame.setJMenuBar(menuBar);
//Initializing the array list which displays the label names.
label_names = new ArrayList<String>();
label_names.add(“Java”);
label_names.add(“Red”);
label_names.add(“Drag”);
label_names.add(“Face”);
label_names.add(“Java”);
label_names.add(“Sad”);
label_names.add(“Cloudy”);
label_names.add(“Very”);
label_names.add(“Great”);
label_names.add(“Hands”);
label_names.add(“Through”);
label_names.add(“WSAD 5.1”);
label_names.add(“Word”);
label_names.add(“Skippy”);
label_names.add(“Hands”);
label_names.add(“Run”);
label_names.add(“J2EE”);
label = new ArrayList<BoxWord>();
for (int i = 0; i < label_names.size(); ++i) {
label.add(new BoxWord(label_names.get(i), Color.PINK, Font.decode(Font.SANS_SERIF), 50, 30));
panel.add(label.get(i));
}
frame.add(panel);
frame.setVisible(true);
frame.setSize(1000, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addWord) {
String newLabel = (String) JOptionPane.showInputDialog(null, “New Word Entry”, “Enter new word”, JOptionPane.QUESTION_MESSAGE);
label.add(new BoxWord(newLabel, Color.PINK, Font.decode(Font.SANS_SERIF), 50, 30));
label_names.add(newLabel);
label.get(label_names.size() – 1).addMouseMotionListener(label.get(label_names.size() – 1));
panel.add(label.get(label_names.size() – 1));
SwingUtilities.updateComponentTreeUI(frame);
} else if (e.getSource() == quit) {
frame.setVisible(false);
frame.dispose();
System.exit(0);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WordDrag();
}
});
}
}
==================================================================================
sample output