Results 1 to 3 of 3
Thread: [solved] Java JComponent
Hybrid View
-
14th June 2010 00:02 #1
[solved] Java JComponent
! Java Swing- printing layout. , Adobe Reader. . . . , . . . ? Swing GUI-, . , , .
EDIT: , PrintPreview JPanel, JComponent. . , .
Code:package com.buildware.sweethome3dex.swing; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Line2D; import java.awt.image.BufferedImage; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.JToolBar; import javax.swing.border.MatteBorder; import javax.swing.plaf.basic.BasicTextUI; import javax.swing.text.BoxView; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.Element; import javax.swing.text.StyleContext; import javax.swing.text.TableView; import javax.swing.text.View; import javax.swing.text.rtf.RTFEditorKit; import com.buildware.sweethome3dex.model.ConstructionPlan; import com.buildware.sweethome3dex.model.HomeEx; import com.eteks.sweethome3d.model.UserPreferences; public class ConstructionComponent extends JPanel implements Printable { protected final HomeEx home; protected final UserPreferences preferences; protected final ConstructionPlan document; protected final JTextPane monitor; protected final RTFEditorKit editorKit; protected PrintPreview printPreview; protected PrintView printView; public ConstructionComponent(HomeEx home, UserPreferences preferences) { this.home = home; this.document = home.getConstructionPlan(); this.preferences = preferences; // TODO: Make sure we install the editor kit before creating the initial document. this.monitor = new JTextPane(); this.editorKit = new RTFEditorKit(); this.monitor.setEditorKit(this.editorKit); this.monitor.setDocument(this.document); this.monitor.setText("AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaa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AAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "AAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"); this.printPreview = new PrintPreview(this); add(this.printPreview); } public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { graphics.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY()); int wPage = (int)pageFormat.getImageableWidth(); int hPage = (int)pageFormat.getImageableHeight(); graphics.setClip(0, 0, wPage, hPage); // Only do this once per print if (this.printView == null) { BasicTextUI textUI = (BasicTextUI)this.monitor.getUI(); View root = textUI.getRootView(null); this.printView = new PrintView(this.document.getDefaultRootElement(), root, wPage, hPage); } boolean continues = this.printView.paintPage(graphics, hPage, pageIndex); System.gc(); if (continues) return PAGE_EXISTS; else { this.printView = null; return NO_SUCH_PAGE; } } class PrintView extends BoxView { protected int m_firstOnPage = 0; protected int m_lastOnPage = 0; protected int m_pageIndex = 0; public PrintView(Element elem, View root, int w, int h) { super(elem, Y_AXIS); setParent(root); setSize(w, h); layout(w, h); } public boolean paintPage(Graphics g, int hPage, int pageIndex) { if (pageIndex > m_pageIndex) { m_firstOnPage = m_lastOnPage + 1; if (m_firstOnPage >= getViewCount()) return false; m_pageIndex = pageIndex; } int yMin = getOffset(Y_AXIS, m_firstOnPage); int yMax = yMin + hPage; Rectangle rc = new Rectangle(); for (int k = m_firstOnPage; k < getViewCount(); k++) { rc.x = getOffset(X_AXIS, k); rc.y = getOffset(Y_AXIS, k); rc.width = getSpan(X_AXIS, k); rc.height = getSpan(Y_AXIS, k); if (rc.y + rc.height > yMax) break; m_lastOnPage = k; rc.y -= yMin; paintChild(g, rc, k); } return true; } } public class PrintPreview extends JComponent { protected int pageWidth; protected int pageHeight; protected Printable target; protected JComboBox scaleComboBox; protected PreviewContainer preview; public PrintPreview(Printable target) { super(); this.target = target; JToolBar toolbar = new JToolBar(); JButton printButton = new JButton("Print", new ImageIcon("print.gif")); ActionListener lst = new ActionListener() { public void actionPerformed(ActionEvent e) { try { // Use default printer, no dialog PrinterJob prnJob = PrinterJob.getPrinterJob(); prnJob.setPrintable(PrintPreview.this.target); setCursor(Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR)); prnJob.print(); setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } catch (PrinterException ex) { ex.printStackTrace(); System.err.println("Printing error: " + ex.toString()); } } }; printButton.addActionListener(lst); printButton.setAlignmentY(0.5f); printButton.setMargin(new Insets(2, 6, 2, 6)); toolbar.add(printButton); String [] scales = {"10 %", "25 %", "50 %", "100 %"}; scaleComboBox = new JComboBox(scales); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { Thread runner = new Thread() { public void run() { String str = scaleComboBox.getSelectedItem(). toString(); if (str.endsWith("%")) str = str.substring(0, str.length() - 1); str = str.trim(); int scale = 0; try { scale = Integer.parseInt(str); } catch (NumberFormatException ex) { return; } int w = (int) (pageWidth * scale / 100); int h = (int) (pageHeight * scale / 100); Component [] comps = preview.getComponents(); for (int k = 0; k < comps.length; k++) { if (!(comps [k] instanceof PagePreview)) continue; PagePreview pp = (PagePreview) comps [k]; pp.setScaledSize(w, h); } preview.doLayout(); preview.getParent().getParent().validate(); } }; runner.start(); } }; scaleComboBox.addActionListener(lst); scaleComboBox.setMaximumSize(scaleComboBox.getPreferredSize()); scaleComboBox.setEditable(true); toolbar.addSeparator(); toolbar.add(scaleComboBox); add(toolbar, BorderLayout.NORTH); preview = new PreviewContainer(); PrinterJob prnJob = PrinterJob.getPrinterJob(); PageFormat pageFormat = prnJob.defaultPage(); if (pageFormat.getHeight() == 0 || pageFormat.getWidth() == 0) { System.err.println("Unable to determine default page size"); return; } pageWidth = (int) (pageFormat.getWidth()); pageHeight = (int) (pageFormat.getHeight()); int scale = 100; int w = (int) (pageWidth * scale / 100); int h = (int) (pageHeight * scale / 100); int pageIndex = 0; try { while (true) { BufferedImage img = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.green); g.fillRect(0, 0, pageWidth, pageHeight); if (target.print(g, pageFormat, pageIndex) != Printable.PAGE_EXISTS) break; PagePreview pp = new PagePreview(w, h, img); preview.add(pp); pageIndex++; } } catch (PrinterException e) { e.printStackTrace(); System.err.println("Printing error: " + e.toString()); } JScrollPane ps = new JScrollPane(preview); add(ps, BorderLayout.CENTER); } class PreviewContainer extends JPanel { protected int H_GAP = 16; protected int V_GAP = 10; public Dimension getPreferredSize() { int n = getComponentCount(); if (n == 0) return new Dimension(H_GAP, V_GAP); Component comp = getComponent(0); Dimension dc = comp.getPreferredSize(); int w = dc.width; int h = dc.height; Dimension dp = getParent().getSize(); int nCol = Math.max((dp.width - H_GAP) / (w + H_GAP), 1); int nRow = n / nCol; if (nRow * nCol < n) nRow++; int ww = nCol * (w + H_GAP) + H_GAP; int hh = nRow * (h + V_GAP) + V_GAP; Insets ins = getInsets(); return new Dimension(ww + ins.left + ins.right, hh + ins.top + ins.bottom); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getMinimumSize() { return getPreferredSize(); } public void doLayout() { Insets ins = getInsets(); int x = ins.left + H_GAP; int y = ins.top + V_GAP; int n = getComponentCount(); if (n == 0) return; Component comp = getComponent(0); Dimension dc = comp.getPreferredSize(); int w = dc.width; int h = dc.height; Dimension dp = getParent().getSize(); int nCol = Math.max((dp.width - H_GAP) / (w + H_GAP), 1); int nRow = n / nCol; if (nRow * nCol < n) nRow++; int index = 0; for (int k = 0; k < nRow; k++) { for (int m = 0; m < nCol; m++) { if (index >= n) return; comp = getComponent(index++); comp.setBounds(x, y, w, h); x += w + H_GAP; } y += h + V_GAP; x = ins.left + H_GAP; } } } class PagePreview extends JPanel { protected int width; protected int height; protected Image sourceImage; protected Image image; public PagePreview(int width, int height, Image source) { this.sourceImage = source; this.width = width; this.height = height; this.image = source.getScaledInstance(width, height, Image.SCALE_SMOOTH); this.image.flush(); setBackground(Color.yellow); setBorder(new MatteBorder(1, 1, 2, 2, Color.black)); } public void setScaledSize(int width, int height) { this.width = width; this.height = height; this.image = this.sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); repaint(); } public Dimension getPreferredSize() { Insets ins = getInsets(); return new Dimension(width + ins.left + ins.right, height + ins.top + ins.bottom); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paint(Graphics g) { g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); g.drawImage(this.image, 0, 0, this); paintBorder(g); } } } }Last edited by svr; 14th June 2010 at 13:57.
MSI B450 Gaming Pro Carbon AC | Ryzen 9 5900x | HyperX Predator 3200 | Asus Strix 3090 | Kingston KC3000 2TB | WD Red 4TB | Dell G3223Q + LG 27UK650-W | Arctic Freezer 360 | Seasonic Focus GX-1000 | Lian Li O11 Dynamic
-
14th June 2010 00:48 #2
AWT + Swing = PITA.
EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|
-
14th June 2010 02:26 #3
"PITA" .
MFC + COM + ATL, .
. , . - ConstructionComponent -> PrintPreview -> PreviewContainer -> PagePreview. . . PagePreview.paint() .
:
MSI B450 Gaming Pro Carbon AC | Ryzen 9 5900x | HyperX Predator 3200 | Asus Strix 3090 | Kingston KC3000 2TB | WD Red 4TB | Dell G3223Q + LG 27UK650-W | Arctic Freezer 360 | Seasonic Focus GX-1000 | Lian Li O11 Dynamic




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in