public void drawStar(int xpos, int ypos, double rot, double xscale, double yscale) { g.translate(xpos, ypos); g.rotate(rot * Math.PI / 180.0); g.scale(xscale, yscale); g.fillOval(-10, -10, 20, 20); g.scale(1.0 / xscale, 1.0 / yscale); g.rotate(-rot * Math.PI / 180.0); g.translate(-xpos, -ypos); }
public void drawCircle(int xpos, int ypos, double rot, double xscale, double yscale, int width) { g.translate(xpos, ypos); g.rotate(rot * Math.PI / 180.0); g.scale(xscale, yscale); g.fillOval(-width / 2, -width / 2, width, width); g.scale(1.0 / xscale, 1.0 / yscale); g.rotate(-rot * Math.PI / 180.0); g.translate(-xpos, -ypos); }
public void drawRocket( Image image, int xpos, int ypos, double rot, double xscale, double yscale) { int width = rocketImage.getWidth(this); int height = rocketImage.getHeight(this); g.translate(xpos, ypos); g.rotate(rot * Math.PI / 180.0); g.scale(xscale, yscale); g.drawImage(image, -width / 2, -height / 2, width, height, this); g.scale(1.0 / xscale, 1.0 / yscale); g.rotate(-rot * Math.PI / 180.0); g.translate(-xpos, -ypos); }
@Override public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } int i = pf.getOrientation(); // get the size of the page double pageWidth = pf.getImageableWidth(); double pageHeight = pf.getImageableHeight(); double myWidth = this.getWidth(); // - borderWidth * 2; double myHeight = this.getHeight(); // - borderWidth * 2; double scaleX = pageWidth / myWidth; double scaleY = pageHeight / myHeight; double minScale = Math.min(scaleX, scaleY); Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.scale(minScale, minScale); drawPlot(g); return PAGE_EXISTS; }
public void paint(Graphics g) { gRef = (Graphics2D) g; // change size of font gRef.setFont(gRef.getFont().deriveFont(9.0f)); fmRef = g.getFontMetrics(); // Clear background if (Preferences.monochrome) { gRef.setColor(Preferences.whiteColor); } else { gRef.setColor(Preferences.backgroundColor); } gRef.fillRect(0, 0, getWidth(), getHeight()); // set colour to correct drawing colour if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { gRef.setColor(Preferences.penColor); } gRef.translate(0, margin); // Call c code to draw tree gRef.scale(scale, scale); nativeDrawTree(); }
/** * Override the pain method do draw the axis lines * * @param g The graphics to paint to */ public void paint(Graphics g) { Rectangle b = getBounds(); g.setColor(canvasBg); g.fillRect(0, 0, b.width, b.height); paintGrid(g); Point center = getCenter(); if (g instanceof Graphics2D) { ((Graphics2D) g).translate(center.x, center.y); } super.paint(g); if (g instanceof Graphics2D) { ((Graphics2D) g).scale(1.0, 1.0); } g.setColor(Color.gray); g.drawLine(0, -10 * b.height, 0, 10 * b.height); g.drawLine(-10 * b.width, 0, 10 * b.width, 0); MetSymbol tmp = highlightedMetSymbol; if (tmp != null) { Rectangle tb = tmp.getBounds(); g.setColor(Color.red); g.drawRect(tb.x - 2, tb.y - 2, tb.width + 4, tb.height + 4); } }
/** * Adds the componentType buttons to the Panel of Main Window. * * @param pkg_name whose componentType buttons will be added * @param a needed for the addition of the ActionListener of respective buttons and calling method * of Pad_Draw */ public void add_componentType_buttons(package_cls pkg_name, final counts a) { int i; JLabel pkg_hdng = new JLabel(pkg_name.getName()); panel.add(pkg_hdng); for (i = 0; i < pkg_name.getComponentType_list().size(); i++) { final componentType cmp_Types = pkg_name.getComponentType_list().get(i); int btnHeight, btnWidth; double scale = 0.5; btnWidth = (int) (scale * (double) cmp_Types.getWidth()); btnHeight = (int) (scale * (double) cmp_Types.getHeight()); ImageIcon myIcon = new ImageIcon(cmp_Types.getType_Img()); BufferedImage bi = new BufferedImage(btnWidth, btnHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); g.scale(scale, scale); myIcon.paintIcon(null, g, 0, 0); g.dispose(); JButton strctButton = new JButton(new ImageIcon(bi)); strctButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { drawPad.add_componentType(cmp_Types, a); taskbar.setText("Click to add a component"); } }); panel.add(strctButton); } panel.validate(); // Updates the Panel }
/** [Internal] */ public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } RepaintManager currentManager = RepaintManager.currentManager(this); currentManager.setDoubleBufferingEnabled(false); Graphics2D g2 = (Graphics2D) g; initState(g2, true); g2.translate((int) (pf.getImageableX() + 1), (int) (pf.getImageableY() + 1)); g2.scale(printScale, printScale); doBuffer(g2, true, null); currentManager.setDoubleBufferingEnabled(true); return Printable.PAGE_EXISTS; }
public void paint(Graphics g) { Graphics2D gr = (Graphics2D) g; super.paint(g); Rectangle bounds = scene.getBounds(); Dimension size = getSize(); double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0; double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0; double scale = Math.min(sx, sy); int vw = (int) (scale * bounds.width); int vh = (int) (scale * bounds.height); int vx = (size.width - vw) / 2; int vy = (size.height - vh) / 2; if (image == null || vw != imageWidth || vh != imageHeight) { imageWidth = vw; imageHeight = vh; image = this.createImage(imageWidth, imageHeight); Graphics2D ig = (Graphics2D) image.getGraphics(); ig.scale(scale, scale); scene.setRealZoomFactor(scale); scene.paint(ig); scene.setRealZoomFactor(0.0); } gr.drawImage(image, vx, vy, this); JComponent component = scene.getView(); double zoomFactor = scene.getZoomFactor(); Rectangle viewRectangle = component != null ? component.getVisibleRect() : null; if (viewRectangle != null) { Rectangle window = new Rectangle( (int) ((double) viewRectangle.x * scale / zoomFactor), (int) ((double) viewRectangle.y * scale / zoomFactor), (int) ((double) viewRectangle.width * scale / zoomFactor), (int) ((double) viewRectangle.height * scale / zoomFactor)); window.translate(vx, vy); gr.setColor(new Color(200, 200, 200, 128)); gr.fill(window); gr.setColor(Color.BLACK); gr.drawRect(window.x, window.y, window.width - 1, window.height - 1); } }
/** This method is required to implement the Printable interface */ public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= 1) { return NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) g; Dimension cs = printTarget.getSize(); g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); double imageableWidth = pageFormat.getImageableWidth(); double imageableHeight = pageFormat.getImageableHeight(); double scale = 1; if (cs.width >= imageableWidth) { scale = imageableWidth / cs.width; } g2.scale(scale, scale); // g2.translate((imageableWidth - cs.width)*scale/2, // (imageableHeight - cs.height)*scale/2); printTarget.paintAll(g2); return Printable.PAGE_EXISTS; }
@Override public void draw(Graphics2D g) { double opacity = get(OPACITY); opacity = Math.min(Math.max(0d, opacity), 1d); if (opacity != 0d) { if (opacity != 1d) { Rectangle2D.Double drawingArea = getDrawingArea(); Rectangle2D clipBounds = g.getClipBounds(); if (clipBounds != null) { Rectangle2D.intersect(drawingArea, clipBounds, drawingArea); } if (!drawingArea.isEmpty()) { BufferedImage buf = new BufferedImage( (int) ((2 + drawingArea.width) * g.getTransform().getScaleX()), (int) ((2 + drawingArea.height) * g.getTransform().getScaleY()), BufferedImage.TYPE_INT_ARGB); Graphics2D gr = buf.createGraphics(); gr.scale(g.getTransform().getScaleX(), g.getTransform().getScaleY()); gr.translate((int) -drawingArea.x, (int) -drawingArea.y); gr.setRenderingHints(g.getRenderingHints()); drawFigure(gr); gr.dispose(); Composite savedComposite = g.getComposite(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity)); g.drawImage( buf, (int) drawingArea.x, (int) drawingArea.y, 2 + (int) drawingArea.width, 2 + (int) drawingArea.height, null); g.setComposite(savedComposite); } } else { drawFigure(g); } } }
@Override public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); switch (command) { case "New File": { OSC = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics osg = OSC.getGraphics(); osg.setColor(fillColor); osg.fillRect(0, 0, getWidth(), getHeight()); osg.dispose(); repaint(); break; } case "Open": { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG or PNG Images", "jpg", "png"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { try { OSC = ImageIO.read(chooser.getSelectedFile()); Graphics2D g2 = (Graphics2D) OSC.getGraphics(); g2.scale((double) 640 / OSC.getWidth(), (double) 480 / OSC.getHeight()); g2.drawImage(OSC, 0, 0, OSC.getWidth(), OSC.getHeight(), null); } catch (Exception e) { } } repaint(); break; } case "Save": { JFileChooser fileDialog = new JFileChooser(); fileDialog.setSelectedFile(new File("image.PNG")); fileDialog.setDialogTitle("Select File to be Saved"); int option = fileDialog.showSaveDialog(null); if (option != JFileChooser.APPROVE_OPTION) return; // User canceled or clicked the dialog’s close box. File selectedFile = fileDialog.getSelectedFile(); if (selectedFile.exists()) { // Ask the user whether to replace the file. int response = JOptionPane.showConfirmDialog( null, "The file \"" + selectedFile.getName() + "\" already exists.\nDo you want to replace it?", "Confirm Save", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (response != JOptionPane.YES_OPTION) return; // User does not want to replace the file. } try { boolean hasFormat = ImageIO.write(OSC, "PNG", selectedFile); if (!hasFormat) throw new Exception("PNG" + " format is not available."); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Sorry, an error occurred while trying to save image."); e.printStackTrace(); } break; } case "Exit": { System.exit(0); } case "Select Drawing Color...": { Color newColor = JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Drawing Color", currentColor); if (newColor != null) currentColor = newColor; break; } case "Fill With Color...": { Color newColor = JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Fill Color", fillColor); if (newColor != null) { fillColor = newColor; Graphics osg = OSC.getGraphics(); osg.setColor(fillColor); osg.fillRect(0, 0, OSC.getWidth(), OSC.getHeight()); osg.dispose(); AdvancedGUIEX1.this.repaint(); } break; } case "Draw With Black": currentColor = Color.BLACK; break; case "Draw With White": currentColor = Color.WHITE; break; case "Draw With Red": currentColor = Color.RED; break; case "Draw With Green": currentColor = Color.GREEN; break; case "Draw With Blue": currentColor = Color.BLUE; break; case "Draw With Yellow": currentColor = Color.YELLOW; break; case "Curve": currentTool = Tool.CURVE; break; case "Line": currentTool = Tool.LINE; break; case "Rectangle": currentTool = Tool.RECT; break; case "Oval": currentTool = Tool.OVAL; break; case "Filled Rectangle": currentTool = Tool.FILLED_RECT; break; case "Filled Oval": currentTool = Tool.FILLED_OVAL; break; case "Smudge": currentTool = Tool.SMUDGE; break; case "Erase": currentTool = Tool.ERASE; break; } }
// also clip, transform, composite, // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;} // --------------------------------------------------------- private void doPaint(Graphics2D g, int s, Object o) { // process an operation from the buffer // System.out.println(s); Object o1 = null, o2 = null, o3 = null, o4 = null, o5 = null, o6 = null, o7 = null, o8 = null, o9 = null, o10 = null, o11 = null; if (o instanceof Object[]) { Object[] a = (Object[]) o; if (a.length > 0) o1 = a[0]; if (a.length > 1) o2 = a[1]; if (a.length > 2) o3 = a[2]; if (a.length > 3) o4 = a[3]; if (a.length > 4) o5 = a[4]; if (a.length > 5) o6 = a[5]; if (a.length > 6) o7 = a[6]; if (a.length > 7) o8 = a[7]; if (a.length > 8) o9 = a[8]; if (a.length > 9) o10 = a[9]; if (a.length > 10) o11 = a[10]; } switch (s) { case clear: paintBackground(g, theBackground); break; // public void addRenderingHints(Map<?,?> hints) // {toBuffer("addRenderingHints",hints );} case addRenderingHints: g.addRenderingHints((Map<?, ?>) o); break; case clip1: g.clip((Shape) o); break; case draw1: g.draw((Shape) o); break; case draw3DRect: g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case drawGlyphVector: g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3); break; case drawImage1: g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4); break; case drawImage2: g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3); break; case drawRenderableImage: g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2); break; case drawRenderedImage: g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2); break; case drawString1: g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3); break; case drawString2: g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3); break; case drawString3: g.drawString((String) o1, (Float) o2, (Float) o3); break; case drawString4: g.drawString((String) o1, (Integer) o2, (Integer) o3); break; case fill: g.fill((Shape) o); break; case fill3DRect: g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case rotate1: g.rotate((Double) o); break; case rotate2: g.rotate((Double) o1, (Double) o2, (Double) o3); break; case scale1: g.scale((Double) o1, (Double) o2); break; case setBackground: g.setBackground( (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/ break; case setComposite: g.setComposite((Composite) o); break; case setPaint: g.setPaint((Paint) o); break; case setRenderingHint: g.setRenderingHint((RenderingHints.Key) o1, o2); break; case setRenderingHints: g.setRenderingHints((Map<?, ?>) o); break; case setStroke: g.setStroke((Stroke) o); break; case setTransform: g.setTransform(makeTransform(o)); break; case shear: g.shear((Double) o1, (Double) o2); break; case transform1: g.transform(makeTransform(o)); break; case translate1: g.translate((Double) o1, (Double) o2); break; case translate2: g.translate((Integer) o1, (Integer) o2); break; case clearRect: g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case copyArea: g.copyArea( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawArc: g.drawArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawBytes: g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawChars: g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawImage4: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5); break; case drawImage5: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4); break; case drawImage6: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Color) o6, (ImageObserver) o7); break; case drawImage7: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6); break; case drawImage8: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (Color) o10, (ImageObserver) o11); break; case drawImage9: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (ImageObserver) o10); break; case drawLine: g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawOval: g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawPolygon1: g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case drawPolygon2: g.drawPolygon((Polygon) o); break; case drawPolyline: g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3); break; case drawRect: g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawRoundRect: g.drawRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillArc: g.fillArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillOval: g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; // {toBuffer("fillPolygon",mkArg(xPoints, yPoints, nPoints) );} case fillPolygon1: g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case fillPolygon2: g.fillPolygon((Polygon) o); break; case fillRect: g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case fillRoundRect: g.fillRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case setClip1: g.setClip((Shape) o); break; case setColor: g.setColor((Color) o); break; case setFont: g.setFont((Font) o); break; case setPaintMode: g.setPaintMode(); break; case setXORMode: g.setXORMode((Color) o); break; case opaque: super.setOpaque((Boolean) o); break; case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break; { FontRenderContext frc = g.getFontRenderContext(); TextLayout tl = new TextLayout((String) o1, g.getFont(), frc); Shape s1 = tl.getOutline(null); AffineTransform af = g.getTransform(); g.translate((Integer) o2, (Integer) o3); g.draw(s1); g.setTransform(af); } ; break; default: System.out.println("Unknown image operation " + s); } }