Java Code Examples for java.awt.Graphics2D.fillRect()

The following are Jave code examples for showing how to use fillRect() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: geomapapp   File: PoleMapServer.java   View Source Code Vote up 6 votes
private static BufferedImage drawTileName(BufferedImage image, String name) {
	BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = i2.createGraphics();
	g2.drawImage(image, null, 0, 0);
	g2.setColor(Color.red);
	g2.setStroke(new BasicStroke(4));
	g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);

	g2.setColor(Color.RED);
	g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
	FontMetrics fm = g2.getFontMetrics();
	String[] parts = name.split("/");
	for (int i = 0; i < parts.length; i++) {
		String s = i == parts.length - 1 ? parts[i] : parts[i] + "/"; 
		int x = 40 + i * 5;
		int y = 40 + i * (fm.getHeight() + 5);
		Rectangle2D rect = fm.getStringBounds(s, g2);
		g2.setColor(Color.white);
		g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
		g2.setColor(Color.red);
		g2.drawString(s, x, y);
	}
	return i2;
}
 
Example 2
Project: Progetto-C   File: SheetPanel.java   View Source Code Vote up 6 votes
/**
 * @see javax.swing.JComponent#paint(java.awt.Graphics)
 */
public void paint(Graphics g1d) {
       Graphics2D g = (Graphics2D) g1d;
       
	g.setPaint(background);
	g.fillRect(0,0,getWidth(), getHeight());
	g.setColor(Color.yellow);
	g.drawRect(0,0,width,height);
	
	if (image != null) {
		g.drawImage(image, 0, 0, null);
	}
	
	g.setColor(Color.green);
	for (int i=0;i<selected.size();i++) {
		Sprite sprite = (Sprite) selected.get(i);
		
		g.drawRect(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
	}
}
 
Example 3
Project: jdk8u-jdk   File: DrawRotatedStringUsingRotatedFont.java   View Source Code Vote up 6 votes
/**
 * Creates an BufferedImage and draws a text, using two transformations,
 * one for graphics and one for font.
 */
private static BufferedImage createImage(final boolean aa,
                                         final AffineTransform gtx,
                                         final AffineTransform ftx) {
    final BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_RGB);
    final Graphics2D bg = bi.createGraphics();
    bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        aa ? RenderingHints.VALUE_ANTIALIAS_ON
                           : RenderingHints.VALUE_ANTIALIAS_OFF);
    bg.setColor(Color.RED);
    bg.fillRect(0, 0, SIZE, SIZE);
    bg.translate(100, 100);
    bg.transform(gtx);
    bg.setColor(Color.BLACK);
    bg.setFont(bg.getFont().deriveFont(20.0f).deriveFont(ftx));
    bg.drawString(STR, 0, 0);
    bg.dispose();
    return bi;
}
 
Example 4
Project: jdk8u-jdk   File: CustomCompositeTest.java   View Source Code Vote up 6 votes
public static void renderTest(Graphics2D g2d, int w, int h) {
    g2d.setColor(Color.yellow);
    g2d.fillRect(0, 0, w, h);

    BufferedImage image = getTestImage();
    // draw original image
    g2d.drawRenderedImage(image, null);

    // draw image with custom composite
    g2d.translate(175, 25);
    Composite currentComposite = g2d.getComposite();
    g2d.setComposite(new TestComposite());
    g2d.drawRenderedImage(image, null);
    g2d.setComposite(currentComposite);

    // draw image with XOR
    g2d.translate(175, 25);
    g2d.setXORMode(Color.red);
    g2d.drawRenderedImage(image, null);


    System.out.println("Painting is done...");
}
 
Example 5
Project: openjdk-jdk10   File: ReadingInterruptionTest.java   View Source Code Vote up 5 votes
private static void createTestFile() {
    int w = 1280;
    int h = 1024;

    BufferedImage img = new
        BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();
    Color[] colors = { Color.red, Color.green, Color.blue };
    float[] dist = {0.0f, 0.5f, 1.0f };
    Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);

    RadialGradientPaint p =
        new RadialGradientPaint(center, 0.5f * w, dist, colors);
    g.setPaint(p);
    g.fillRect(0, 0, w, h);
    g.dispose();

    try {
        System.out.println("Create test image " + file.getAbsolutePath());
        boolean b = ImageIO.write(img, "JPEG", file);
        if (!b) {
            throw new RuntimeException("Failed to create test image.");
        }
    } catch (IOException e) {
        throw new RuntimeException("Test failed", e);
    }
}
 
Example 6
Project: incubator-netbeans   File: DragWindow.java   View Source Code Vote up 5 votes
private void repaintImageBuffer() {
    if( !useFadeEffects )
        return;
    // #128324 - image might not be created yet
    if ( null == imageBuffer ) {
        return;
    }
    Graphics2D g2d = imageBuffer.createGraphics();
    g2d.setColor( contentBackground );
    g2d.fillRect(0, 0, imageBuffer.getWidth(), imageBuffer.getHeight() );
    g2d.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, contentAlpha ) );
    g2d.drawImage(contentImage, 0, 0, null );
    g2d.dispose();
}
 
Example 7
Project: GIFKR   File: BoolInterpolator.java   View Source Code Vote up 5 votes
@Override
public void paintButton(Graphics2D g, int width, int height) {
	g.setColor(new Color(255, 0, 0, 80));
	g.fillRect(0, height/2, width, height/2);
	g.setColor(new Color(0, 255, 0, 80));
	g.fillRect(0, 0, width, height/2);

	super.paintButton(g, width, height);
}
 
Example 8
Project: openjdk-jdk10   File: ConvertToByteIndexedTest.java   View Source Code Vote up 5 votes
public static void ConvertToByteIndexed(Color color, int srcType) {
    // setup source image and graphics for conversion.
    BufferedImage srcImage = new BufferedImage(width, height, srcType);
    Graphics2D srcG2D = srcImage.createGraphics();
    srcG2D.setColor(color);
    srcG2D.fillRect(0, 0, width, height);

    // setup destination image and graphics for conversion.
    int dstType = BufferedImage.TYPE_BYTE_INDEXED;
    BufferedImage dstImage = new BufferedImage(width, height, dstType);
    Graphics2D dstG2D = (Graphics2D)dstImage.getGraphics();
    // draw source image into Byte Indexed destination
    dstG2D.drawImage(srcImage, 0, 0, null);

    // draw into ARGB image to verify individual pixel value.
    BufferedImage argbImage = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D argbG2D = (Graphics2D)argbImage.getGraphics();
    argbG2D.drawImage(dstImage, 0, 0, null);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            if (color.getRGB() != argbImage.getRGB(i, j)) {
                throw new RuntimeException("Conversion from " +
                        TYPE_TABLE.get(srcType) + " to BYTE_INDEXED is not"
                        + " done properly for " + color);
            }
        }
    }
}
 
Example 9
Project: xdman   File: XDMMenuUI.java   View Source Code Vote up 5 votes
protected void paintButtonPressed(Graphics g, AbstractButton b) {
		Color c = g.getColor();

		

		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(colorSelect);
		g2.fillRect(0, 0, b.getWidth(), b.getHeight());
		
//		if ("THEME".equals(b.getName()) || "CTX_SORT".equals(b.getName())) {
//			g2.setPaint(gradPressed);
//			int gapx = 0;
//
//			g2.fillRect(gapx + 2, 0, b.getWidth() - (4 + gapx + 2), b
//					.getHeight() - 2);
//			g2.setColor(Color.LIGHT_GRAY);
//			g2.drawRect(gapx + 2, 0, b.getWidth() - (4 + gapx + 2), b
//					.getHeight() - 2);
//			if (menuItem.getIcon() != null) {
//				int gap = menuItem.getIcon().getIconWidth() + 2;
//				g.setColor(this.darkColor);
//				g.drawLine(gap, 1, gap, menuItem.getHeight() - 3);
//				g.setColor(this.lightColor2);
//				g.drawLine(gap + 1, 1, gap + 1, menuItem.getHeight() - 3);
//			}
//		} else {
//			g2.setPaint(gradPressed);
//			g2.fillRoundRect(0, 0, b.getWidth() - 1, b.getHeight() - 1, 4, 4);
//			g2.setColor(Color.LIGHT_GRAY);
//			g2.drawRoundRect(0, 0, b.getWidth() - 1, b.getHeight() - 1, 4, 4);
//		}
		g.setColor(c);
	}
 
Example 10
Project: brModelo   File: PreTexto.java   View Source Code Vote up 5 votes
protected void PaintGradiente(Graphics2D g, boolean round) {
    int dist = 0;
    int w = getWidth() - dist;
    int h = getHeight() - dist;
    int L = getLeft();
    int T = getTop();
    boolean dv = getGDirecao() == VERTICAL;

    //Composite originalComposite = g.getComposite();
    //g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, alfa));
    GradientPaint GP = new GradientPaint(L, T, getGradienteStartColor(), dv ? L : L + w, dv ? T + h : T, getGradienteEndColor(), true);
    //g.setPaint(GP);

    g.setPaint(getForeColor());
    if (round) {
        g.drawRoundRect(L, T, w - 1, h - 1, roundRectSize, roundRectSize);
        g.setPaint(GP);
        g.fillRoundRect(L + 1, T + 1, w - 2, h - 2, roundRectSize, roundRectSize);
        g.setPaint(isDisablePainted()? disabledColor : Color.WHITE);
        g.drawRoundRect(L + 1, T + 1, w - 3, h - 3, roundRectSize, roundRectSize);
    } else {
        g.drawRect(L, T, w - 1, h - 1);
        g.setPaint(GP);
        g.fillRect(L + 1, T + 1, w - 2, h - 2);
        g.setPaint(isDisablePainted()? disabledColor : Color.WHITE);
        g.drawRect(L + 1, T + 1, w - 3, h - 3);
    }
    if (isGradientePinteDetalhe()) {
        g.setPaint(getGradienteCorDetalhe());
        GeneralPath path = new GeneralPath();
        path.moveTo(L + 2, T + 2);
        path.quadTo(L + w / 2 + 1, T + h / 2 + 1, L + w - 1, T + 2);
        path.closePath();
        g.fill(path);
    }
    //g.setComposite(originalComposite);

}
 
Example 11
Project: rapidminer   File: DynamicIconUrlStreamHandler.java   View Source Code Vote up 5 votes
@Override
public void drawIcon(Graphics2D g2, int width, int height, int percentage) {
	g2.setColor(Color.WHITE);
	g2.setPaint(new GradientPaint(0, 0, Color.WHITE.darker(), 0, (float) (height * 0.5), Color.WHITE, true));
	g2.fillRect(0, 0, width, height);
	g2.setColor(Color.GREEN);
	g2.setPaint(new GradientPaint(0, 0, Color.GREEN.darker(), percentage, (float) (height * 0.5), Color.GREEN
			.darker().darker(), false));
	g2.fillRect(0, 0, (int) (percentage * 200d / 100d), height);
	g2.setColor(Color.BLACK);
	g2.drawRect(0, 0, width - 1, height - 1);
	g2.dispose();
}
 
Example 12
Project: xdman   File: XDMProgressBarUI.java   View Source Code Vote up 5 votes
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
	Insets b = progressBar.getInsets(); // area for border
	int barRectWidth = progressBar.getWidth() - (b.right + b.left);
	int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);

	if (barRectWidth <= 0 || barRectHeight <= 0) {
		return;
	}

	// amount of progress to draw
	int amountFull = getAmountFull(b, barRectWidth, barRectHeight);

	Graphics2D g2 = (Graphics2D) g;
	g2.setColor(progressBar.getForeground());

	if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
		g2.setPaint(high);
		g2.fillRect(0, 0, amountFull, c.getHeight() / 2);
		g2.setPaint(low);
		g2.fillRect(0, c.getHeight() / 2, amountFull, c.getHeight());
	} else { // VERTICAL
	}

	// Deal with possible text painting
	if (progressBar.isStringPainted()) {
		paintString(g, b.left, b.top, barRectWidth, barRectHeight,
				amountFull, b);
	}
}
 
Example 13
Project: jdk8u-jdk   File: ToolBarSeparatorPainter.java   View Source Code Vote up 5 votes
@Override
protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
    //it is assumed that in the normal orientation the separator renders
    //horizontally. Other code rotates it as necessary for a vertical
    //separator.
    g.setColor(c.getForeground());
    int y = height / 2;
    for (int i=INSET; i<=width-INSET; i+=SPACE) {
        g.fillRect(i, y, 1, 1);
    }
}
 
Example 14
Project: Pogamut3   File: ShedCollapseWidget.java   View Source Code Vote up 5 votes
private void paintMinus(Graphics2D g, Point center, int thickness) {
    g.fillRect(
            center.x - SQUARE_SIZE / 2,
            center.y - thickness / 2,
            SQUARE_SIZE,
            thickness);
}
 
Example 15
Project: EVB   File: GameWindow.java   View Source Code Vote up 5 votes
private void paintContent(Graphics g) {
	Graphics2D gr = (Graphics2D) g;
	// fitting
	double xspace = getWidth() / 2;
	double yspace = getHeight() / game.ceilingHeight;
	int posXStart;
	int posYStart;
	int drawWidth;
	int drawHeight;
	if (yspace > xspace) {
		posXStart = 0;
		drawWidth = getWidth();
		posYStart = (int) ((getHeight() - drawWidth * game.ceilingHeight / 2) / 2);
		drawHeight = (int) (drawWidth * game.ceilingHeight / 2);
	} else {
		posYStart = 0;
		drawHeight = getHeight();
		posXStart = (int) (getWidth() - drawHeight / game.ceilingHeight * 2) / 2;
		drawWidth = (int) (drawHeight / game.ceilingHeight * 2);
	}
	gr.setClip(posXStart, posYStart, drawWidth, drawHeight);
	int middleX = posXStart + drawWidth / 2;
	// background
	gr.setColor(COLOR_BACKGROUND_0);
	gr.fillRect(posXStart, posYStart, drawWidth / 2, drawHeight);
	gr.setColor(COLOR_BACKGROUND_1);
	gr.fillRect(middleX, posYStart, drawWidth / 2, drawHeight);
	// GameObjects
	// players
	drawPlayer(gr, game.playerLeft, COLOR_PLAYER_0, posXStart, posYStart, drawWidth, drawHeight);
	drawPlayer(gr, game.playerRight, COLOR_PLAYER_1, posXStart, posYStart, drawWidth, drawHeight);
	// ball
	gr.setColor(COLOR_BALL);
	double ballDiameter = drawWidth * game.ballRadius;
	gr.fillOval((int) (posXStart + game.ball.getPosX() * drawWidth / 2 - ballDiameter / 2),
			(int) (posYStart + (1 - game.ball.getPosY() / game.ceilingHeight) * drawHeight - ballDiameter / 2), (int) ballDiameter, (int) ballDiameter);
}
 
Example 16
Project: BaseClient   File: CFont.java   View Source Code Vote up 4 votes
protected BufferedImage generateFontImage(Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars)
{
    int imgSize = (int) this.imgSize;
    BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
    g.setFont(font);
    g.setColor(new Color(255, 255, 255, 0));
    g.fillRect(0, 0, imgSize, imgSize);
    g.setColor(Color.WHITE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    FontMetrics fontMetrics = g.getFontMetrics();
    int charHeight = 0;
    int positionX = 0;
    int positionY = 1;

    for (int i = 0; i < chars.length; i++)
    {
        char ch = (char) i;
        CharData charData = new CharData();
        Rectangle2D dimensions = fontMetrics.getStringBounds(String.valueOf(ch), g);
        charData.width = (dimensions.getBounds().width + 8);
        charData.height = dimensions.getBounds().height;

        if (positionX + charData.width >= imgSize)
        {
            positionX = 0;
            positionY += charHeight;
            charHeight = 0;
        }

        if (charData.height > charHeight)
        {
            charHeight = charData.height;
        }

        charData.storedX = positionX;
        charData.storedY = positionY;

        if (charData.height > this.fontHeight)
        {
            this.fontHeight = charData.height;
        }

        chars[i] = charData;
        g.drawString(String.valueOf(ch), positionX + 2, positionY + fontMetrics.getAscent());
        positionX += charData.width;
    }

    return bufferedImage;
}
 
Example 17
Project: openjdk-jdk10   File: FontPanel.java   View Source Code Vote up 4 votes
private void drawText( Graphics g, int w, int h ) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.white);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.black);

    /// sets font, RenderingHints.
    setParams( g2 );

    /// If flag is set, recalculate fontMetrics and reset the scrollbar
    if ( updateFontMetrics || isPrinting ) {
        /// NOTE: re-calculates in case G2 transform
        /// is something other than NONE
        calcFontMetrics( g2, w, h );
        updateFontMetrics = false;
    }
    /// Calculate the amount of text that can be drawn...
    calcTextRange();

    /// Draw according to the set "Text to Use" mode
    if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
        int charToDraw = drawStart;
        if ( showGrid )
          drawGrid( g2 );

        for ( int i = 0; i < numCharDown && charToDraw <= drawEnd; i++ ) {
          for ( int j = 0; j < numCharAcross && charToDraw <= drawEnd; j++, charToDraw++ ) {
              int gridLocX = j * gridWidth + canvasInset_X;
              int gridLocY = i * gridHeight + canvasInset_Y;

              modeSpecificDrawChar( g2, charToDraw,
                                    gridLocX + gridWidth / 2,
                                    gridLocY + maxAscent );

          }
        }
    }
    else if ( textToUse == USER_TEXT ) {
        g2.drawRect( 0, 0, w - 1, h - 1 );
        for ( int i = drawStart; i <= drawEnd; i++ ) {
            int lineStartX = canvasInset_Y;
            int lineStartY = ( i - drawStart ) * gridHeight + maxAscent;
            modeSpecificDrawLine( g2, userText[i], lineStartX, lineStartY );
        }
    }
    else {
        float xPos, yPos = (float) canvasInset_Y;
        g2.drawRect( 0, 0, w - 1, h - 1 );
        for ( int i = drawStart; i <= drawEnd; i++ ) {
            TextLayout oneLine = (TextLayout) lineBreakTLs.elementAt( i );
            xPos =
              oneLine.isLeftToRight() ?
              canvasInset_X : ( (float) w - oneLine.getAdvance() - canvasInset_X );

            float fmData[] = {0, oneLine.getAscent(), 0, oneLine.getDescent(), 0, oneLine.getLeading()};
            if (g2Transform != NONE) {
                AffineTransform at = getAffineTransform(g2Transform);
                at.transform( fmData, 0, fmData, 0, 3);
            }
            //yPos += oneLine.getAscent();
            yPos += fmData[1]; // ascent
            //oneLine.draw( g2, xPos, yPos );
            tlDrawLine( g2, oneLine, xPos, yPos );
            //yPos += oneLine.getDescent() + oneLine.getLeading();
            yPos += fmData[3] + fmData[5]; // descent + leading
        }
    }
    g2.dispose();
}
 
Example 18
Project: openjdk-jdk10   File: WriteAfterAbort.java   View Source Code Vote up 4 votes
private void test(final ImageWriter writer) throws IOException {
    try {
        // Image initialization
        final BufferedImage imageWrite =
                new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY);
        final Graphics2D g = imageWrite.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();

        // File initialization
        file = File.createTempFile("temp", ".img");
        fos = new SkipWriteOnAbortOutputStream(file);
        final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        writer.setOutput(ios);
        writer.addIIOWriteProgressListener(this);

        // This write will be aborted, and file will not be touched
        writer.write(imageWrite);
        if (!isStartedCalled) {
            throw new RuntimeException("Started should be called");
        }
        if (!isProgressCalled) {
            throw new RuntimeException("Progress should be called");
        }
        if (!isAbortCalled) {
            throw new RuntimeException("Abort should be called");
        }
        if (isCompleteCalled) {
            throw new RuntimeException("Complete should not be called");
        }
        // Flush aborted data
        ios.flush();

        /*
         * This write should be completed successfully and the file should
         * contain correct image data.
         */
        abortFlag = false;
        isAbortCalled = false;
        isCompleteCalled = false;
        isProgressCalled = false;
        isStartedCalled = false;
        writer.write(imageWrite);

        if (!isStartedCalled) {
            throw new RuntimeException("Started should be called");
        }
        if (!isProgressCalled) {
            throw new RuntimeException("Progress should be called");
        }
        if (isAbortCalled) {
            throw new RuntimeException("Abort should not be called");
        }
        if (!isCompleteCalled) {
            throw new RuntimeException("Complete should be called");
        }
        ios.close();

        // Validates content of the file.
        final BufferedImage imageRead = ImageIO.read(file);
        for (int x = 0; x < WIDTH; ++x) {
            for (int y = 0; y < HEIGHT; ++y) {
                if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {
                    throw new RuntimeException("Test failed.");
                }
            }
        }
    } finally {
        writer.dispose();
        if (file != null) {
            if (fos != null) {
                fos.close();
            }
            Files.delete(file.toPath());
        }
    }
}
 
Example 19
Project: GIFKR   File: WatermarkFilter.java   View Source Code Vote up 4 votes
@Override
public BufferedImage apply(BufferedImage img) {
	
	final int minSize = 2;
	//final int maxSize = 2*img.getWidth();

	float fontS = (float) Math.max(fontSize, minSize);
	
	Graphics2D g = (Graphics2D) img.getGraphics();
	g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	

	g.setFont(font.deriveFont(fontS));
	FontMetrics fm = g.getFontMetrics();
	int strWidth = fm.stringWidth(text), strHeight = fm.getHeight();
	
	g.setColor(backgroundColor);
	g.fillRect(position.getX(img.getWidth(), strWidth), position.getY(img.getHeight(), strHeight), strWidth, strHeight);
	
	g.setColor(fontColor);
	g.drawString(text, position.getX(img.getWidth(), strWidth), position.getY(img.getHeight(), strHeight)-fm.getDescent()+strHeight);
	
	return img;
}
 
Example 20
Project: Wurst-MC-1.12   File: WurstFont.java   View Source Code Vote up 4 votes
private void setupTexture(final boolean antiAlias)
{
	if(font.getSize() <= 15)
	{
		IMAGE_WIDTH = 256;
		IMAGE_HEIGHT = 256;
	}
	if(font.getSize() <= 43)
	{
		IMAGE_WIDTH = 512;
		IMAGE_HEIGHT = 512;
	}else if(font.getSize() <= 91)
	{
		IMAGE_WIDTH = 1024;
		IMAGE_HEIGHT = 1024;
	}else
	{
		IMAGE_WIDTH = 2048;
		IMAGE_HEIGHT = 2048;
	}
	
	final BufferedImage img = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
		BufferedImage.TYPE_INT_ARGB);
	final Graphics2D g = (Graphics2D)img.getGraphics();
	g.setFont(font);
	
	g.setColor(new Color(255, 255, 255, 0));
	g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
	g.setColor(Color.white);
	
	int rowHeight = 0;
	int positionX = 0;
	int positionY = 0;
	for(int i = 0; i < 2048; i++)
	{
		final char ch = (char)i;
		final BufferedImage fontImage = getFontImage(ch, antiAlias);
		
		final IntObject newIntObject = new IntObject();
		
		newIntObject.width = fontImage.getWidth();
		newIntObject.height = fontImage.getHeight();
		
		if(positionX + newIntObject.width >= IMAGE_WIDTH)
		{
			positionX = 0;
			positionY += rowHeight;
			rowHeight = 0;
		}
		
		newIntObject.storedX = positionX;
		newIntObject.storedY = positionY;
		
		if(newIntObject.height > fontHeight)
			fontHeight = newIntObject.height;
		
		if(newIntObject.height > rowHeight)
			rowHeight = newIntObject.height;
		chars[i] = newIntObject;
		g.drawImage(fontImage, positionX, positionY, null);
		
		positionX += newIntObject.width;
	}
	
	try
	{
		texID = TextureUtil.uploadTextureImageAllocate(
			TextureUtil.glGenTextures(), img, true, true);
	}catch(final NullPointerException e)
	{
		e.printStackTrace();
	}
}