Java Code Examples for java.awt.Graphics#create()

The following examples show how to use java.awt.Graphics#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemPanel.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
	// draw the background image
	background.draw(g, 0, 0);

	// Take a temporary copy in case the game loop destroys the view under
	// us.
	EntityView<?> entityView = view;
	if (entityView != null) {
		// Center the entity view (assume 1x1 tile)
		final int x = (getWidth() - IGameScreen.SIZE_UNIT_PIXELS) / 2;
		final int y = (getHeight() - IGameScreen.SIZE_UNIT_PIXELS) / 2;

		final Graphics2D vg = (Graphics2D) g.create(0, 0, getWidth(),
				getHeight());
		vg.translate(x, y);
		entityView.draw(vg);
		vg.dispose();
	} else if (placeholder != null) {
		placeholder.draw(g, (getWidth() - placeholder.getWidth()) / 2,
				(getHeight() - placeholder.getHeight()) / 2);
	}
}
 
Example 2
Source File: AnimationLayer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Paints the current state (i.e. the state corresponding to the current
 * phase) of the given component.
 *
 * @param g graphics context.
 * @param comp component to paint.
 */
private void paintComponent(Graphics g, Component comp) {
    Rectangle bounds = currentBounds(comp);
    float alpha = currentAlpha(comp);
    Graphics gg = g.create(bounds.x, bounds.y, bounds.width, bounds.height);
    if (alpha != 1f) {
        AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        ((Graphics2D)gg).setComposite(alphaComposite);
    }
    comp.setBounds(bounds);
    comp.validate();
    // Intentionally using print instead of paint.
    // Print doesn't use double buffering and it solves some mysterious
    // problems with modified clip during painting of containers.
    // BTW: animated transitions library also uses print()
    if (comp instanceof JComponent) {
        comp.print(gg);
    } else {
        java.awt.peer.ComponentPeer peer = FakePeerSupport.getPeer(comp);
        if (peer != null) {
            peer.paint(gg);
        }
    }
    gg.dispose();
}
 
Example 3
Source File: ViewComponent.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public final void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth();
    int height = getHeight();
    Insets insets = getInsets();
    width -= insets.left + insets.right;
    height -= insets.top + insets.bottom;
    renderedObjects.clear();
    transform.rescale(width, height);
    Graphics2D copy = (Graphics2D)g.create(insets.left, insets.top, width, height);
    if (isOpaque()) {
        copy.setColor(getBackground());
        copy.fillRect(0, 0, width, height);
    }
    renderedObjects.addAll(render(copy, transform, width, height));
}
 
Example 4
Source File: StatusDisplayBar.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
	Insets insets = getInsets();
	int barHeight = getHeight() - insets.top - insets.bottom - 2;
	// Paint frame
	g.setColor(getForeground());
	g.drawRect(insets.left, insets.top, getWidth() - insets.left - insets.right - 1,
			getHeight() - insets.top - insets.bottom - 1);
	// Paint what is not covered by the colored bar
	g.setColor(getBackground());
	g.fillRect(insets.left + 1, insets.top + 1, getWidth() - insets.left - insets.right - 2,
			barHeight);

	if (barImage != null) {
		Graphics clipped = g.create(insets.left + 1, insets.top + 1, model.getRepresentation(), barHeight);
		clipped.drawImage(barImage, 0, 0, null);
		clipped.dispose();
	} else {
		g.setColor(color);
		g.fillRect(insets.left + 1, insets.top + 1, model.getRepresentation(), barHeight);
	}
}
 
Example 5
Source File: FlatAbstractIcon.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
@Override
	public void paintIcon( Component c, Graphics g, int x, int y ) {
		Graphics2D g2 = (Graphics2D) g.create();
		try {
			FlatUIUtils.setRenderingHints( g2 );

			// for testing
//			g2.setColor( Color.blue );
//			g2.drawRect( x, y, getIconWidth() - 1, getIconHeight() - 1 );

			g2.translate( x, y );
			UIScale.scaleGraphics( g2 );

			if( color != null )
				g2.setColor( color );

			paintIcon( c, g2 );
		} finally {
			g2.dispose();
		}
	}
 
Example 6
Source File: SeaGlassGraphicsUtils.java    From seaglass with Apache License 2.0 6 votes vote down vote up
/**
 * Paints text at the specified location. This will not attempt to render
 * the text as html nor will it offset by the insets of the component.
 *
 * @param ss
 *            SynthContext
 * @param g
 *            Graphics used to render string in.
 * @param text
 *            Text to render
 * @param x
 *            X location to draw text at.
 * @param y
 *            Upper left corner to draw text at.
 * @param mnemonicIndex
 *            Index to draw string at.
 */
public void paintText(SynthContext ss, Graphics g, String text, int x, int y, int mnemonicIndex) {
    if (text != null) {
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        JComponent c = ss.getComponent();

        // SynthStyle style = ss.getStyle();
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g2d);

        y += fm.getAscent();
        SwingUtilities2.drawString(c, g2d, text, x, y);

        if (mnemonicIndex >= 0 && mnemonicIndex < text.length()) {
            int underlineX = x + SwingUtilities2.stringWidth(c, fm, text.substring(0, mnemonicIndex));
            int underlineY = y;
            int underlineWidth = fm.charWidth(text.charAt(mnemonicIndex));
            int underlineHeight = 1;

            g2d.fillRect(underlineX, underlineY + fm.getDescent() - 1, underlineWidth, underlineHeight);
        }
    }
}
 
Example 7
Source File: ButtonTabComponent.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    //shift the image for pressed buttons
    if (getModel().isPressed()) {
        g2.translate(1, 1);
    }
    g2.setStroke(new BasicStroke(2));
    g2.setColor(Color.BLACK);
    if (getModel().isRollover()) {
        g2.setColor(Color.MAGENTA);
    }
    int delta = 6;
    g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
    g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
    g2.dispose();
}
 
Example 8
Source File: DialogUtils.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {
	super.paintComponent(g);

	int w = getWidth();
	int h = getHeight();

	int shadowX = w/2 - (cmp.getWidth()+100)/2;
	int shadowY = h/2 - (cmp.getHeight()+100)/2;
	cmp.setLocation(w/2-cmp.getWidth()/2, h/2-cmp.getHeight()/2);
	title.setLocation(w/2-cmp.getWidth()/2, h/2-cmp.getHeight()/2-title.getHeight());
	info.setLocation(w/2+cmp.getWidth()/2-info.getWidth(), h/2-cmp.getHeight()/2-info.getHeight());

	Graphics2D gg = (Graphics2D) g.create();
	gg.setPaint(fill);
	gg.fillRect(0, 0, w, h);
	gg.drawImage(shadowImage.getImage(), shadowX, shadowY, cmp.getWidth()+100, cmp.getHeight()+100, null);
	gg.dispose();
}
 
Example 9
Source File: AltTabCrashTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void paint(Graphics g, Color c) {
    if (c == null) {
        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(color);
        g2d.fillOval(x, y, diameter, diameter);
    } else {
        g.setColor(c);
        g.fillOval(x-2, y-2, diameter+4, diameter+4);
    }
}
 
Example 10
Source File: FancyDropDownButton.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
	if (getModel().isArmed()) {
		((Graphics2D) g).translate(1.1, 1.1);
	}

	((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	Graphics2D g2 = (Graphics2D) g.create();
	if (hovered) {
		// fill background white
		g2.setColor(BACKGROUND_COLOR);
		g2.fillRect(0, 0, getWidth() - 6, getHeight() - 6);

		// draw border which complements the DropShadow
		g2.setColor(BORDER_LIGHTEST_GRAY);
		g2.drawLine(0, 0, 0, getHeight() - 6);
		g2.drawLine(0, getHeight() - 6, getWidth() - 6, getHeight() - 6);
		g2.drawLine(0, 0, getWidth() - 6, 0);
		g2.drawLine(getWidth() - 6, 0, getWidth() - 6, getHeight() - 6);

		// cut off arrow ends so the cut looks like an imaginary vertical line
		g2.setColor(BACKGROUND_COLOR);
		g2.fillRect((int) (getWidth() * 0.81), (int) (getHeight() * 0.29), 7, (int) (getHeight() * 0.5));
	}
	g2.dispose();

	super.paintComponent(g);
}
 
Example 11
Source File: GlassPane.java    From 07kit with GNU General Public License v3.0 5 votes vote down vote up
protected void paintComponent(Graphics g) {
	Graphics2D copy = (Graphics2D) g.create();
	copy.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	
	if(fWindow.getMousePosition() != null) {
		fStyle.getOverlay().paintOverlayMouseOver(copy, fCornerRadius);
		fStyle.getCloseButton().paintCloseButton(copy);
	} else {
		fStyle.getOverlay().paintOverlayMouseOut(copy, fCornerRadius);
	}
	copy.dispose();
}
 
Example 12
Source File: PlotterAdapter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private void drawNominalLegend(Graphics graphics, DataTable table, int legendColumn, int xOffset, int alpha) {
	Graphics2D g = (Graphics2D) graphics.create();
	g.translate(xOffset, 0);

	// painting label name
	String legendName = table.getColumnName(legendColumn);
	g.drawString(legendName, MARGIN, 15);
	Rectangle2D legendNameBounds = LABEL_FONT.getStringBounds(legendName, g.getFontRenderContext());
	g.translate(legendNameBounds.getWidth(), 0);

	// painting values
	int numberOfValues = table.getNumberOfValues(legendColumn);
	int currentX = MARGIN;

	for (int i = 0; i < numberOfValues; i++) {
		if (currentX > getWidth()) {
			break;
		}
		String nominalValue = table.mapIndex(legendColumn, i);
		if (nominalValue.length() > 16) {
			nominalValue = nominalValue.substring(0, 16) + "...";
		}
		Shape colorBullet = new Ellipse2D.Double(currentX, 7, 7.0d, 7.0d);
		Color color = getColorProvider().getPointColor((double) i / (double) (numberOfValues - 1), alpha);
		g.setColor(color);
		g.fill(colorBullet);
		g.setColor(Color.black);
		g.draw(colorBullet);
		currentX += 12;
		g.drawString(nominalValue, currentX, 15);
		Rectangle2D stringBounds = LABEL_FONT.getStringBounds(nominalValue, g.getFontRenderContext());
		currentX += stringBounds.getWidth() + 15;
	}
}
 
Example 13
Source File: FlashTextOverlay.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (text != null) {
        Graphics2D g2d = (Graphics2D) g.create();
        paintFlashText(g2d);
        g2d.dispose();
    }
}
 
Example 14
Source File: MatteBorder.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void paintEdge(Component c, Graphics g, int x, int y, int width, int height, int tileW, int tileH) {
    g = g.create(x, y, width, height);
    int sY = -(y % tileH);
    for (x = -(x % tileW); x < width; x += tileW) {
        for (y = sY; y < height; y += tileH) {
            this.tileIcon.paintIcon(c, g, x, y);
        }
    }
    g.dispose();
}
 
Example 15
Source File: NonOpaqueDestLCDAATest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void render(Graphics g, int w, int h) {
    initImages(w, h);

    g.setColor(new Color(0xAD, 0xD8, 0xE6));
    g.fillRect(0, 0, w, h);

    Graphics2D g2d = (Graphics2D) g.create();
    for (Image im : images) {
        g2d.drawImage(im, 0, 0, null);
        g2d.translate(0, im.getHeight(null));
    }
}
 
Example 16
Source File: ColorQuartilePlotter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	if (drawLegend) {
		drawLegend(graphics, dataTable, colorIndex, 50, RectangleStyle.ALPHA);
	}
	int pixWidth = getWidth() - 2 * MARGIN;
	int pixHeight = getHeight() - 2 * MARGIN;
	Graphics2D translated = (Graphics2D) graphics.create();
	translated.translate(MARGIN, MARGIN);
	paintQuartiles(translated, pixWidth, pixHeight);
	translated.dispose();
}
 
Example 17
Source File: StrokeBorder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Paints the border for the specified component
 * with the specified position and size.
 * If the border was not specified with a {@link Paint} object,
 * the component's foreground color will be used to render the border.
 * If the component's foreground color is not available,
 * the default color of the {@link Graphics} object will be used.
 *
 * @param c       the component for which this border is being painted
 * @param g       the paint graphics
 * @param x       the x position of the painted border
 * @param y       the y position of the painted border
 * @param width   the width of the painted border
 * @param height  the height of the painted border
 *
 * @throws NullPointerException if the specified {@code g} is {@code null}
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    float size = this.stroke.getLineWidth();
    if (size > 0.0f) {
        g = g.create();
        if (g instanceof Graphics2D) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(this.stroke);
            g2d.setPaint(this.paint != null ? this.paint : c == null ? null : c.getForeground());
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.draw(new Rectangle2D.Float(x + size / 2, y + size / 2, width - size, height - size));
        }
        g.dispose();
    }
}
 
Example 18
Source File: MultiGradientTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g.create();

    int w = getWidth();
    int h = getHeight();
    g2d.setColor(Color.black);
    g2d.fillRect(0, 0, w, h);

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         antialiasHint);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                         renderHint);

    g2d.transform(transform);
    g2d.setPaint(paint);

    switch (shapeType) {
    default:
    case RECT:
        g2d.fillRect(0, 0, w, h);
        break;

    case ELLIPSE:
        g2d.fillOval(0, 0, w, h);
        break;

    case MULTIPLE:
        g2d.fillRect(0, 0, w/2, h/2);
        g2d.fillOval(w/2, 0, w/2, h/2);
        g2d.drawOval(0, h/2, w/2, h/2);
        g2d.drawLine(0, h/2, w/2, h);
        g2d.drawLine(0, h, w/2, h/2);
        Polygon p = new Polygon();
        p.addPoint(w/2, h);
        p.addPoint(w, h);
        p.addPoint(3*w/4, h/2);
        g2d.fillPolygon(p);
        break;
    }

    switch (paintType) {
    default:
    case BASIC:
    case LINEAR:
        g2d.setColor(Color.white);
        g2d.fillRect(startX-1, startY-1, 2, 2);
        g2d.drawString("1", startX, startY + 12);
        g2d.fillRect(endX-1, endY-1, 2, 2);
        g2d.drawString("2", endX, endY + 12);
        break;

    case RADIAL:
        g2d.setColor(Color.white);
        g2d.fillRect(ctrX-1, ctrY-1, 2, 2);
        g2d.drawString("C", ctrX, ctrY + 12);
        g2d.fillRect(focusX-1, focusY-1, 2, 2);
        g2d.drawString("F", focusX, focusY + 12);
        break;
    }

    g2d.dispose();
}
 
Example 19
Source File: ImageUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void paintIcon(Component c, Graphics g, int x, int y) {
    if (delegateIcon != null) {
        delegateIcon.paintIcon(c, g, x, y);
    } else {
        /* There is no scalable delegate icon available. On HiDPI displays, this means that
        original low-resolution icons will need to be scaled up to a higher resolution. Do a
        few tricks here to improve the quality of the scaling. See NETBEANS-2614 and the
        before/after screenshots that are attached to said JIRA ticket. */
        Graphics2D g2 = (Graphics2D) g.create();
        try {
            final AffineTransform tx = g2.getTransform();
            final int txType = tx.getType();
            final double scale;
            if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
                txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
            {
              scale = tx.getScaleX();
            } else {
              scale = 1.0;
            }
            if (scale != 1.0) {
                /* The default interpolation mode is nearest neighbor. Use bicubic
                interpolation instead, which looks better, especially with non-integral
                HiDPI scaling factors (e.g. 150%). Even for an integral 2x scaling factor
                (used by all Retina displays on MacOS), the blurred appearance of bicubic
                scaling ends up looking better on HiDPI displays than the blocky appearance
                of nearest neighbor. */
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                /* For non-integral scaling factors, we frequently encounter non-integral
                device pixel positions. For instance, with a 150% scaling factor, the
                logical pixel position (7,0) would map to device pixel position (10.5,0).
                On such scaling factors, icons look a lot better if we round the (x,y)
                translation to an integral number of device pixels before painting. */
                g2.setTransform(new AffineTransform(scale, 0, 0, scale,
                        (int) tx.getTranslateX(), (int) tx.getTranslateY()));
            }
            g2.drawImage(this, x, y, null);
        } finally {
            g2.dispose();
        }
    }
}
 
Example 20
Source File: StrokeBorder.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Paints the border for the specified component
 * with the specified position and size.
 * If the border was not specified with a {@link Paint} object,
 * the component's foreground color will be used to render the border.
 * If the component's foreground color is not available,
 * the default color of the {@link Graphics} object will be used.
 *
 * @param c       the component for which this border is being painted
 * @param g       the paint graphics
 * @param x       the x position of the painted border
 * @param y       the y position of the painted border
 * @param width   the width of the painted border
 * @param height  the height of the painted border
 *
 * @throws NullPointerException if the specified {@code g} is {@code null}
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    float size = this.stroke.getLineWidth();
    if (size > 0.0f) {
        g = g.create();
        if (g instanceof Graphics2D) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(this.stroke);
            g2d.setPaint(this.paint != null ? this.paint : c == null ? null : c.getForeground());
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.draw(new Rectangle2D.Float(x + size / 2, y + size / 2, width - size, height - size));
        }
        g.dispose();
    }
}