Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Dragging JLabels  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
Offline ken1992

Junior Newbie





« Posted 2012-11-08 04:31:19 »

Hello everyone,
This is my first thread in this forum and I am a java beginner, so please don't harsh on me  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
Offline Agro
« Reply #1 - Posted 2012-11-08 04:50:10 »

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?

Offline ken1992

Junior Newbie





« Reply #2 - Posted 2012-11-08 04:54:58 »

Thank you for your reply Smiley
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 Cheesy
here's my rubbish code Sad


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  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
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();
      }

   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Agro
« Reply #3 - Posted 2012-11-08 04:59:18 »

Eh, try putting the code in a
1  
 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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (36 views)
2014-07-24 01:59:36

Riven (37 views)
2014-07-23 21:16:32

Riven (25 views)
2014-07-23 21:07:15

Riven (27 views)
2014-07-23 20:56:16

ctomni231 (57 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!