package org.rosuda.javaXGD; import java.net.*; import java.io.*; import java.util.*; import java.awt.*; class XGDobject { public void paint(XGDcanvas c, Graphics g) {}; } class XGDcanvas extends Canvas { Vector l; Color fill; Color col; Font f; public XGDcanvas(int w, int h) { l=new Vector(); f=new Font(null,0,12); setSize(w,h); } public void add(XGDobject o) { l.add(o); } public void reset() { l.removeAllElements(); } public void paint(Graphics g) { int i=0, j=l.size(); g.setFont(f); while (ix2) { tmp=x1; x1=x2; x2=tmp; } if (y1>y2) { tmp=y1; y1=y2; y2=tmp; } this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; } public void paint(XGDcanvas c, Graphics g) { if (c.fill!=null) { g.setColor(c.fill); g.fillRect((int)x1,(int)y1,(int)(x2-x1),(int)(y2-y1)); if (c.col!=null) g.setColor(c.col); } if (c.col!=null) g.drawRect((int)x1,(int)y1,(int)(x2-x1),(int)(y2-y1)); } } class XGDcircle extends XGDobject { double x,y,r; public XGDcircle(double x, double y, double r) { this.x=x; this.y=y; this.r=r; } public void paint(XGDcanvas c, Graphics g) { if (c.fill!=null) { g.setColor(c.fill); g.fillOval((int)(x-r),(int)(y-r),(int)(r+r),(int)(r+r)); if (c.col!=null) g.setColor(c.col); } if (c.col!=null) g.drawOval((int)(x-r),(int)(y-r),(int)(r+r),(int)(r+r)); } } class XGDtext extends XGDobject { double x,y,r,h; String txt; public XGDtext(double x, double y, double r, double h, String txt) { this.x=x; this.y=y; this.r=r; this.h=h; this.txt=txt; } public void paint(XGDcanvas c, Graphics g) { if (c.col!=null) { double rx=x, ry=y; double hc=0d; if (h!=0d) { FontMetrics fm=g.getFontMetrics(); int w=fm.stringWidth(txt); hc=((double)w)*h; rx=x-(((double)w)*h); } int ix=(int)(rx+0.5), iy=(int)(ry+0.5); if (r!=0d) { Graphics2D g2d=(Graphics2D) g; g2d.translate(x,y); double rr=r/180d*Math.PI; g2d.rotate(rr); if (hc!=0d) g2d.translate(-hc,0d); g2d.drawString(txt,0,0); if (hc!=0d) g2d.translate(hc,0d); g2d.rotate(-rr); g2d.translate(-x,-y); } else g.drawString(txt,ix,iy); } } } class XGDfont extends XGDobject { double cex,ps,lineheight; int face; String family; Font font; public XGDfont(double cex, double ps, double lineheight, int face, String family) { this.cex=cex; this.ps=ps; this.lineheight=lineheight; this.face=face; this.family=family; int jFT=Font.PLAIN; if (face==2) jFT=Font.BOLD; if (face==3) jFT=Font.ITALIC; if (face==4) jFT=Font.BOLD|Font.ITALIC; font=new Font(family.equals("")?null:family, jFT, (int)(cex*ps+0.5)); } public void paint(XGDcanvas c, Graphics g) { g.setFont(font); c.f=font; } } class XGDpolygon extends XGDobject { int n; double x[],y[]; int xi[], yi[]; boolean isPolyline; public XGDpolygon(int n, double[] x, double[] y, boolean isPolyline) { this.x=x; this.y=y; this.n=n; this.isPolyline=isPolyline; int i=0; xi=new int[n]; yi=new int[n]; while (i>8)&255))/255f, ((float)((col>>16)&255))/255f, 1f-((float)((col>>24)&255))/255f); } public void paint(XGDcanvas c, Graphics g) { c.col=gc; if (gc!=null) g.setColor(gc); } } class XGDfill extends XGDobject { int col; Color gc; public XGDfill(int col) { this.col=col; if (col==-1) gc=null; else gc=new Color(((float)(col&255))/255f, ((float)((col>>8)&255))/255f, ((float)((col>>16)&255))/255f, 1f-((float)((col>>24)&255))/255f); } public void paint(XGDcanvas c, Graphics g) { c.fill=gc; } } class XGDlinePar extends XGDobject { double lwd; int lty; BasicStroke bs; public XGDlinePar(double lwd, int lty) { this.lwd=lwd; this.lty=lty; bs=null; if (lty==0) bs=new BasicStroke((float)lwd); else if (lty==-1) bs=new BasicStroke(0f); else { int l=0; int dt=lty; while (dt>0) { dt>>=4; l++; } float[] dash=new float[l]; dt=lty; l=0; while (dt>0) { int rl=dt&15; dash[l++]=(float)rl; dt>>=4; } bs=new BasicStroke((float)lwd, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3f, dash, 0f); } } public void paint(XGDcanvas c, Graphics g) { if (bs!=null) ((Graphics2D)g).setStroke(bs); } } public class XGDserver extends Thread { class XGDworker extends Thread { public Socket s; boolean isBE; XGDcanvas c; Frame f; int getInt(byte[] b, int o) { return (isBE)? ((b[o+3]&255)|((b[o+2]&255)<<8)|((b[o+1]&255)<<16)|((b[o]&255)<<24)) : ((b[o]&255)|((b[o+1]&255)<<8)|((b[o+2]&255)<<16)|((b[o+3]&255)<<24)); } long getLong(byte[] b, int offset) { long l1, l2; if (isBE) { l1=((long)getInt(b,offset+4))&0xffffffffL; l2=((long)getInt(b,offset))&0xffffffffL; } else { l1=((long)getInt(b,offset))&0xffffffffL; l2=((long)getInt(b,offset+4))&0xffffffffL; } return l1|(l2<<32); } double getDouble(byte[] b, int offset) { return Double.longBitsToDouble(getLong(b, offset)); } void setInt(int v, byte[] buf, int o) { if (!isBE) { buf[o]=(byte)(v&255); o++; buf[o]=(byte)((v&0xff00)>>8); o++; buf[o]=(byte)((v&0xff0000)>>16); o++; buf[o]=(byte)((v&0xff000000)>>24); } else { buf[o+3]=(byte)(v&255); buf[o+2]=(byte)((v&0xff00)>>8); buf[o+1]=(byte)((v&0xff0000)>>16); buf[o]=(byte)((v&0xff000000)>>24); } } void setLong(long l, byte[] buf, int o) { setInt((int)(l&0xffffffffL),buf,isBE?o+4:o); setInt((int)(l>>32),buf,isBE?o:o+4); } void setDouble(double d, byte[] buf, int o) { setLong(Double.doubleToLongBits(d),buf,o); } void dump(String s, byte[] b) { System.out.print(s); int i=0; while(i> 8; byte[] par=new byte[len]; if (len>0) { int n2=sis.read(par); if (n2!=len) { System.out.println("Needed "+len+" bytes, got "+n2); break; } } //dump("Got pars: ",par); if (cmd == 1) { double w=getDouble(par, 0); double h=getDouble(par, 8); if (f!=null) { f.removeAll(); f.dispose(); f=null; if (c!=null) c=null; } f=new Frame("xGD"); c=new XGDcanvas((int)w, (int)h); f.add(c); f.pack(); f.setVisible(true); } if (cmd == 2) { if (f!=null) { f.removeAll(); f.dispose(); f=null; if (c!=null) c=null; } return; } if (cmd == 9 && c!=null) { c.add(new XGDline(getDouble(par,0), getDouble(par,8), getDouble(par,16), getDouble(par,24))); } if (cmd == 15 && c!=null) { c.add(new XGDrect(getDouble(par,0), getDouble(par,8), getDouble(par,16), getDouble(par,24))); } if (cmd == 5 && c!=null) { c.add(new XGDcircle(getDouble(par,0), getDouble(par,8), getDouble(par,16))); } if (cmd == 11 && c!=null) { if (getInt(par, 0)==0) c.repaint(); } if (cmd == 12 && c!=null) { c.reset(); } if ((cmd == 13 || cmd == 14) && c!=null) { int pn=getInt(par,0); int i=0; double x[], y[]; x=new double[pn]; y=new double[pn]; while (i