public JSPrintPreview2(boolean devMode) { // this.devMode = devMode; elements = new Vector<Object[]>(); setBackground(new Color(175, 175, 175)); setSize(600, 900); setDefaultCloseOperation(DISPOSE_ON_CLOSE); buttonBar = new JPanel(); buttonBar.setLayout(new GridLayout(1, 5)); if (devMode) { coordinateField = new JTextField("- - -, - - -"); coordinateField.setHorizontalAlignment(CENTER); coordinateField.setEditable(false); coordinateField.setPreferredSize(new Dimension(120, 30)); buttonBar.add(coordinateField); } zoomPanel = new JPanel(new GridLayout(1, 2)); zoomLabel = new JLabel("Zoom: ", RIGHT); zoomPanel.add(zoomLabel); String[] zoomLevels = {"25%", "50%", "75%", "100%"}; zoomBox = new JComboBox(zoomLevels); zoomBox.addItemListener(this); zoomPanel.add(zoomBox); buttonBar.add(zoomPanel); printButton = new JButton("Print"); printButton.addActionListener(this); buttonBar.add(printButton); paperPanel = new PaperPanel(); paperPanel.setPreferredSize(new Dimension(600, 725)); paperPanel.addMouseListener(paperPanel); paperPanel.addMouseMotionListener(paperPanel); scrollPane = new JScrollPane(paperPanel); scrollPane.getVerticalScrollBar().setUnitIncrement(20); setLayout(new BorderLayout()); add("North", buttonBar); add("Center", scrollPane); currentFont = new Font("Arial", Font.BOLD, 14); styleFont = currentFont; }
public void itemStateChanged(ItemEvent event) { if (event.getSource() == zoomBox) { switch (zoomBox.getSelectedIndex()) { case 0: this.scale = 1.0D; break; case 1: this.scale = 1.25D; break; case 2: this.scale = 1.5D; break; case 3: this.scale = 1.75D; break; } repaint(); render(paperPanel.getGraphics(), -1, scale); paperPanel.setPreferredSize( new Dimension((int) (600 * scale), (int) (numberOfPages * scale * (pageHeight + 25)))); } }
private void render(Graphics g, int drawingPage, double scale) { Graphics2D g2 = (Graphics2D) g; g2.scale(scale, scale); int pageIndex; paperPanel.setPreferredSize(new Dimension(600, numberOfPages * (pageHeight + 25))); for (int page = 0; page < numberOfPages; page++) { g2.setColor(Color.white); g2.fillRect(0, page * (pageHeight + 25), pageWidth, pageHeight); } g2.setColor(Color.black); for (Object[] task : elements) { switch ((Integer) task[0]) { case DRAW_STRING: FontMetrics metrics = getFontMetrics(g2.getFont()); String string = task[1].toString(); int x = (Integer) task[2]; int y = (Integer) task[3]; pageIndex = (Integer) task[4]; if (drawingPage == -1) y += (pageIndex * (pageHeight + 25)); int width = metrics.stringWidth(string); if (drawingPage == -1 || drawingPage == pageIndex) { if (currentAlignment == LEFT) { g2.drawString(string, x, y); if (underline) { g2.drawLine(x, y + 3, x + width, y + 3); } } else if (currentAlignment == RIGHT) { g2.drawString(string, x - width, y); if (underline) { g2.drawLine(x, y + 3, x - width, y + 3); } } else if (currentAlignment == CENTER) { g2.drawString(string, x - (width / 2), y); if (underline) { g2.drawLine(x - (width / 2), y + 3, x + (width / 2), y + 3); } } } break; case DRAW_LINE: int x1 = (Integer) task[1], y1 = (Integer) task[2], x2 = (Integer) task[3], y2 = (Integer) task[4]; pageIndex = (Integer) task[5]; if (drawingPage == -1) g2.drawLine( x1, (pageIndex * (pageHeight + 25)) + y1, x2, (pageIndex * (pageHeight + 25)) + y2); else if (drawingPage == pageIndex) g2.drawLine(x1, y1, x2, y2); break; case DRAW_RECT: int rectX = (Integer) task[1], rectY = (Integer) task[2], rectWidth = (Integer) task[3], rectHeight = (Integer) task[4]; pageIndex = (Integer) task[5]; if (drawingPage == -1) g2.drawRect(rectX, (pageIndex * (pageHeight + 25)) + rectY, rectWidth, rectHeight); else if (drawingPage == pageIndex) g2.drawRect(rectX, rectY, rectWidth, rectHeight); break; case DRAW_OVAL: int ovalX = (Integer) task[1], ovalY = (Integer) task[3], ovalWidth = (Integer) task[3], ovalHeight = (Integer) task[4]; pageIndex = (Integer) task[5]; if (drawingPage == -1) g2.drawOval(ovalX, (pageIndex * (pageHeight + 25)) + ovalY, ovalWidth, ovalHeight); else if (drawingPage == pageIndex) g2.drawOval(ovalX, ovalY, ovalWidth, ovalHeight); break; case DRAW_IMAGE: Image image = (Image) task[1]; int imageX = (Integer) task[2], imageY = (Integer) task[3], imageWidth = (Integer) task[4], imageHeight = (Integer) task[5]; pageIndex = (Integer) task[6]; if (drawingPage == -1) g2.drawImage( image, imageX, (pageIndex * (pageHeight + 25)) + imageY, imageWidth, imageHeight, this); else if (drawingPage == pageIndex) g2.drawImage(image, imageX, imageY, imageWidth, imageHeight, this); break; case DRAW_POLYGON: int[] xPoints = (int[]) task[1], yPoints = (int[]) task[2]; int numberOfPoints = (Integer) task[3]; pageIndex = (Integer) task[4]; if (drawingPage == -1) { for (int i = 0; i < yPoints.length; i++) { yPoints[i] += (pageIndex * (pageHeight + 25)); } g2.drawPolygon(xPoints, yPoints, numberOfPoints); } else if (drawingPage == pageIndex) g2.drawPolygon(xPoints, yPoints, numberOfPoints); break; case DRAW_ARC: int arcX = (Integer) task[1], arcY = (Integer) task[2], arcWidth = (Integer) task[3], arcHeight = (Integer) task[4], startAngle = (Integer) task[5], arcAngle = (Integer) task[6]; pageIndex = (Integer) task[7]; if (drawingPage == -1) g2.drawArc( arcX, (pageIndex * (pageHeight + 25)) + arcY, arcWidth, arcHeight, startAngle, arcAngle); else if (drawingPage == pageIndex) g2.drawArc(arcX, arcY, arcWidth, arcHeight, startAngle, arcAngle); break; case FILL_RECT: int fillRectX = (Integer) task[1], fillRectY = (Integer) task[2], fillRectWidth = (Integer) task[3], fillRectHeight = (Integer) task[4]; pageIndex = (Integer) task[5]; if (drawingPage == -1) g2.fillRect( fillRectX, (pageIndex * (pageHeight + 25)) + fillRectY, fillRectWidth, fillRectHeight); else if (drawingPage == pageIndex) g2.fillRect(fillRectX, fillRectY, fillRectWidth, fillRectHeight); break; case FILL_OVAL: int fillOvalX = (Integer) task[1], fillOvalY = (Integer) task[2], fillOvalWidth = (Integer) task[3], fillOvalHeight = (Integer) task[4]; pageIndex = (Integer) task[5]; if (drawingPage == -1) g2.fillOval( fillOvalX, (pageIndex * (pageHeight + 25)) + fillOvalY, fillOvalWidth, fillOvalHeight); else if (drawingPage == pageIndex) g2.fillOval(fillOvalX, fillOvalY, fillOvalWidth, fillOvalHeight); break; case FILL_POLYGON: int[] fillXPoints = (int[]) task[1], fillYPoints = (int[]) task[2]; int fillNumberOfPoints = (Integer) task[3]; pageIndex = (Integer) task[4]; if (drawingPage == -1) { for (int i = 0; i < fillYPoints.length; i++) { fillYPoints[i] += (pageIndex * (pageHeight + 25)); } g2.fillPolygon(fillXPoints, fillYPoints, fillNumberOfPoints); } else if (drawingPage == pageIndex) g2.fillPolygon(fillXPoints, fillYPoints, fillNumberOfPoints); break; case FILL_ARC: int fillArcX = (Integer) task[1], fillArcY = (Integer) task[2], fillArcWidth = (Integer) task[3], fillArcHeight = (Integer) task[4], fillStartAngle = (Integer) task[5], fillArcAngle = (Integer) task[6]; pageIndex = (Integer) task[7]; if (drawingPage == -1) g2.fillArc( fillArcX, (pageIndex * (pageHeight + 25)) + fillArcY, fillArcWidth, fillArcHeight, fillStartAngle, fillArcAngle); else if (drawingPage == pageIndex) g2.fillArc( fillArcX, fillArcY, fillArcWidth, fillArcHeight, fillStartAngle, fillArcAngle); break; case SET_COLOR: g2.setColor((Color) task[1]); break; case SET_FONT: g2.setFont((Font) task[1]); break; case SET_STROKE: g2.setStroke((Stroke) task[1]); break; case SET_PAINT: g2.setPaint((Paint) task[1]); break; case SET_ALIGNMENT: currentAlignment = (Integer) task[1]; break; case SET_STYLE: int style = (Integer) task[1]; // currentStyle = style; styleFont = currentFont.deriveFont(Font.PLAIN); if (style == BOLD) styleFont = styleFont.deriveFont(Font.BOLD); if (style == ITALIC) styleFont = styleFont.deriveFont(Font.ITALIC); underline = (style == UNDERLINE); if (style == BOLD_UNDERLINE) { styleFont = styleFont.deriveFont(Font.BOLD); underline = true; } g2.setFont(styleFont); break; } } }