IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Java Console ---> Applet, รบกวนหน่อยคะ
kthoth
post Jun 21 2004, 01:58 PM
Post #1


Newbie
*

Group: Members
Posts: 2
Joined: 21-June 04
Member No.: 1924



จะสามารถเขียน Applet ที่ทำหน้าที่เหมือน Java Console สามารถแสดงผลด้วยคำสั่ง System.out.println("XXXXXXXX"); ได้ไหมคะ sad.gif
Go to the top of the page
 
+Quote Post
iWat
post Jun 21 2004, 04:25 PM
Post #2


Topgun
Group Icon

Group: Topgun
Posts: 3158
Joined: 5-February 04
From: On your monitor
Member No.: 1281



ถ้าทำงานคล้าย ๆ กับระบบ Console คุณ ktoth ต้องจัดการกับ Stream ของคลาส System ครับ คือ System.out และอาจจะแถมไปที่ System.err ด้วยก็ได้
โดยใช้ Statis Method
System.setOut(PrintStream out)
System.setErr(PrintStream err)

จากนั้นก็สร้างคลาสสักตัวที่ extends OutputStream
แล้วทำการ override method ที่ชื่อว่า write(int b)
แล้วก็ System.setOut(new PrintStream(new MyOutputStream())

PrintStream เดิมของคลาส System จะเป็น Native Method ส่งข้อความไปยัง Console คุณก็เขียนให้ write(int b) วาดรูปขึ้นบน Applet แทน ...

วิธีนี้น่าจะช่วยได้นะครับ

This post has been edited by iWat: Jun 21 2004, 07:56 PM
Go to the top of the page
 
+Quote Post
iWat
post Jun 21 2004, 07:56 PM
Post #3


Topgun
Group Icon

Group: Topgun
Posts: 3158
Joined: 5-February 04
From: On your monitor
Member No.: 1281



ลองศึกษา code นี้ดูนะครับ เชื่อว่าคงช่วยได้บ้าง

CODE
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class HelloWorld extends JApplet {
 private StringBuffer data = new StringBuffer();
 
 public void init() {
   PrintStream p = new PrintStream(new AppletOutputStream(this));
   System.setOut(p);
   
   JButton but = new JButton("Test");
   but.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Hello World");
     }
   });
   
   this.setLayout(new FlowLayout());
   this.add(but);
 }

 public void printchar(int b) {
   data.append((char) b);
   repaint();
 }
 
 public void paint(Graphics g) {
   g.drawString(data.toString(), 50, 100);
 }
}


CODE
import java.io.*;

public class AppletOutputStream extends OutputStream {
 private HelloWorld a;

 public AppletOutputStream(HelloWorld a) {
   this.a = a;
 }
 
 public void write(int b) {
   a.printchar(b);
 }
}
Go to the top of the page
 
+Quote Post
kthoth
post Jun 22 2004, 04:05 PM
Post #4


Newbie
*

Group: Members
Posts: 2
Joined: 21-June 04
Member No.: 1924



biggrin.gif ขอบคุณมากค่ะ
พี่เก่งจัง ohmy.gif
Go to the top of the page
 
+Quote Post
bpitk
post Dec 22 2007, 06:10 PM
Post #5


Member
**

Group: Members
Posts: 210
Joined: 21-May 05
Member No.: 3669



ผมลองใช้
CODE
PrintStream myPrintStream = new PrintStream(new myOutput());
System.setOut(myPrintStream);
System.setErr(myPrintStream);
...
class myOutput extends OutputStream {
     public void write(int c) {
         char a[] = new char[1];
         a[0] = (char)c;
         myProgram.this.myTextArea.append(new String(s));
}

จะเจอ warning ว่า deprecated API ตรง new PrintStream ครับ
-จะแก้ warning ใช้คำสั่งอะไรดี
-และก็มีวิธี append int เป็น char ลง AWT TextArea โดยตรงมั้ยครับ ขอบคุณครับ

This post has been edited by bpitk: Dec 22 2007, 06:11 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 9th February 2010 - 09:14 AM