/* * VTPaint.java (very tiny paint program (guestbook)) * (C) 1996,1997,1998 Hiroshi Okada. * You can use this source code without permission. * last update 98.5.06 * * (compile it JDK 1.0.2) */ import java.awt.*; import java.applet.Applet; import java.util.*; import java.io.*; import java.net.*; // ------------------------------------------------------------------------ // * Constants // ------------------------------------------------------------------------ // interface VTPConst { final static int OFFSET = 16; // offset value to add final static int RADIX = 32; // read/write data radix final static int CODE_PENDOWN = 9999; // this vaule must not same as other code. final static int TOOL_PEN1 = 0; // tool code final static int TOOL_PEN2 = 1; final static int TOOL_DOT1 = 2; final static int TOOL_DOT2 = 3; final static int NUMBER_OF_TOOLS = 4; final static int BASE_WIDTH = 16; // unit size (tool switch & color switch) final static int BASE_HIGHT = 16; final static int DOT1_SIZE = 8; // painting dot size final static int DOT2_SIZE = 12; // painting dot size final static Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; final static int CANVAS_W = 360; // canvas width & height final static int CANVAS_H = 40; final static int HR_W = 360; // Horizontal line width (edit/view area sepalator) final static int HR_H = 4; // height final static int READ_N = 5; // How many image read final static int MAXSTEP = 800; // max painting steps final static String LEVEL_STR = "*****"; final static String WRITE_CGI = "write.cgi"; final static String READ_CGI = "read.cgi"; } // ------------------------------------------------------------------------ // * color selection switch // ------------------------------------------------------------------------ // final class VTPColorSwich extends Canvas implements VTPConst{ int select = 0; public VTPColorSwich() { resize( colors.length*BASE_WIDTH, BASE_HIGHT); } // The number of the chosen color is returned int getColorNum() { return select; } public void paint( Graphics g) { // Each fillRect are drawn by each color. for( int i=0; i=0) && ( ((Integer)trace.elementAt(i)).intValue() != CODE_PENDOWN ) ){ trace.removeElementAt( i); i--; } if( i>=0){ trace.removeElementAt( i); } reInterpretation(); colorNum = sv_colorNum; toolNum = sv_toolNum; } public void update( Graphics g) { paint( g); } public void paint( Graphics g) { if( offImage == null){ offImage_init(); } g.drawImage( offImage, 0,0, this); } // A line is drawing on offImage void draw_line_to( int x, int y){ if( offImage == null){ offImage_init(); } offGraphics.setColor( colors[ colorNum]); switch( toolNum){ case TOOL_PEN1 : offGraphics.drawLine( x0,y0,x,y); break; case TOOL_PEN2 : offGraphics.drawLine( x0,y0,x,y); offGraphics.drawLine( x0+1,y0,x+1,y); offGraphics.drawLine( x0,y0+1,x,y+1); offGraphics.drawLine( x0+1,y0+1,x+1,y+1); break; case TOOL_DOT1 : offGraphics.fillOval( x0-DOT1_SIZE/2, y0-DOT1_SIZE/2, DOT1_SIZE, DOT1_SIZE); break; case TOOL_DOT2 : offGraphics.fillOval( x0-DOT2_SIZE/2, y0-DOT2_SIZE/2, DOT2_SIZE, DOT2_SIZE); break; } x0 = x; y0 = y; } // Reevaluate "trace". void reInterpretation(){ int x,y,v; offImage_init(); x0 = y0 = 0; for (Enumeration e = trace.elements() ; e.hasMoreElements() ;){ switch( v = ((Integer)e.nextElement()).intValue() ){ case CODE_PENDOWN : colorNum = ((Integer)e.nextElement()).intValue(); toolNum = ((Integer)e.nextElement()).intValue(); x = ((Integer)e.nextElement()).intValue(); y = ((Integer)e.nextElement()).intValue(); x0 = x; y0=y; draw_line_to( x,y); break; default : x = x0 + v; y = y0 + ((Integer)e.nextElement()).intValue(); draw_line_to( x, y); break; }// end switch }// end for repaint(); } public boolean mouseDown(Event evt, int x, int y){ if( maxstep <= trace.size()){ return true; } trace.addElement( new Integer( CODE_PENDOWN)); trace.addElement( new Integer( colorNum)); trace.addElement( new Integer( toolNum)); trace.addElement( new Integer( x)); trace.addElement( new Integer( y)); x0 = x; y0=y; draw_line_to( x,y); repaint(); return true; } public boolean mouseDrag(Event evt, int x, int y){ if( maxstep <= trace.size()){ return true; } if( Math.abs(x-x0) + Math.abs(y-y0) < 3){ return true; } trace.addElement( new Integer(x-x0)); trace.addElement( new Integer(y-y0)); draw_line_to( x,y); repaint(); return true; } Vector getContents(){ return trace; } } public final class VTPaint extends Applet implements Runnable,VTPConst{ VTPCanvas vtpc = new VTPCanvas(); VTPCanvas vtpc_r[] = new VTPCanvas[ READ_N]; VTPColorSwich vtpcs = new VTPColorSwich(); VTPToolSwich vtpts = new VTPToolSwich(); Canvas hr= new Canvas(); Button prevBtn = new Button("<-"); Button nextBtn = new Button("->"); Button undobtn = new Button("Undo"); Button okbtn = new Button("OK"); Label steplabel = new Label( LEVEL_STR); int readPos = 9999; public void init() { add( vtpc ); add( vtpcs); add( vtpts); add( undobtn); add( okbtn); add( steplabel); hr.resize( HR_W, HR_H); hr.setBackground( Color.black); add( hr); add( prevBtn); add( nextBtn); for( int i=0; i 1){ s = "/" + s + "/"; } return s; } // vector to string String v2s( Vector v){ int i; String s = ""; for (Enumeration e = v.elements() ; e.hasMoreElements() ;){ i = ((Integer)e.nextElement()).intValue(); s = s + i2s( i); } return s; } // string to vector Vector s2v( String s){ String nums; Vector v = new Vector(); int n; for( int i=0; i"); try { URL u = new URL(getCodeBase(), WRITE_CGI); URLConnection uc = u.openConnection(); uc.setDoOutput( true); // use POST method PrintStream ps = new PrintStream( new BufferedOutputStream( uc.getOutputStream() ) ); ps.println( outs ); ps.close(); getAppletContext().showStatus(""); int i = 0; DataInputStream dis = new DataInputStream( new BufferedInputStream( uc.getInputStream() ) ); String ins = dis.readLine(); readPos = Integer.parseInt( ins); while( ins != null){ ins = dis.readLine(); if( (i"); try { URL u = new URL( getCodeBase(), READ_CGI); URLConnection uc = u.openConnection(); uc.setDoOutput( true); // use POST method PrintStream ps = new PrintStream( new BufferedOutputStream( uc.getOutputStream() ) ); ps.println( readPos); ps.close(); int i = 0; DataInputStream dis = new DataInputStream( new BufferedInputStream( uc.getInputStream() ) ); String ins = dis.readLine(); readPos = Integer.parseInt( ins); while( ins != null){ ins = dis.readLine(); if( (i