Marauroa Chat Tutorial: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>Merlin No edit summary |
imported>Merlin No edit summary |
||
| Line 267: | Line 267: | ||
handler.apply(message, world_objects); |
handler.apply(message, world_objects); |
||
} catch (java.lang.Exception e) { |
} catch (java.lang.Exception e) { |
||
// Something weird happened while applying perception |
|||
logger.error(e); |
|||
} |
} |
||
} |
} |
||
| Line 423: | Line 423: | ||
Don't forget to replace login and password with the actual ones. To create a new account just add a third command-line parameter (that should be an email, but no validation at the moment). |
Don't forget to replace login and password with the actual ones. To create a new account just add a third command-line parameter (that should be an email, but no validation at the moment). |
||
=== Output === |
|||
Ideally you should see something like |
|||
<pre> |
|||
>java -cp marauroa.jar;log4j.jar;. Test test1 test1 |
|||
Cannot find log4j.properties in classpath. Using default properties. |
|||
0.5 |
|||
Do not contact us! |
|||
Marauroa server |
|||
Chat |
|||
*test1* : test50 |
|||
*test1* : test150 |
|||
</pre> |
|||
Note the message about log4j.properties: you can create that file in order to configure the Log4J usage inside Marauroa framework. |
|||
== Swing client == |
== Swing client == |
||
=== Code === |
|||
TBD |
|||
The design of our Client class allows us to easily use it in something more complex then Test class above. Here is the example of a simple Swing application that uses Client as a communication tool |
|||
<pre> |
|||
import javax.swing.*; |
|||
import java.awt.event.*; |
|||
import java.io.IOException; |
|||
import java.net.URL; |
|||
import java.util.*; |
|||
import java.awt.*; |
|||
public class View extends JFrame implements ActionListener { |
|||
private javax.swing.JButton jSayButton; |
|||
private javax.swing.JButton jConnectButton; |
|||
private javax.swing.JScrollPane jScrollPane1; |
|||
private javax.swing.JTextArea jChatArea; |
|||
private javax.swing.JTextField jInputField; |
|||
private javax.swing.Timer timer; |
|||
private Client client = null; |
|||
public View() { |
|||
initComponents(); |
|||
jSayButton.addActionListener( |
|||
new ActionListener() { |
|||
public void actionPerformed(ActionEvent e) { |
|||
String message = jInputField.getText(); |
|||
jInputField.setText(""); |
|||
client.SendMessage(message); |
|||
} |
|||
}); |
|||
jConnectButton.addActionListener( |
|||
new ActionListener() { |
|||
public void actionPerformed(ActionEvent e) { |
|||
try { |
|||
client = Client.get(); |
|||
client.connect("127.0.0.1", 555); |
|||
client.login("test1", "test1"); |
|||
client.chooseCharacter("test1"); |
|||
jSayButton.setEnabled(true); |
|||
jInputField.setEnabled(true); |
|||
} catch (Exception exception) { |
|||
JOptionPane.showMessageDialog( |
|||
View.this, "Error", exception.toString(), JOptionPane.WARNING_MESSAGE); |
|||
client = null; |
|||
} |
|||
} |
|||
}); |
|||
timer = new javax.swing.Timer(300, this); |
|||
timer.setInitialDelay(500); |
|||
timer.start(); |
|||
setVisible(true); |
|||
} |
|||
public void actionPerformed(ActionEvent e) { |
|||
if (client != null) { |
|||
client.loop(0); |
|||
String s = client.popQuote(); |
|||
while (s != null) { |
|||
jChatArea.append(s + "\n"); |
|||
s = client.popQuote(); |
|||
} |
|||
} |
|||
} |
|||
private void initComponents() { |
|||
jScrollPane1 = new javax.swing.JScrollPane(); |
|||
jChatArea = new javax.swing.JTextArea(); |
|||
jSayButton = new javax.swing.JButton(); |
|||
jConnectButton = new javax.swing.JButton(); |
|||
jInputField = new javax.swing.JTextField(); |
|||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
|||
setTitle("Chat 0.5"); |
|||
setName("main"); |
|||
jChatArea.setColumns(20); |
|||
jChatArea.setEditable(false); |
|||
jChatArea.setRows(5); |
|||
jScrollPane1.setViewportView(jChatArea); |
|||
jSayButton.setText("Say"); |
|||
jSayButton.setEnabled(false); |
|||
jConnectButton.setText("Connect"); |
|||
jInputField.setEnabled(false); |
|||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); |
|||
getContentPane().setLayout(layout); |
|||
layout.setHorizontalGroup( |
|||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|||
.addContainerGap() |
|||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) |
|||
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE) |
|||
.addGroup(layout.createSequentialGroup() |
|||
.addComponent(jInputField, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE) |
|||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|||
.addComponent(jSayButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE) |
|||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|||
.addComponent(jConnectButton))) |
|||
.addContainerGap()) |
|||
); |
|||
layout.setVerticalGroup( |
|||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|||
.addContainerGap() |
|||
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE) |
|||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
|||
.addComponent(jConnectButton) |
|||
.addComponent(jSayButton) |
|||
.addComponent(jInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
|||
.addContainerGap()) |
|||
); |
|||
pack(); |
|||
} |
|||
} |
|||
</pre> |
|||
To create the layout of the elements NetBeans was used, but we concentrate just on the source code here. |
|||
The client establishes a connection to a hard-coded location upon Connect button is pressed. There is no "Create new account" functionality, so account should be created beforehand. |
|||
We use timer to regularly check for the new messages in the Client queue. If there are some - they are put to the large text box. Sending the new message also relies on the Client facilities. |
|||
The only thing left for the Swing client now is a main method. Here is the source code |
|||
<pre> |
|||
final class Chat { |
|||
private Chat() {} |
|||
public static void main( String[] args ) { |
|||
new View(); |
|||
} |
|||
} |
|||
</pre> |
|||
=== Deployment === |
|||
You still use the same command for compilation |
|||
<pre> |
|||
javac -cp marauroa.jar;log4j.jar;. *.java |
|||
</pre> |
|||
Make sure that source files with code for Swing client are in the same directory with jars mentioned and Client.java file. You can run the client with the following command |
|||
<pre> |
|||
java -cp marauroa.jar;log4j.jar;. Chat |
|||
</pre> |
|||
== TODO == |
== TODO == |
||
As soon as you finished reading this tutorial you would probably like to make one of this exercises. You are welcome to introduce your results back to the tutorial. |
As soon as you finished reading this tutorial you would probably like to make one of this exercises. You are welcome to introduce your results back to the tutorial. |
||