Narisa.com: Java Console ---> Applet - Narisa.com

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Java Console ---> Applet รบกวนหน่อยคะ Rate Topic: -----

#1 User is offline   kthoth 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-June 04

Posted 21 June 2004 - 01:58 PM

จะสามารถเขียน Applet ที่ทำหน้าที่เหมือน Java Console สามารถแสดงผลด้วยคำสั่ง System.out.println("XXXXXXXX"); ได้ไหมคะ :(
0

#2 User is offline   iWat 

  • Topgun
  • View blog
  • Group: Topgun
  • Posts: 3248
  • Joined: 05-February 04

  Posted 21 June 2004 - 04:25 PM

ถ้าทำงานคล้าย ๆ กับระบบ 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: 21 June 2004 - 07:56 PM

0

#3 User is offline   iWat 

  • Topgun
  • View blog
  • Group: Topgun
  • Posts: 3248
  • Joined: 05-February 04

  Posted 21 June 2004 - 07:56 PM

ลองศึกษา 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);
  }
}


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);
  }
}

0

#4 User is offline   kthoth 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-June 04

  Posted 22 June 2004 - 04:05 PM

:D ขอบคุณมากค่ะ
พี่เก่งจัง :o
0

#5 User is offline   bpitk 

  • Member
  • PipPip
  • Group: Members
  • Posts: 218
  • Joined: 21-May 05

Posted 22 December 2007 - 06:10 PM

ผมลองใช้
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: 22 December 2007 - 06:11 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users