 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tony_P Guest
|
Posted: Mon Feb 20, 2006 6:12 pm Post subject: Stream Classes |
|
|
HI,Can one of you java girls/boys help me, stuck on project...
Tony_P )
---------------------------------------------------------
This is what i've got to do!!!
I have got modify my SendAndClearHandler.class
Add a field of type DataInputStream, then add an OutputStream argument to
your constructor, chain the OutputStream argument to my DataOutputStream.
Then modify my actionPerformed() method so that text in the TextField is
sent to the DataOutputStream using writeUTF() method.
clear text as before, but do not append the String to the TextArea, This
will be done somewhere else.
Then I have got modify my MessageReader.class
Add a field of type DataInputStream, then add an InputStream argument to
your constructor, chain the InputStream argument to my DataInputStream.
within the Run() method, use readUTF() method of my DataInputStream field to
read in a String from the stream.
Instead of appending "Hello, Thread" to TextArea, append the String from the
call to readUTF().
Then I have got modify my ChatWindow.class
Before instantiating your MessageReader and SendAndClearHandler objects,
create a File object associated with a file called "chat.txt".
Instantiate a FileOutputStream & FileInputStream object using you File
object.
Use the FileOutputStream object as the argument to the constructor
when instantiating your SendAndClear object
Use the FileInputStream object as the argument to the constructor
when instantiating your MessageReader object
Save, compile and run the ChatWindow program.
WHAT YOU SHOULD SEE
----------------------------------------------------------
Typing in the TEXTFIELD and clicking "Send" will send the String to a file.
This file is being read by a thread that places the string into your
TextArea
----------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SendAndClearHandler implements ActionListener
{
private TextArea ta2;
private TextField tf2;
// Declaring Field of type DataOutputStream
private DataOutputStream dataout;
//Adding outstr argument to constructor
public SendAndClearHandler(TextArea ta2, TextField tf2, OutputStream
outstr, FileOutputStream fileout)
{
// chainning DataOutputStream to outstr argument
this.dataout = new DataOutputStream(outstr);
this.ta2 = ta2;
this.tf2 = tf2;
}
public void actionPerformed(ActionEvent a)
{
if(a.getActionCommand() == "Send" || a.getSource() == tf2)
{
try
{
dataout.writeUTF("tf2");
dataout.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
else if(a.getActionCommand() == "Clear"){ta2.setText("");
{
//tf2.setText();
}
}
else if(a.getActionCommand() == "Enter" || a.getSource() == tf2)
{
try
{
DataOutputStream tf2 =
new DataOutputStream(dataout);
dataout.writeUTF("tf2");
dataout.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
}
}
----------------------------------------------------------
import java.awt.TextArea;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class MessageReader implements Runnable
{
private TextArea ta4;
//Declaring Field of type DataInputStream
private DataInputStream datain;
public MessageReader(TextArea ta4, InputStream instr, FileInputStream
filein)
{
// chainning DataInputStream to InputStream args...
this.datain = new DataInputStream(instr);
// creating txt area number 4
this.ta4 = ta4;
}
public void run()
{
while(true)
{
try
{
DataInputStream ta4 =
new DataInputStream(datain);
String in = ((DataInputStream) ta4).readUTF();
System.out.print(in + "Hello, Thread");
datain.readUTF();
datain.available();
datain.close();
}catch(IOException e)
{
e.printStackTrace();
}
this.ta4.setText(ta4.toString());
/* THIS THREAD WILL SLEEP FOR 50 SECONDS
*
* NUMBER IN MILLISECONDS >-- 5000
*/
// TRY/CATCH BLOCK HERE
try
{
Thread.sleep((long)(Math.random()*5000 + 1));
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
----------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ChatWindow extends Frame
{
private Button clear;
private Button send;
private Button quit;
private TextField tf1;
private Choice combo;
private TextArea ta1;
public ChatWindow(String title)
{
super(title);
/*
* (non-Javadoc)
*
* pan1, 1st panel
*/
Panel pan1 = new Panel();
/*
* (non-Javadoc)
*
* pan2, 2nd panel
*/
Panel pan2 = new Panel();
/*
* (non-Javadoc)
*
* This code is setting panel 2, to borderlayout manager
*/
pan2.setLayout(new BorderLayout ());
/*
* (non-Javadoc)
*
* This line of code is setting panel 1, to Gridlayout manager
*/
pan1.setLayout(new GridLayout (3,1));
/*
* (non-Javadoc)
*
* This line of code is declairing main txt field
*/
tf1 = new TextField(80);
/*
* (non-Javadoc)
*
* This line of code is declairing choice list
*/
combo = new Choice();
/*
* (non-Javadoc)
*
* This code is adding three buttons to 1st panel
*/
clear = new Button ("Clear");
pan1.add(clear,
BorderLayout.WEST);
send = new Button ("Send");
pan1.add(send,
BorderLayout.WEST);
quit = new Button ("quit");
pan1.add(quit,
BorderLayout.WEST);
/*
* (non-Javadoc)
*
* This line of code is adding choice list to 2nd panel
*/
pan2.add(combo, BorderLayout.WEST);
/*
* (non-Javadoc)
*
* This code is adding choice box options
*/
combo.addItem("brb");
combo.addItem("lol");
combo.addItem("cul8r");
/*
* (non-Javadoc)
*
* This code is adding a text field to 2nd panel
*/
pan2.add(tf1, BorderLayout.CENTER);
/*
* (non-Javadoc)
*
* This line of code is declairing text area
*/
ta1 = new TextArea();
this.add(pan1, BorderLayout.WEST);
this.add(ta1, BorderLayout.CENTER);
this.add(pan2, BorderLayout.SOUTH);
/*
* (non-Javadoc)
*
* LAB 6_2
*
* Creating an instance QuitChat
*/
QuitChat qc = new QuitChat();
/*
* (non-Javadoc)
*
* This line of code is adding Frame to ActionListener, by calling
* it this
*/
this.addWindowListener(qc);
/*
* This line of code is adding quit to ActionListener
*/
quit.addActionListener(qc);
/*
* (non-Javadoc)
*
* Creating an instance of SendAndClearHandler
* With two arguments TextArea & TextField
*/
SendAndClearHandler sch = new SendAndClearHandler(this.ta1,
this.tf1, null, null);
/*
* (non-Javadoc)
*
* Creating an instance of ChoiceHandler
* With one argument TextArea
*/
ChoiceHandler ch = new ChoiceHandler(this.ta1);
/*
* (non-Javadoc)
*
* This line of code is adding TextField to a listener,
* & then adding handler
*/
this.tf1.addActionListener(sch);
this.combo.addItemListener(ch);
/*
* (non-Javadoc)
*
* This line of code is adding send to a listener,
* & then adding handler
*/
send.addActionListener(sch);
/*
* (non-Javadoc)
*
* This line of code is adding clear to a listener,
* & then adding handler
*/
clear.addActionListener(sch);
/*
* (non-Javadoc)
*
* Local variable
*/
Button enter = send;
/*
* (non-Javadoc)
*
* This line of code is adding enter to a listener,
* & then adding handler
*/
enter.addActionListener(sch);
// Instantiating an MessageReader object
MessageReader mr = new MessageReader(ta1, null, null);
Thread t1 = new Thread(mr);
t1.start();
//...
try
{
File file = new File("chat.txt");
FileOutputStream fileout = new FileOutputStream(file);
FileInputStream filein = new FileInputStream(file);
}catch(IOException e){
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* Declairing main
*/
public static void main(String [] args)
{
ChatWindow cw = new ChatWindow("Hello, ChatWindows");
cw.setSize(500,400);
cw.setVisible(true);
}
}
----------------------------------------------------------
This is the exceptions i am getting when i complie my code
----------------------------------------------------------
java.lang.NullPointerException
at java.io.FilterInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at MessageReader.run(MessageReader.java:2
at java.lang.Thread.run(Unknown Source) |
|
| Back to top |
|
 |
Thomas Weidenfeller Guest
|
Posted: Tue Feb 21, 2006 4:12 pm Post subject: Re: Stream Classes |
|
|
Roedy Green wrote:
| Quote: | The whole idea of homework is YOU do it so YOU learn something. Asking
others to do your homework like asking them to workout on the Nautilus
equipment for you.
|
I think a week or so ago I asked him to spend at least some effort to
explain to us what that strange program is supposed to do. I didn't get
an answer. Maybe I was asking for to much or it can't be that urgent.
/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/ |
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue Feb 21, 2006 4:12 pm Post subject: Re: Stream Classes |
|
|
On Mon, 20 Feb 2006 17:36:45 GMT, "Tony_P"
<tony.prowse (AT) blueyonder (DOT) co.uk> wrote, quoted or indirectly quoted
someone who said :
| Quote: | This is what i've got to do!!!
|
see .http://mindprod.com/jgloss/homework.html
The whole idea of homework is YOU do it so YOU learn something. Asking
others to do your homework like asking them to workout on the Nautilus
equipment for you.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|