Cherrytree cn=? | Bon, voilà le truc : je cherche à créer un Pong en réseau. J'ai choisi d'utiliser RMI pour mettre des objets en partage. Pour l'instant je cherche à écrire un module de connection pour les joueurs. Chaque joueur ouvre une appli PongClient clique sur Join et ajoute son nom à la liste puis "launch". On lance aussi un PongServer qui crée l'objet distant RemoteList. Tout ça j'y arrive. Maintenant ce que je voudrais faire et je n'y arrive pas, c'est gérer le fait que lorsqu'un joueur fait "launch" ça réactualise les listes dans les menus Join des autres PongClient.
Code :
- //AboutDialog.java
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class AboutDialog extends JDialog implements ActionListener {
-
- public AboutDialog(Frame parent, String title, String message) {
- super(parent, title, true);
-
- if (parent != null) {
- Dimension parentSize = parent.getSize();
- Point p = parent.getLocation();
- setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
- }
-
- JPanel messagePane = new JPanel();
- messagePane.add(new JLabel(message));
- getContentPane().add(messagePane, BorderLayout.CENTER);
-
- JPanel buttonPane = new JPanel();
- JButton button = new JButton("OK" );
- buttonPane.add(button);
- button.addActionListener(this);
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- pack();
- setVisible(true);
- }
-
- public void actionPerformed(ActionEvent e) {
- setVisible(false);
- dispose();
- }
- }
- //PongClient.java
- import java.awt.*;
- import java.awt.event.*;
- public class PongClient {
- public PongClient() {
- }
-
- public static void main(String args[]) {
- theApp = new PongClient();
- theApp.init();
- }
-
- public void init() {
- window = new PongClientFrame(this, "Pong" );
- window.setBounds(0, 0, 640, 480);
- window.setResizable(false);
- window.setVisible(true);
- }
-
- public String getUser() {
- return user;
- }
-
- public void setUser(String user) {
- this.user = user;
- }
-
- public void windowClosing(WindowEvent e) {
- window.dispose();
- System.exit(0);
- }
-
- public void windowOpened(WindowEvent e) {}
- public void windowClosed(WindowEvent e) {}
- public void windowIconified(WindowEvent e) {}
- public void windowDeiconified(WindowEvent e) {}
- public void windowActivated(WindowEvent e) {}
- public void windowDeactivated(WindowEvent e) {}
-
- private static PongClientFrame window;
-
- private static PongClient theApp;
-
- private String user = "user";
- }
- //PongClientFrame.java
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class PongClientFrame extends JFrame implements ActionListener {
-
- public PongClientFrame(PongClient theApp, String title) {
- setTitle(title);
- this.theApp = theApp;
- setJMenuBar(menuBar);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
-
- JMenu gameMenu = new JMenu("Game" );
- JMenu helpMenu = new JMenu("Help" );
-
- gameMenu.setMnemonic('G');
- helpMenu.setMnemonic('H');
-
- joinAction = new GameAction("Join", KeyStroke.getKeyStroke('J', Event.CTRL_MASK));
- exitAction = new GameAction("Exit", KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
-
- addMenuItem(gameMenu, joinAction);
- addMenuItem(gameMenu, exitAction);
-
- aboutItem = createMenuItem("About" );
- helpMenu.add(aboutItem);
-
- rulesItem = createMenuItem("Rules" );
- helpMenu.add(rulesItem);
-
- aboutItem.setAccelerator(KeyStroke.getKeyStroke('A', Event.CTRL_MASK));
- rulesItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
-
- menuBar.add(gameMenu);
- menuBar.add(helpMenu);
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- }
-
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == aboutItem) {
- AboutDialog aboutDialog = new AboutDialog(this, "About", "Copyright 2002 Cherrytree" );
- }
- else if (e.getSource() == rulesItem) {
- }
- }
-
- private JMenuItem createMenuItem(String label) {
- JMenuItem item = new JMenuItem(label);
- item.addActionListener(this);
- return item;
- }
-
- private JMenuItem addMenuItem(JMenu menu, Action action) {
- JMenuItem item = menu.add(action);
-
- KeyStroke keystroke = (KeyStroke)action.getValue(action.ACCELERATOR_KEY);
- if(keystroke != null)
- item.setAccelerator(keystroke);
- return item;
- }
-
- private void showConnectDialog() {
- PongConnectDialog connectDialog = new PongConnectDialog(theApp, this, "Join", theApp.getUser());
- }
-
- class GameAction extends AbstractAction {
-
- GameAction(String name) {
- super(name);
- }
-
- GameAction(String name, KeyStroke keystroke) {
- this(name);
- if(keystroke != null)
- putValue(ACCELERATOR_KEY, keystroke);
- }
-
- public void actionPerformed(ActionEvent e) {
- String name = (String)getValue(NAME);
- if (name.equals(joinAction.getValue(NAME)))
- showConnectDialog();
- else if (name.equals(exitAction.getValue(NAME)))
- System.exit(0);
- }
- }
-
- private PongClient theApp;
-
- private JMenuBar menuBar = new JMenuBar();
- private GameAction joinAction, exitAction;
- private JMenuItem aboutItem, rulesItem;
- }
- //PongConnectDialog
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.rmi.*;
- import java.net.*;
- public class PongConnectDialog extends JDialog implements ActionListener {
-
- public PongConnectDialog(PongClient theApp, Frame parent, String title, String user) {
- super(parent, title, true);
- this.theApp = theApp;
- this.user = user;
- if (parent != null) {
- Dimension parentSize = parent.getSize();
- Point p = parent.getLocation();
- setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
- }
- setSize(new Dimension(180, 300));
-
- Box inputBox = Box.createHorizontalBox();
- JLabel userLabel = new JLabel("User: " );
- inputBox.add(userLabel);
- inputBox.add(userInput);
-
- Box buttonBox = Box.createHorizontalBox();
- addButton = new JButton("Add" );
- addButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
- addButton.addActionListener(new AddListener());
- buttonBox.add(addButton);
-
- removeButton = new JButton("Remove" );
- removeButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
- removeButton.setEnabled(false);
- removeButton.addActionListener(new RemoveListener());
- buttonBox.add(removeButton);
-
- Box userBox = Box.createVerticalBox();
- userBox.add(inputBox);
- userBox.add(buttonBox);
- getContentPane().add(userBox, BorderLayout.NORTH);
-
- try {
- RemoteListModel remoteList = (RemoteListModel)Naming.lookup("rmi://localhost/remoteList" );
- JList list = new JList(remoteList.getList());
- JScrollPane scrollPane = new JScrollPane(list);
- getContentPane().add(scrollPane, BorderLayout.CENTER);
- }
- catch (NotBoundException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- catch (MalformedURLException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- catch (RemoteException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
-
- Box launchBox = Box.createHorizontalBox();
- launchButton = new JButton("Launch" );
- launchButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
- launchButton.setEnabled(false);
- launchButton.addActionListener(new LaunchListener());
- launchBox.add(launchButton);
-
- cancelButton = new JButton("Cancel" );
- cancelButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
- cancelButton.addActionListener(this);
- launchBox.add(cancelButton);
-
- getContentPane().add(launchBox, BorderLayout.SOUTH);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setVisible(true);
- }
-
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == cancelButton) {
- setVisible(false);
- dispose();
- }
- }
-
- class AddListener implements ActionListener {
-
- public void actionPerformed(ActionEvent e) {
- user = new String(userInput.getText());
- if (user.equals("" ) || user.equalsIgnoreCase("user" )) {
- Toolkit.getDefaultToolkit().beep();
- return;
- }
- else {
- addButton.setEnabled(false);
- removeButton.setEnabled(true);
- launchButton.setEnabled(true);
- }
- }
- }
-
- class RemoveListener implements ActionListener {
-
- public void actionPerformed(ActionEvent e) {
- addButton.setEnabled(true);
- removeButton.setEnabled(false);
- launchButton.setEnabled(false);
- }
- }
-
- class LaunchListener implements ActionListener {
-
- public void actionPerformed(ActionEvent event) {
- try {
- theApp.setUser(user);
- RemoteListModel remoteList = (RemoteListModel)Naming.lookup("rmi://localhost/remoteList" );
- remoteList.addElement(user);
- setVisible(false);
- dispose();
- }
- catch (NotBoundException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- catch (MalformedURLException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- catch (RemoteException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
-
- private PongClient theApp;
-
- private String user;
- private JTextField userInput = new JTextField(user);
-
- private JButton addButton, removeButton, launchButton, cancelButton;
-
- private DefaultListModel users;
- }
- //PongServer.java
- import java.rmi.*;
- public class PongServer {
-
- public static void main(String[] args) {
- try {
- RemoteList remoteList = new RemoteList();
- Naming.rebind("rmi://localhost/remoteList", remoteList);
- }
- catch (Exception e) {
- System.err.println("PongServer exception: " + e.getMessage());
- e.printStackTrace();
- }
- }
- }
- //RemoteList.java
- import javax.swing.*;
- import java.rmi.*;
- import java.rmi.server.*;
- public class RemoteList extends UnicastRemoteObject implements RemoteListModel {
-
- public RemoteList() throws RemoteException {
- super();
- }
-
- public DefaultListModel getList() {
- return remoteList;
- }
-
- public void addElement(String name) {
- remoteList.addElement(name);
- }
-
- private DefaultListModel remoteList = new DefaultListModel();
- }
- //RemoteListModel.java
- import javax.swing.*;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- public interface RemoteListModel extends Remote {
- DefaultListModel getList() throws RemoteException;
- void addElement(String name) throws RemoteException;
- }
|
[jfdsdjhfuetppo]--Message édité par Cherrytree--[/jfdsdjhfuetppo] ---------------
Le site de ma maman
|