Dragging JLabels

Hello everyone,
This is my first thread in this forum and I am a java beginner, so please don’t harsh on me :stuck_out_tongue:
Excuse me if my question sounds SO SO LAME.

I now have to write a networked board game which involves dragging around cards in the window.
As of the start, I am trying to drag a JLabel around in one client and send the data to server which distributes to other clients to see that JLabel moving in their windows as well.

What I did is add MouseActionListener and MouseMotionListener to the JLabel and send the data to server while the JLabel is being dragged.
There I got a problem where I can’t write out server to update the JLabel’s location and keep getting Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

I did it in the same way as I did my chat client where I write out data from client’s input with ActionListener.

Any ponters on where I should start learning about it and any pointer on this matter will be welcomed…
All I want to see now is my two clients synchronizing when I label is dragged…

Thanks in advance

I dunno, in these circumstances, I wouldn’t really use a JLabel. I would make a separate class that you can call a .render(Graphics g) method on, that will draw the card. In the update method, you can check if the card is being dragged with a MouseListener event(mouseDragged) and align it with that mouse position.

But if you’re using eclipse, click on the errors and they will take you to what’s causing the problem. Can you post any code?

Thank you for your reply :slight_smile:
I plan to use different classes for different GUI components for my game later on, since my game needs things like cards, tokens, dices and so on,

but what i want is i just want to see two screens sync-ing when i drag :smiley:
here’s my rubbish code :frowning:


import java.awt.Component;
import java.awt.Cursor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.net.*;
import java.awt.Color;
import javax.swing.SwingConstants;

public class Drag extends JFrame {

	private JLabel lblNewLabel = null;
	private JLabel lblNewlabel1 = null;
	private InputStream is = null;
	private OutputStream os = null;
	private PrintWriter out = null;
	private Scanner input = null;
	private JPanel contentPane;
	private boolean drag;
	private int x;
	private int y;

	/**
	 * Create the frame.
	 */
	public Drag() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblNewLabel = new JLabel("New label");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBackground(Color.RED);
		lblNewLabel.setOpaque(true);
		lblNewLabel.setBounds(29, 61, 96, 70);
		contentPane.add(lblNewLabel);

		lblNewlabel1 = new JLabel("New label1");
		lblNewlabel1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewlabel1.setBackground(Color.RED);
		lblNewlabel1.setOpaque(true);
		lblNewlabel1.setBounds(199, 61, 96, 70);
		contentPane.add(lblNewlabel1);

		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.getSource() == lblNewLabel) {
					drag = true;
					x = e.getX();
					y = e.getY();
				}
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				drag = false;
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

			}
		});

		lblNewLabel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				if (drag) {
					if (e.getSource() == lblNewLabel) {
						Component c = (Component) e.getSource();
						int X = c.getX() + e.getX();
						int Y = c.getY() + e.getY();
						
						
					//lblNewLabel.setLocation(c.getX() + e.getX() - x, 	c.getY() + e.getY()- y);
						String s = "lblNewLabel" + "/"
								+ Integer.toString((X - x))
								+ "/"
								+ Integer.toString(Y - y);
						out.println(s);

					}
				}
			}
		});

		lblNewlabel1.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.getSource() == lblNewlabel1) {
					drag = true;
					x = e.getX();
					y = e.getY();
				}
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				drag = false;
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

			}
		});

		lblNewlabel1.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				if (drag) {
					if (e.getSource() == lblNewlabel1) {
						Component c = (Component) e.getSource();
				//		String s = "lblNewLabel1" + "/"
				//				+ Integer.toString((c.getX() + e.getX() - x))
				//				+ "/"
				//				+ Integer.toString(c.getY() + e.getY() - y);
						out.println("lblNewLabel1" + "/" + Integer.toString((c.getX() + e.getX() - x)) + "/" + Integer.toString((c.getY()+e.getY() - y)));
						// c.setLocation(c.getX() + e.getX() - x, c.getY() + e.getY()- y);

					}
				}
			}
		});

	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		Drag frame = new Drag();
		frame.setVisible(true);
		frame.run();
	}

	private void run() {
		try {
			Socket socket = new Socket("localhost", 55555);

			InputStream inStream = socket.getInputStream();
			OutputStream outStream = socket.getOutputStream();

			input = new Scanner(inStream);
			out = new PrintWriter(outStream, true);

			while (true) {
				String line = input.nextLine();
				String[] parts = line.split("/");
				String parts0 = parts[0];
				String parts1 = parts[1];
				String parts2 = parts[2];
				int x = Integer.parseInt(parts1);
				int y = Integer.parseInt(parts2);
				if (parts0.equals("lblNewLabel1"))
					lblNewlabel1.setLocation(x, y);
				if (parts0.equals("lblNewLabel"))
					lblNewLabel.setLocation(x, y);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
} 

Eh, try putting the code in a

 tags or paste it. Also, I think you're making it much more complicated than you need to. Create a separate Entity class that represents any entity on the board. Then create subclasses for Card, Dice, etc. You'll find this much more easier to work with.