use of java.awt.Graphics2D in project ACS by ACS-Community.
the class PrintableTreeExplorer method print.
//
// -- implements Printable ----------------------------------------------
//
/* (non-Javadoc)
* @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
*/
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
//leave room for page number
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double treeWidth = (double) myTree.getWidth();
double scale = 1;
if (treeWidth >= pageWidth) {
scale = pageWidth / treeWidth;
}
double headerHeightOnPage = 0.0;
double tableWidthOnPage = treeWidth * scale;
double oneRowHeight = myTree.getRowBounds(0).getHeight() * scale;
int numRowsOnAPage = (int) (pageHeight / oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages = (int) Math.ceil(((double) myTree.getRowCount()) / numRowsOnAPage);
if (pageIndex >= totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Page: " + (pageIndex + 1), (int) pageWidth / 2 - 35, (int) (pageHeight + fontHeight - fontDesent));
//bottom center
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);
//clip to the appropriate bounds.
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = myTree.getRowCount() - lastRowPrinted;
g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft));
} else //else clip to the entire area available.
{
g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable));
}
g2.scale(scale, scale);
myTree.paint(g2);
g2.scale(1 / scale, 1 / scale);
g2.translate(0f, pageIndex * pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(headerHeightOnPage));
g2.scale(scale, scale);
return Printable.PAGE_EXISTS;
}
use of java.awt.Graphics2D in project ACS by ACS-Community.
the class AlarmTable method initGUI.
/**
* Init the GUI
*/
private void initGUI() {
setShowHorizontalLines(true);
// Build and set the selection model
selectionModel = new DefaultListSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setSelectionModel(selectionModel);
this.setOpaque(false);
sorter = new TableRowSorter<AlarmTableModel>(model);
this.setRowSorter(sorter);
sorter.setMaxSortKeys(2);
sorter.setSortsOnUpdates(true);
// Initially sort by timestamp
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(AlarmTableColumn.PRIORITY.ordinal(), SortOrder.ASCENDING));
sortKeys.add(new RowSorter.SortKey(AlarmTableColumn.TIME.ordinal(), SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys);
// Remove all the columns not visible at startup
TableColumnModel colModel = getColumnModel();
columns = new TableColumn[colModel.getColumnCount()];
for (int t = 0; t < columns.length; t++) {
columns[t] = colModel.getColumn(t);
columns[t].setIdentifier(AlarmTableColumn.values()[t]);
if (columns[t].getIdentifier() == AlarmTableColumn.ICON || columns[t].getIdentifier() == AlarmTableColumn.IS_CHILD || columns[t].getIdentifier() == AlarmTableColumn.IS_PARENT) {
columns[t].setWidth(20);
columns[t].setResizable(false);
columns[t].setPreferredWidth(20);
columns[t].setMaxWidth(20);
columns[t].setMinWidth(20);
} else if (columns[t].getIdentifier() == AlarmTableColumn.PRIORITY) {
BufferedImage bImg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2D = bImg.createGraphics();
FontMetrics fm = g2D.getFontMetrics();
int sz = fm.stringWidth(PriorityLabel.VERY_HIGH.description);
columns[t].setPreferredWidth(sz + 6);
columns[t].setMaxWidth(sz + 8);
}
}
for (AlarmTableColumn col : AlarmTableColumn.values()) {
if (!col.visibleAtStartup) {
colModel.removeColumn(columns[col.ordinal()]);
}
}
buildPopupMenu();
addMouseListener(mouseAdapter);
getTableHeader().addMouseListener(new AlarmHeaderMouseAdapter());
// Set the tooltip
ToolTipManager ttm = ToolTipManager.sharedInstance();
ttm.setDismissDelay(Integer.MAX_VALUE);
ttm.setLightWeightPopupEnabled(true);
}
use of java.awt.Graphics2D in project EnrichmentMapApp by BaderLab.
the class JRangeSlider method paintComponent.
/**
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics g) {
Rectangle bounds = getBounds();
int width = (int) bounds.getWidth() - 1;
int height = (int) bounds.getHeight() - 1;
int min = toScreen(getLowValue());
int max = toScreen(getHighValue());
// Paint the full slider if the slider is marked as empty
if (empty) {
if (direction == LEFTRIGHT_TOPBOTTOM) {
min = ARROW_SZ;
max = (orientation == VERTICAL) ? (height - ARROW_SZ) : (width - ARROW_SZ);
} else {
min = (orientation == VERTICAL) ? (height - ARROW_SZ) : (width - ARROW_SZ);
max = ARROW_SZ;
}
}
Graphics2D g2 = (Graphics2D) g;
g2.setColor(getBackground());
g2.fillRect(0, 0, width, height);
g2.setColor(getForeground());
g2.drawRect(0, 0, width, height);
customPaint(g2, width, height);
// Draw arrow and thumb backgrounds
g2.setStroke(new BasicStroke(1));
if (orientation == VERTICAL) {
if (direction == LEFTRIGHT_TOPBOTTOM) {
g2.setColor(getForeground());
g2.fillRect(0, min - ARROW_SZ, width, ARROW_SZ - 1);
paint3DRectLighting(g2, 0, min - ARROW_SZ, width, ARROW_SZ - 1);
if (thumbColor != null) {
g2.setColor(thumbColor);
g2.fillRect(0, min, width, max - min - 1);
paint3DRectLighting(g2, 0, min, width, max - min - 1);
}
g2.setColor(getForeground());
g2.fillRect(0, max, width, ARROW_SZ - 1);
paint3DRectLighting(g2, 0, max, width, ARROW_SZ - 1);
// Draw arrows
g2.setColor(Color.black);
paintArrow(g2, (width - ARROW_WIDTH) / 2.0, min - ARROW_SZ + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), ARROW_WIDTH, ARROW_HEIGHT, true);
paintArrow(g2, (width - ARROW_WIDTH) / 2.0, max + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), ARROW_WIDTH, ARROW_HEIGHT, false);
} else {
g2.setColor(getForeground());
g2.fillRect(0, min, width, ARROW_SZ - 1);
paint3DRectLighting(g2, 0, min, width, ARROW_SZ - 1);
if (thumbColor != null) {
g2.setColor(thumbColor);
g2.fillRect(0, max, width, min - max - 1);
paint3DRectLighting(g2, 0, max, width, min - max - 1);
}
g2.setColor(getForeground());
g2.fillRect(0, max - ARROW_SZ, width, ARROW_SZ - 1);
paint3DRectLighting(g2, 0, max - ARROW_SZ, width, ARROW_SZ - 1);
// Draw arrows
g2.setColor(Color.black);
paintArrow(g2, (width - ARROW_WIDTH) / 2.0, min + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), ARROW_WIDTH, ARROW_HEIGHT, false);
paintArrow(g2, (width - ARROW_WIDTH) / 2.0, max - ARROW_SZ + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), ARROW_WIDTH, ARROW_HEIGHT, true);
}
} else {
if (direction == LEFTRIGHT_TOPBOTTOM) {
g2.setColor(getForeground());
g2.fillRect(min - ARROW_SZ, 0, ARROW_SZ - 1, height);
paint3DRectLighting(g2, min - ARROW_SZ, 0, ARROW_SZ - 1, height);
if (thumbColor != null) {
g2.setColor(thumbColor);
g2.fillRect(min, 0, max - min - 1, height);
paint3DRectLighting(g2, min, 0, max - min - 1, height);
}
g2.setColor(getForeground());
g2.fillRect(max, 0, ARROW_SZ - 1, height);
paint3DRectLighting(g2, max, 0, ARROW_SZ - 1, height);
// Draw arrows
g2.setColor(Color.black);
paintArrow(g2, min - ARROW_SZ + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), (height - ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, true);
paintArrow(g2, max + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), (height - ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, false);
} else {
g2.setColor(getForeground());
g2.fillRect(min, 0, ARROW_SZ - 1, height);
paint3DRectLighting(g2, min, 0, ARROW_SZ - 1, height);
if (thumbColor != null) {
g2.setColor(thumbColor);
g2.fillRect(max, 0, min - max - 1, height);
paint3DRectLighting(g2, max, 0, min - max - 1, height);
}
g2.setColor(getForeground());
g2.fillRect(max - ARROW_SZ, 0, ARROW_SZ - 1, height);
paint3DRectLighting(g2, max - ARROW_SZ, 0, ARROW_SZ - 1, height);
// Draw arrows
g2.setColor(Color.black);
paintArrow(g2, min + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), (height - ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, true);
paintArrow(g2, max - ARROW_SZ + ((ARROW_SZ - ARROW_HEIGHT) / 2.0), (height - ARROW_WIDTH) / 2.0, ARROW_HEIGHT, ARROW_WIDTH, false);
}
}
}
use of java.awt.Graphics2D in project EnrichmentMapApp by BaderLab.
the class SwingUtil method resizeIcon.
public static ImageIcon resizeIcon(final ImageIcon icon, int width, int height) {
final Image img = icon.getImage().getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(img, 0, 0, width, height, null);
g.dispose();
return new ImageIcon(bi);
}
use of java.awt.Graphics2D in project Asqatasun by Asqatasun.
the class TgTestRun method resizeImage.
/**
* Resize the snapshot image to 270*170
*
* @param originalImage
* @return
*/
private BufferedImage resizeImage(BufferedImage originalImage) {
float scaleRatio = (float) 270 / originalImage.getWidth();
int resizedImageHeight = Float.valueOf(originalImage.getHeight() * scaleRatio).intValue();
BufferedImage resizedImage = new BufferedImage(270, resizedImageHeight, originalImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 270, resizedImageHeight, null);
g.dispose();
return resizedImage.getSubimage(0, 0, 270, 170);
}
Aggregations