글쓴이 :조신제 1998년 7월 25일 20:10:52
In Reply to: [질문] 조신제님께 (애플릿 프린트하는법) posted by 최종철 on 1998년 7월 25일 18:15:10:
제가 자바를 공부하면서 여기서 많은 정보를 얻어
갔거든요..
많이 얻어 가기만 하고 내가 주는 것을 너무 없는 것
같아서 요즘에는 여기 들어왔을 때 제가 아는게 있으면
몇자 적어 놓고 나갔는데...
좀 많았나 보죠? ^^;
최종철님께서 질문하신 내용이 조금 애매하네요..
애플릿을 띄웠을 때 그모양을 프린트하고 싶은건지,
아니면 애플릿에서 프린트 작업을 하고싶은건지 잘 모르
겠지만 일단 두가지를 다 제가 아는대로 말씀드려 보겠습니다.
애플릿을 띄운 모양을 프린트 하고 싶으면 윈도우 환경에서는
ALT + PrintScreen 버튼을 누르면 시스템 클립보드에
저장이 되니까 PhotoShop등의 그림툴에서 copy만 하면 되구요.
이게 아니라 프로그램을 이용하여 프린트 작업을 하고
싶다면 스트림(ObjectStream)을 통해 서버측으로 데이타를
보낸 후 서버가 프린트 작업을 해야 하는 걸로 알아요.
서버가 프린트작업을 하는 것을 어플리케이션에서 프린트
하는 것과 같으니까
아래의 프로그램을 참고하면 됩니다.
------< PrintExample.java >----------
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class PrintScribble extends Frame {
private short last_x = 0, last_y = 0; // last click posistion
private Vector lines = new Vector(256,256); // store the scribble
private Properties printprefs = new Properties(); // store user preferences
public PrintScribble() {
super("PrintScribble");
// Add a print button.
this.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
Button b = new Button("Print");
this.add(b);
// Call the print() method when the button is clicked.
// Note anonymous class.
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { print(); }
});
// Exit when the user closes the window.
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
// Register other event types we're interested in -- for scribbling
enableEvents(AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK);
// Set our initial size and pop the window up.
this.setSize(400, 400);
this.show();
}
/** Redraw (or print) the scribble based on stored lines */
public void paint(Graphics g)
{
for(int i = 0; i < lines.size(); i++) {
Line l = (Line)lines.elementAt(i);
g.drawLine(l.x1, l.y1, l.x2, l.y2);
}
}
/** Print out the scribble */
void print() {
// Obtain a PrintJob and a Graphics object to use with it
Toolkit toolkit = this.getToolkit();
PrintJob job = toolkit.getPrintJob(this, "PrintScribble", printprefs);
if (job == null) return; // If the user clicked Cancel in the print dialog
Graphics g = job.getGraphics();
// Give the output a larger top and left margin. Otherwise it will
// be scrunched up in the upper-left corner of the page.
g.translate(100, 100);
// Draw a border around the output area.
Dimension size = this.getSize();
g.drawRect(-1, -1, size.width+1, size.height+1);
// Set a clipping region so our scribbles don't go outside the border
// On-screen this happens automatically, but not on paper.
g.setClip(0, 0, size.width, size.height);
// Print this component and all components it contains
this.printAll(g); // Use print() if you don't want the button to show
// Finish up.
g.dispose(); // End the page
job.end(); // End the job
}
/** Called when the user clicks */
public void processMouseEvent(MouseEvent e)
{
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
last_x = (short)e.getX(); // remember click position
last_y = (short)e.getY();
}
else super.processMouseEvent(e);
}
/** Called when the the user drags the mouse: does the scribbling */
public void processMouseMotionEvent(MouseEvent e)
{
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
Graphics g = getGraphics();
g.drawLine(last_x, last_y, e.getX(), e.getY()); // draw the line
lines.addElement(new Line(last_x, last_y, // and save the line
(short) e.getX(), (short)e.getY()));
last_x = (short) e.getX(); last_y = (short) e.getY();
}
else super.processMouseMotionEvent(e);
}
/** The main method. Create a PrintScribble() object and away we go! */
public static void main(String[] args)
{
PrintScribble s = new PrintScribble();
}
/** This nested toplevel helper class stores the coordinates
* of one line of the scribble. */
class Line {
public short x1, y1, x2, y2;
public Line(short x1, short y1, short x2, short y2) {
this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2;
}
}
}
그리고 CardLayout예제는 아래를 참고하세요.
------< CardLayoutExample.java >----------
/*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutExample extends Applet {
public void init() {
// Create a layout manager, and save a reference to it for future use.
// This CardLayout leaves 10 pixels margins around the component.
final CardLayout cardlayout = new CardLayout(10, 10);
// Specify the layout manager for the applet
this.setLayout(cardlayout);
for(int i = 1; i <= 9; i++) {
Button b = new Button("Button #" + i);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) { // When button is clicked
cardlayout.next(CardLayoutExample.this); // display the next one.
}
};
b.addActionListener(listener);
this.add("Button" + i, b); // Specify a name for each component
}
}
}
많은 도움이 되었으면 좋겠네요....
^^;