Example usage for java.awt RenderingHints KEY_TEXT_ANTIALIASING

List of usage examples for java.awt RenderingHints KEY_TEXT_ANTIALIASING

Introduction

In this page you can find the example usage for java.awt RenderingHints KEY_TEXT_ANTIALIASING.

Prototype

Key KEY_TEXT_ANTIALIASING

To view the source code for java.awt RenderingHints KEY_TEXT_ANTIALIASING.

Click Source Link

Document

Text antialiasing hint key.

Usage

From source file:gg.msn.ui.panel.MainPanel.java

@Override
public void paint(Graphics g) {
    super.paint(g);
    try {/*from  ww w  .j ava  2s. co m*/
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        //render per utenti Facebook
        if (ChatClientView.protocol.equals(ChatClientView.FACEBOOK_PROTOCOL)) {
            final FacebookUser user = (FacebookUser) value;
            int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

            //name string
            g2.setFont(list.getFont());
            g2.drawString(user.name, WHITE_SPACE + IMAGE_LATE + 3, textY);

            //status string
            //                g2.setFont(list.getFont());
            //                g2.drawString(user.status, WHITE_SPACE + IMAGE_LATE + 3, textY);

            //icon
            ImageIcon icon = user.portrait;
            //log.debug("icon [" + icon + "]");
            //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
            g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                    IMAGE_LATE, null);
            //                 g2.setColor(Color.WHITE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE, IMAGE_LATE, ARC_SIZE, ARC_SIZE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+1) / 2), IMAGE_LATE+1, IMAGE_LATE+1, ARC_SIZE, ARC_SIZE);
            //                g2.drawRoundRect(WHITE_SPACE, getHeight() / 2 - ((IMAGE_LATE+2) / 2), IMAGE_LATE+2, IMAGE_LATE+2, ARC_SIZE, ARC_SIZE);
            //render per utenti Client
            //                log.debug("user status [" + user.status + "]");
            //                log.debug("user online status [" + user.onlineStatus + "]");
            if (StringUtils.equals(user.status, FacebookUser.STATUS_ONLINE)) {
                g2.setColor(Color.GREEN);
                g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                        STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
            } else {
                g2.setColor(Color.GRAY);
                g2.fillOval(getWidth() - STATUS_ICON_OFFSET, (getHeight() / 2) - (STATUS_ICON_WIDTH / 2),
                        STATUS_ICON_WIDTH, STATUS_ICON_WIDTH);
            }

        } else {
            g2.setFont(list.getFont());
            int textY = (getHeight() / 2) + (g2.getFont().getSize() / 2);

            Client client = (Client) value;
            //                setText((client).getNick());
            ImageIcon icon = null;
            try {
                new ImageIcon(client.getImage());
            } catch (Exception e) {
                //                    log.debug("immgine non presente");
                icon = ThemeManager.getTheme().get(ThemeManager.USER_ICON);
            }
            g2.drawString(client.getNick(), WHITE_SPACE + IMAGE_LATE + 3, textY);
            //log.debug("icon [" + icon + "]");
            //ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_AREA_AVERAGING));
            g2.drawImage(icon.getImage(), WHITE_SPACE, getHeight() / 2 - (IMAGE_LATE / 2), IMAGE_LATE,
                    IMAGE_LATE, null);

            //                setFont(list.getFont());
            //                setIcon(scaledIcon);
        }
    } catch (Exception e) {
        log.warn(e);
    }

}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public BufferedImage getImage() {
    // Create an image that supports transparent pixels
    Dimension d = getPreferredSize();
    BufferedImage img = GraphicUtils.createCompatibleImage(d.width, d.height, false);

    // prepare graphics context
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g2d.setBackground(new Color(255, 255, 255, 0));

    // paint/*  www .  ja  v  a  2 s  .c  o m*/
    is_printing = true;
    this.paint(g2d);
    is_printing = false;

    return img;
}

From source file:edu.ku.brc.specify.utilapps.ERDVisualizer.java

/**
 * @return/*ww w . j  av  a2 s.co m*/
 */
public static RenderingHints createTextRenderingHints() {
    RenderingHints renderingHints;
    //RenderingHints renderingHints1 = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    RenderingHints renderingHints2 = new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    //RenderingHints renderingHints3 = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    renderingHints = renderingHints2;

    Object value = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
    try {
        java.lang.reflect.Field declaredField = RenderingHints.class
                .getDeclaredField("VALUE_TEXT_ANTIALIAS_LCD_HRGB");
        value = declaredField.get(null);

    } catch (Exception e) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ERDVisualizer.class, e);
        // do nothing
    }
    renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, value);
    //renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    //renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    //renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB);
    return renderingHints;
}

From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java

private byte[] createImage(int width, int height, String text) throws IOException {
    if (width < 0) {
        width = random.nextInt(DEFAULT_MAX_IMAGE_WIDTH - DEFAULT_MIN_IMAGE_WIDTH) + DEFAULT_MIN_IMAGE_WIDTH;
    }/*ww  w .  j  av a2 s  .c o m*/
    if (height < 0) {
        height = random.nextInt(DEFAULT_MAX_IMAGE_HEIGHT - DEFAULT_MIN_IMAGE_HEIGHT) + DEFAULT_MIN_IMAGE_HEIGHT;
    }

    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = (Graphics2D) img.getGraphics();
    g2.setBackground(new Color(220, 220, 220));

    Dimension size;
    float fontSize = g2.getFont().getSize();
    // Make the text as large as possible.
    do {
        g2.setFont(g2.getFont().deriveFont(fontSize));
        FontMetrics metrics = g2.getFontMetrics(g2.getFont());
        int hgt = metrics.getHeight();
        int adv = metrics.stringWidth(text);
        size = new Dimension(adv + 2, hgt + 2);
        fontSize = fontSize + 1f;
    } while (size.width < Math.round(0.9 * width) && size.height < Math.round(0.9 * height));

    g2.setColor(Color.DARK_GRAY);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.drawString(text, (width - size.width) / 2, (height - size.height) / 2);
    g2.setColor(Color.LIGHT_GRAY);
    g2.drawRect(0, 0, width - 1, height - 1);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(width * height);
    ImageIO.write(img, "png", baos);
    baos.flush();
    byte[] rawBytes = baos.toByteArray();
    baos.close();

    return rawBytes;
}

From source file:com.projity.contrib.calendar.JXXMonthView.java

/**
 * {@inheritDoc}//from   ww w.  ja  va  2  s . c o m
 */
protected void paintComponent(Graphics g) {
    Object oldAAValue = null;
    Graphics2D g2 = (g instanceof Graphics2D) ? (Graphics2D) g : null;
    if (g2 != null && _antiAlias) {
        oldAAValue = g2.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

    Rectangle clip = g.getClipBounds();

    updateIfNecessary();

    if (isOpaque()) {
        g.setColor(getBackground());
        g.fillRect(clip.x, clip.y, clip.width, clip.height);
    }
    g.setColor(getForeground());
    Color shadowColor = g.getColor();
    shadowColor = new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(),
            (int) (.20 * 255));

    FontMetrics fm = g.getFontMetrics();

    // Reset the calendar.
    _cal.setTimeInMillis(_firstDisplayedDate);

    // Center the calendars vertically in the available space.
    int y = _startY;
    for (int row = 0; row < _numCalRows; row++) {
        // Center the calendars horizontally in the available space.
        int x = _startX;
        int tmpX, tmpY;

        // Check if this row falls in the clip region.
        _bounds.x = 0;
        _bounds.y = _startY + row * (_calendarHeight + CALENDAR_SPACING);
        _bounds.width = getWidth();
        _bounds.height = _calendarHeight;

        if (!_bounds.intersects(clip)) {
            _cal.add(Calendar.MONTH, _numCalCols);
            y += _calendarHeight + CALENDAR_SPACING;
            continue;
        }

        for (int column = 0; column < _numCalCols; column++) {
            String monthName = _monthsOfTheYear[_cal.get(Calendar.MONTH)];
            monthName = monthName + " " + _cal.get(Calendar.YEAR);

            _bounds.x = _ltr ? x : x - _calendarWidth;
            _bounds.y = y + _boxPaddingY;
            _bounds.width = _calendarWidth;
            _bounds.height = _boxHeight;

            if (_bounds.intersects(clip)) {
                // Paint month name background.
                paintMonthStringBackground(g, _bounds.x, _bounds.y, _bounds.width, _bounds.height);

                // Paint month name.
                g.setColor(getForeground());
                tmpX = _ltr ? x + (_calendarWidth / 2) - (fm.stringWidth(monthName) / 2)
                        : x - (_calendarWidth / 2) - (fm.stringWidth(monthName) / 2) - 1;
                tmpY = y + _boxPaddingY + _boxHeight - fm.getDescent();

                g.drawString(monthName, tmpX, tmpY);

                if ((_dropShadowMask & MONTH_DROP_SHADOW) != 0) {
                    g.setColor(shadowColor);
                    g.drawString(monthName, tmpX + 1, tmpY + 1);
                    g.setColor(getForeground());
                }
            }

            _bounds.x = _ltr ? x : x - _calendarWidth;
            _bounds.y = y + _boxPaddingY + _boxHeight + _boxPaddingY + _boxPaddingY;
            _bounds.width = _calendarWidth;
            _bounds.height = _boxHeight;

            if (_bounds.intersects(clip)) {
                _cal.set(Calendar.DAY_OF_MONTH, _cal.getActualMinimum(Calendar.DAY_OF_MONTH));
                Calendar weekCal = (Calendar) _cal.clone();
                // Paint short representation of day of the week.
                int dayIndex = _firstDayOfWeek - 1;
                int month = weekCal.get(Calendar.MONTH);
                //               dayIndex = (_cal.get(Calendar.DAY_OF_WEEK) -1) %7;
                for (int i = 0; i < DAYS_IN_WEEK; i++) {
                    //                  PROJITY_MODIFICATION
                    // set the week calendar to the current day of week and make sure it's still in this month
                    weekCal.set(Calendar.DAY_OF_WEEK, dayIndex + 1);
                    if (weekCal.get(Calendar.MONTH) != month)
                        weekCal.roll(Calendar.DAY_OF_YEAR, 7); // make sure in this month

                    tmpX = _ltr
                            ? x + (i * (_boxPaddingX + _boxWidth + _boxPaddingX)) + _boxPaddingX
                                    + (_boxWidth / 2) - (fm.stringWidth(_daysOfTheWeek[dayIndex]) / 2)
                            : x - (i * (_boxPaddingX + _boxWidth + _boxPaddingX)) - _boxPaddingX
                                    - (_boxWidth / 2) - (fm.stringWidth(_daysOfTheWeek[dayIndex]) / 2);
                    tmpY = y + _boxPaddingY + _boxHeight + _boxPaddingY + _boxPaddingY + fm.getAscent();
                    boolean flagged = _flaggedWeekDates[dayIndex];
                    boolean colored = _coloredWeekDates[dayIndex];
                    calculateBoundsForDay(_bounds, weekCal, true);
                    drawDay(colored, flagged, false, g, _daysOfTheWeek[dayIndex], tmpX, tmpY);

                    //                  if ((_dropShadowMask & WEEK_DROP_SHADOW) != 0) {
                    //                     calculateBoundsForDay(_bounds,weekCal,true); // add shadow arg
                    //                     drawDay(colored,flagged,false,g,_daysOfTheWeek[dayIndex], tmpX + 1,
                    //                           tmpY + 1);
                    //                  }
                    if (_selectedWeekDays[dayIndex]) {
                        paintSelectedDayBackground(g, _bounds.x, _bounds.y, _bounds.width, _bounds.height);
                    }
                    dayIndex++;
                    if (dayIndex == 7) {
                        dayIndex = 0;
                    }
                }

                int lineOffset = 2;
                // Paint a line across bottom of days of the week.
                g.drawLine(_ltr ? x + 2 : x - 3, lineOffset + y + (_boxPaddingY * 3) + (_boxHeight * 2),
                        _ltr ? x + _calendarWidth - 3 : x - _calendarWidth + 2,
                        lineOffset + y + (_boxPaddingY * 3) + (_boxHeight * 2));
                if ((_dropShadowMask & MONTH_LINE_DROP_SHADOW) != 0) {
                    g.setColor(shadowColor);
                    g.drawLine(_ltr ? x + 3 : x - 2, y + (_boxPaddingY * 3) + (_boxHeight * 2) + 1,
                            _ltr ? x + _calendarWidth - 2 : x - _calendarWidth + 3,
                            y + (_boxPaddingY * 3) + (_boxHeight * 2) + 1);
                    g.setColor(getForeground());
                }
            }

            // Check if the month to paint falls in the clip.
            _bounds.x = _startX + (_ltr ? column * (_calendarWidth + CALENDAR_SPACING)
                    : -(column * (_calendarWidth + CALENDAR_SPACING) + _calendarWidth));
            _bounds.y = _startY + row * (_calendarHeight + CALENDAR_SPACING);
            _bounds.width = _calendarWidth;
            _bounds.height = _calendarHeight;

            // Paint the month if it intersects the clip. If we don't move
            // the calendar forward a month as it would have if paintMonth
            // was called.
            if (_bounds.intersects(clip)) {
                paintMonth(g, column, row);
            } else {
                _cal.add(Calendar.MONTH, 1);
            }

            x += _ltr ? _calendarWidth + CALENDAR_SPACING : -(_calendarWidth + CALENDAR_SPACING);
        }
        y += _calendarHeight + CALENDAR_SPACING;
    }

    // Restore the calendar.
    _cal.setTimeInMillis(_firstDisplayedDate);
    if (g2 != null && _antiAlias) {
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, oldAAValue);
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

public byte[] printImage(float scale, CanvasCommon canvas, Dimension graphsize, String format)
        throws IOException {
    int h_margin = 72, v_margin = 72;
    BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    if (scale > 0)
        g2.scale(scale, scale);//  w w  w  .j a  va  2  s. co m
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, image.getWidth(), image.getHeight());
    // canvas.paint(g2);
    Color bgsave = canvas.getBackground();
    boolean edsave = canvas.editable;
    canvas.editable = false;
    canvas.setBackground(Color.white);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    canvas.paintComponent(g2);
    canvas.setBackground(bgsave);
    canvas.editable = edsave;
    g2.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, format, baos);
    image = null;
    Runtime r = Runtime.getRuntime();
    r.gc();
    return baos.toByteArray();
}

From source file:lucee.runtime.img.Image.java

public void setAntiAliasing(boolean antiAlias) throws ExpressionException {
    this.antiAlias = antiAlias ? ANTI_ALIAS_ON : ANTI_ALIAS_OFF;
    Graphics2D graphics = getGraphics();
    if (antiAlias) {
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {// w ww  . ja va2s . co  m
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }
}

From source file:org.jfree.experimental.chart.swt.ChartComposite.java

public void paintControl(PaintEvent e) {
    // first determine the size of the chart rendering area...
    // TODO workout insets for SWT
    Rectangle available = getBounds();
    // skip if chart is null
    if (this.chart == null) {
        this.canvas.drawBackground(e.gc, available.x, available.y, available.width, available.height);
        return;/*  w  w  w .ja va2  s .  c  o  m*/
    }
    SWTGraphics2D sg2 = new SWTGraphics2D(e.gc);

    // work out if scaling is required...
    boolean scale = false;
    int drawWidth = available.width;
    int drawHeight = available.height;
    if (drawWidth == 0.0 || drawHeight == 0.0)
        return;
    this.scaleX = 1.0;
    this.scaleY = 1.0;
    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = (double) drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = (double) drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = (double) drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = (double) drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }
    // are we using the chart buffer?
    if (this.useBuffer) {
        // SwtGraphics2D sg2 = new SwtGraphics2D( e.gc );
        this.chartBuffer = (org.eclipse.swt.graphics.Image) this.canvas.getData("double-buffer-image");
        // do we need to fill the buffer?
        if (this.chartBuffer == null || this.chartBufferWidth != available.width
                || this.chartBufferHeight != available.height) {
            this.chartBufferWidth = available.width;
            this.chartBufferHeight = available.height;
            if (this.chartBuffer != null) {
                this.chartBuffer.dispose();
            }
            this.chartBuffer = new org.eclipse.swt.graphics.Image(getDisplay(), this.chartBufferWidth,
                    this.chartBufferHeight);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            // Performs the actual drawing here ...
            GC gci = new GC(this.chartBuffer);
            // anti-aliasing
            if (this.chart.getAntiAlias()) {
                gci.setAntialias(SWT.ON);
            }
            if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
                gci.setTextAntialias(SWT.ON);
            }
            SWTGraphics2D sg2d = new SWTGraphics2D(gci);
            if (scale) {
                sg2d.scale(this.scaleX, this.scaleY);
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            } else {
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            }
            this.canvas.setData("double-buffer-image", this.chartBuffer);
            sg2d.dispose();
            gci.dispose();
            this.refreshBuffer = false;
        }

        // zap the buffer onto the canvas...
        sg2.drawImage(this.chartBuffer, 0, 0);
    }
    // or redrawing the chart every time...
    else {
        if (this.chart.getAntiAlias()) {
            e.gc.setAntialias(SWT.ON);
        }
        if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
            e.gc.setTextAntialias(SWT.ON);
        }
        this.chart.draw(sg2, new Rectangle2D.Double(0, 0, getBounds().width, getBounds().height), getAnchor(),
                this.info);
    }
    Rectangle area = getScreenDataArea();
    // TODO see if we need to apply some line color and style to the
    // axis traces
    if (this.verticalAxisTrace && area.x < this.verticalTraceLineX
            && area.x + area.width > this.verticalTraceLineX) {
        e.gc.drawLine(this.verticalTraceLineX, area.y, this.verticalTraceLineX, area.y + area.height);
    }
    if (this.horizontalAxisTrace && area.y < this.horizontalTraceLineY
            && area.y + area.height > this.horizontalTraceLineY) {
        e.gc.drawLine(area.x, this.horizontalTraceLineY, area.x + area.width, this.horizontalTraceLineY);
    }
    this.verticalTraceLineX = 0;
    this.horizontalTraceLineY = 0;
    if (this.zoomRectangle != null) {
        e.gc.drawRectangle(this.zoomRectangle);
    }
    sg2.dispose();
}

From source file:com.munch.exchange.ExchangeChartComposite.java

/**
 * Paints the control./*w  w  w .j a  v  a  2 s .  com*/
 *
 * @param e  the paint event.
 */
public void paintControl(PaintEvent e) {
    // first determine the size of the chart rendering area...
    // TODO workout insets for SWT
    Rectangle available = getBounds();
    // skip if chart is null
    if (this.chart == null) {
        this.canvas.drawBackground(e.gc, available.x, available.y, available.width, available.height);
        return;
    }
    SWTGraphics2D sg2 = new SWTGraphics2D(e.gc);

    // work out if scaling is required...
    boolean scale = false;
    int drawWidth = available.width;
    int drawHeight = available.height;
    if (drawWidth == 0.0 || drawHeight == 0.0)
        return;
    this.scaleX = 1.0;
    this.scaleY = 1.0;
    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = (double) drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = (double) drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = (double) drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = (double) drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }
    // are we using the chart buffer?
    if (this.useBuffer) {
        //SwtGraphics2D sg2 = new SwtGraphics2D(e.gc);
        this.chartBuffer = (org.eclipse.swt.graphics.Image) this.canvas.getData("double-buffer-image");
        // do we need to fill the buffer?
        if (this.chartBuffer == null || this.chartBufferWidth != available.width
                || this.chartBufferHeight != available.height) {
            this.chartBufferWidth = available.width;
            this.chartBufferHeight = available.height;
            if (this.chartBuffer != null) {
                this.chartBuffer.dispose();
            }
            this.chartBuffer = new org.eclipse.swt.graphics.Image(getDisplay(), this.chartBufferWidth,
                    this.chartBufferHeight);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            // Performs the actual drawing here ...
            GC gci = new GC(this.chartBuffer);
            // anti-aliasing
            if (this.chart.getAntiAlias()) {
                gci.setAntialias(SWT.ON);
            }
            if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
                gci.setTextAntialias(SWT.ON);
            }
            SWTGraphics2D sg2d = new SWTGraphics2D(gci);
            if (scale) {
                sg2d.scale(this.scaleX, this.scaleY);
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            } else {
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            }
            this.canvas.setData("double-buffer-image", this.chartBuffer);
            sg2d.dispose();
            gci.dispose();
            this.refreshBuffer = false;
        }

        // zap the buffer onto the canvas...
        sg2.drawImage(this.chartBuffer, 0, 0);
    }
    // or redrawing the chart every time...
    else {
        if (this.chart.getAntiAlias()) {
            e.gc.setAntialias(SWT.ON);
        }
        if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
            e.gc.setTextAntialias(SWT.ON);
        }
        this.chart.draw(sg2, new Rectangle2D.Double(0, 0, getBounds().width, getBounds().height), getAnchor(),
                this.info);
    }
    Rectangle area = getScreenDataArea();
    // TODO see if we need to apply some line color and style to the
    // axis traces
    if (this.horizontalAxisTrace && area.x < this.verticalTraceLineX
            && area.x + area.width > this.verticalTraceLineX) {
        e.gc.drawLine(this.verticalTraceLineX, area.y, this.verticalTraceLineX, area.y + area.height);
    }
    if (this.verticalAxisTrace && area.y < this.horizontalTraceLineY
            && area.y + area.height > this.horizontalTraceLineY) {
        e.gc.drawLine(area.x, this.horizontalTraceLineY, area.x + area.width, this.horizontalTraceLineY);
    }
    this.verticalTraceLineX = 0;
    this.horizontalTraceLineY = 0;
    if (this.zoomRectangle != null) {
        e.gc.drawRectangle(this.zoomRectangle);
    }
    sg2.dispose();
}

From source file:org.mwc.cmap.grideditor.chart.FixedChartComposite.java

/**
 * Paints the control.// w ww.  j  av a2s . co  m
 * 
 * @param e
 *          the paint event.
 */
public void paintControl(final PaintEvent e) {
    // first determine the size of the chart rendering area...
    // TODO workout insets for SWT
    final Rectangle available = getBounds();
    // skip if chart is null
    if (this.chart == null) {
        this.canvas.drawBackground(e.gc, available.x, available.y, available.width, available.height);
        return;
    }
    final SWTGraphics2D sg2 = new SWTGraphics2D(e.gc);

    // work out if scaling is required...
    boolean scale = false;
    int drawWidth = available.width;
    int drawHeight = available.height;
    if (drawWidth == 0.0 || drawHeight == 0.0)
        return;
    this.scaleX = 1.0;
    this.scaleY = 1.0;
    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = (double) drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = (double) drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = (double) drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = (double) drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }
    // are we using the chart buffer?
    if (this.useBuffer) {
        // SwtGraphics2D sg2 = new SwtGraphics2D(e.gc);
        this.chartBuffer = (org.eclipse.swt.graphics.Image) this.canvas.getData("double-buffer-image");
        // do we need to fill the buffer?
        if (this.chartBuffer == null || this.chartBufferWidth != available.width
                || this.chartBufferHeight != available.height) {
            this.chartBufferWidth = available.width;
            this.chartBufferHeight = available.height;
            if (this.chartBuffer != null) {
                this.chartBuffer.dispose();
            }
            this.chartBuffer = new org.eclipse.swt.graphics.Image(getDisplay(), this.chartBufferWidth,
                    this.chartBufferHeight);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            // Performs the actual drawing here ...
            final GC gci = new GC(this.chartBuffer);
            // anti-aliasing
            if (this.chart.getAntiAlias()) {
                gci.setAntialias(SWT.ON);
            }
            if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
                gci.setTextAntialias(SWT.ON);
            }
            final SWTGraphics2D sg2d = new SWTGraphics2D(gci);
            if (scale) {
                sg2d.scale(this.scaleX, this.scaleY);
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            } else {
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            }
            this.canvas.setData("double-buffer-image", this.chartBuffer);
            sg2d.dispose();
            gci.dispose();
            this.refreshBuffer = false;
        }

        // zap the buffer onto the canvas...
        sg2.drawImage(this.chartBuffer, 0, 0);
    }
    // or redrawing the chart every time...
    else {
        if (this.chart.getAntiAlias()) {
            e.gc.setAntialias(SWT.ON);
        }
        if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
            e.gc.setTextAntialias(SWT.ON);
        }
        this.chart.draw(sg2, new Rectangle2D.Double(0, 0, getBounds().width, getBounds().height), getAnchor(),
                this.info);
    }
    final Rectangle area = getScreenDataArea();
    // TODO see if we need to apply some line color and style to the
    // axis traces
    if (this.horizontalAxisTrace && area.x < this.verticalTraceLineX
            && area.x + area.width > this.verticalTraceLineX) {
        e.gc.drawLine(this.verticalTraceLineX, area.y, this.verticalTraceLineX, area.y + area.height);
    }
    if (this.verticalAxisTrace && area.y < this.horizontalTraceLineY
            && area.y + area.height > this.horizontalTraceLineY) {
        e.gc.drawLine(area.x, this.horizontalTraceLineY, area.x + area.width, this.horizontalTraceLineY);
    }
    this.verticalTraceLineX = 0;
    this.horizontalTraceLineY = 0;
    if (this.zoomRectangle != null) {
        e.gc.drawRectangle(this.zoomRectangle);
    }
    sg2.dispose();
}