private void drawMesh(Graphics g) { try { for (Enumeration parts = previewTree.getMesh().elements(); parts.hasMoreElements(); ) { MeshPart m = (MeshPart) parts.nextElement(); if (m.getLevel() == previewTree.getShowLevel()) { g.setColor(thisLevelColor); } else { g.setColor(otherLevelColor); } drawMeshPart(g, m); } } catch (Exception e) { // System.err.println("nummer "+i); e.printStackTrace(); } }
public TreePreview(PreviewTree prvTree, int perspect) { super(); setMinimumSize(new Dimension(100, 100)); setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); setOpaque(true); setBackground(Color.WHITE); previewTree = prvTree; perspective = perspect; initRotation(); previewTree.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { repaint(); } }); }
public void paint(Graphics g) { if (previewTree.getMesh() == null) { try { previewTree.remake(); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } return; } Graphics2D g2 = (Graphics2D) g; // turn antialiasing on RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.addRenderingHints(rh); try { g.clearRect(0, 0, getWidth(), getHeight()); g.setColor(bgClr); if (perspective == PERSPECTIVE_FRONT) g.fillRect(0, 0, getWidth() - 1, getHeight()); else g.fillRect(0, 0, getWidth(), getHeight() - 1); // g.drawRect(10,10,getWidth()-20,getHeight()-20); initTransform(g); if (draft) previewTree.traverseTree(new StemDrawer(g)); // drawStems(g); else { drawMesh(g); // drawLeaves(g); previewTree.traverseTree(new LeafDrawer(g)); } // DEBUG // Enumeration e = previewTree.allStems(previewTree.getShowLevel()-1); // Stem stem = (Stem)e.nextElement(); // Vector diag = stem.getMaxPoint().sub(stem.getMinPoint()); // Vector orig = stem.getTransformation().getT(); // setOrigin(new Vector(orig.getX(),orig.getY(),0)); // Vector max = stem.getMaxPoint(); // Vector min = stem.getMinPoint(); // // // get greatest distance from orig // double x1 = Math.abs(min.getX())-Math.abs(orig.getX()); // double x2 = Math.abs(max.getX())-Math.abs(orig.getX()); // double x = Math.max(Math.abs(x1),Math.abs(x2)); // double y1 = Math.abs(min.getY())-Math.abs(orig.getY()); // double y2 = Math.abs(max.getY())-Math.abs(orig.getY()); // double y = Math.max(Math.abs(y1),Math.abs(y2)); // // Vector av = max.sub(orig).add(min.sub(orig)); // //av=av.mul(0.5); // //// double dw = Math.sqrt(x*x+y*y)*2; // double dw = Math.sqrt(av.getX()*av.getX()+av.getY()*av.getY())*2; // double minw = -dw/2; // // double dh = diag.getZ(); // double minh = min.getZ(); // // g.setColor(Color.RED); // drawLine(g,orig,min); // drawLine(g,orig,max); // // g.setColor(Color.GREEN); // drawLine(g,orig,orig.add(new Vector(dw/2,dw/2,0))); // // g.setColor(Color.BLUE); // drawLine(g,orig,av.add(orig)); //// drawLine(g,orig,new Vector(dw/4,dw/4,orig.getZ())); // //// g.setColor(Color.BLUE); //// drawLine(g,stem.getTransformation().getT(),stem.getMaxPoint().sub(stem.getMinPoint())); } catch (Exception e) { // do nothing, don't draw } }
private void initTransform(Graphics g) throws Exception { // Perform transformation transform = new AffineTransform(); double dw = 1; double minw = 0; double dh = 0; double minh = 0; double scale; double x; double y; // double abs; final int margin = 5; int showLevel = previewTree.getShowLevel(); class FindAStem extends DefaultTreeTraversal { Stem found = null; int level; public FindAStem(int level) { this.level = level; } public Stem getFound() { return found; } public boolean enterStem(Stem stem) { if (found == null && stem.stemlevel < level) return true; // look further else if (found != null || stem.stemlevel > level) return false; // found a stem or too deep else if (stem.stemlevel == level) found = stem; return true; } public boolean leaveTree(Tree tree) { return (found != null); } } if (showLevel < 1) { setOrigin(new Vector()); //////////// FRONT view if (perspective == PERSPECTIVE_FRONT) { // get width and height of the tree dw = previewTree.getWidth() * 2; dh = previewTree.getHeight(); minh = 0; minw = -dw / 2; ///////////////// TOP view } else { // get width of the tree dw = previewTree.getWidth() * 2; minw = -dw / 2; } } else { // find stem which to show /* Enumeration e = previewTree.allStems(showLevel-1); if (! e.hasMoreElements()) throw new Exception("Stem not found"); Stem stem = (Stem)e.nextElement(); */ Stem aStem = null; FindAStem stemFinder = new FindAStem(showLevel - 1); if (previewTree.traverseTree(stemFinder)) { aStem = stemFinder.getFound(); } if (aStem != null) { Vector diag = aStem.getMaxPoint().sub(aStem.getMinPoint()); Vector orig = aStem.getTransformation().getT(); setOrigin(new Vector(orig.getX(), orig.getY(), 0)); Vector max = aStem.getMaxPoint(); Vector min = aStem.getMinPoint(); // get greatest distance from orig x = Math.max(Math.abs(min.getX() - orig.getX()), Math.abs(max.getX() - orig.getX())); y = Math.max(Math.abs(min.getY() - orig.getY()), Math.abs(max.getY() - orig.getY())); dw = Math.sqrt(x * x + y * y) * 2; minw = -dw / 2; dh = diag.getZ(); minh = min.getZ(); } // DEBUG // System.err.println("O: "+ orig +" min: "+min+" max: "+max); // System.err.println("maxX: "+x+" maxY: "+y); // System.err.println("dg: "+diag+" dw: "+dw+" minw: "+minw+" dh: "+dh+" minh: "+minh); } //////////// FRONT view if (perspective == PERSPECTIVE_FRONT) { // how much to scale for fitting into view? scale = Math.min((getHeight() - 2 * margin) / dh, (getWidth() - 2 * margin) / dw); if (previewTree.params.debug) System.err.println("scale: " + scale); // shift to mid point of the view transform.translate(getWidth() / 2, getHeight() / 2); // scale to image height transform.scale(scale, -scale); // shift mid of the tree to the origin of the image transform.translate(-minw - dw / 2, -minh - dh / 2); ///////////////// TOP view } else { // how much to scale for fitting into view? scale = Math.min((getHeight() - 2 * margin) / dw, (getWidth() - 2 * margin) / dw); // shift to mid point of the view transform.translate(getWidth() / 2, getHeight() / 2); // scale to image height transform.scale(scale, -scale); // shift mid of the stem to the origin of the image transform.translate(-minw - dw / 2, -minw - dw / 2); } // DEBUG Point p = new Point(); transform.transform(new Point2D.Double(0.0, 0.0), p); if (previewTree.params.debug) { System.err.println("width: " + minw + "+" + dw); System.err.println("height: " + minh + "+" + dh); System.err.println("Origin at: " + p); System.err.println("view: " + getWidth() + "x" + getHeight()); } }