±Û¾´ÀÌ :ÀÌä¿í 1999³â 5¿ù 28ÀÏ 17:23:47
Àú´Â Áö±Ý µ¥ÀÌÅ͸¦ °¡Á®¿Í¼ ±×·¡ÇÁ¸¦ ±×¸®°í ,
ȸéÀ» Ãâ·ÂÇÏ´Â °£´ÜÇÑ ¾îÇø®ÄÉÀ̼ÇÀ» °³¹ßÇϰí Àִµ¥,
ÇÁ¸°Å͸¦ ÇÒ·Á°í Çϴµ¥ ¾Æ·¡¿Í °°Àº ¹®Á¦°¡ ¹ß»ýÇÕ´Ï´Ù.
¾Æ·¡ ÇÁ·Î±×·¥Àº ÇÁ¸°ÅͰü·Ã ¼Ò½º·Î °Ô½ÃÆÇ¿¡ ¿Ã¶ó¿Í ÀÖ´Â °ÍÀε¥
Á¦°¡ Àß ¸ô¶ó¼ ±×·±Áö paint() ¸Þ¼Òµå¿¡¼
graphics ¸¦ ¸î°³ Ãß°¡ÇÏ¿© ½ºÆ®¸µÀ» ȸ鿡 ±×·Á ÁÖ¾ú´Âµ¥
ȸ鿡´Â Àß ³ªÅ¸³ª´Âµ¥ ,
ÇÁ¸°ÅÍ·Î Ãâ·ÂÀ» ÇÏ¸é ¸¶Áö¸·ÀÇ g.drawString("AAAAAAAD",100,100); ¿¡¼ Ãâ·ÂÇÑ
¹®ÀÚ¸¸ ³ªÅ¸³ª°í ³ª¸ÓÁö´Â Çϳªµµ ³ªÅ¸³ªÁö ¾Ê½À´Ï´Ù..
¿Ö ±×·±Áö ¾Æ½Ã´Â ºÐÀº Á¦¹ß Á» µµ¿Í ÁֽʽÿÀ...
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class PrintExample extends Frame
{
private short last_x =0,last_y =0; //last click position
private Vector lines = new Vector(256,256); //store the scribble
private Properties printprefs = new Properties(); //store user preferences
public PrintExample()
{
super("PrintExample");
//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);
}
g.drawString("adddfafdadfaf",200,200);
g.drawString("AAAAAAAD",100,100);
}
/**Print out the scribble */
void print()
{
//Obtain a PrintJob and a Graphics object to user with it
Toolkit toolkit = this.getToolkit();
PrintJob job = toolkit.getPrintJob(this,"PrintExample",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,
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 wand 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 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
//and save the line
lines.addElement(new Line(last_x,last_y,(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)
{
PrintExample s = new PrintExample();
}
/** This nested top level helper class stores the coordinates
of on 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;
}
}
}