graphics2d

Using Java's Graphics or Graphics2D classes, how do I paint a String?

I have a String and I want to paint it onto an image. I am able to paint points and draw lines, however, even after reading the Text part of the 2D Graphics tutorial, I can't figure out how I can take a String and paint it onto my drawing. Unless I'm looking at the wrong tutorial (but it's the one I get whenever I search for anything ab...

Set bufferedimage to be a color in Java

I need to create a rectangular BufferedImage with specified color as a background, draw some pattern on the background and save it to file. My problem comes from how to create the background,I am using nexted loop: BufferedImage b_img = ... for every row for every column setRGB(r,g,b); But it's very slow when the image is large. How ...

Java setClip seems to redraw

I'm having some troubles with setClip in Java. I have a class that extends JPanel. Within that class I have overridden the paintComponent method. My paintComponent method looks something like this: paintComponent { //draw some lines here Rectangle whole = g2.getClipBounds();//g2 is my Graphics2D object Rectangle part = <s...

Font.createFont leaves files in temp directory

The code below does its work but leaves copies of the font file in the temp directory each time it is run. These files are named +~JF7154903081130224445.tmp where the number seems random for each created file. InputStream fontStream = this.getClass().getResourceAsStream("handsean.ttf"); Font baseFont = Font.createFont(Font.TRUETYPE_FONT...

Java Graphics Font - How to make sure the characters lie in particular area?

I have an Image. On the bottom portion of the image, I would like to create a colored strip say, of height 100. I am already done with creating the strip and I can basically write strings in that portion (e.g. copyright of the image etc). The following is my method. public static BufferedImage captionMyImage(BufferedImage sourceImage) {...

Java Graphics2D and blitting

Quick Java graphics question. From all the graphics tutorials I've seen it looks like using Graphics2D the entire canvas is repainted. I'm trying to make a game and I'm wondering if there's a way to only paint the parts of the canvas that are to be updated on a certain cycle. Do you guys know if this is possible / necessary? ...

Java - best library to help draw text at arbitrary anchor points

Hi all, I'm familiar with how to use the various FontMetrics functions to center text vertically, horizontally, and whatnot. However, I am looking for a library that supports drawing text at a given xy location relative to the string (e.g. I want the center of the string at x,y, or I want the upper right corner of it to be here, etc.) ...

Clear a transparent BufferedImage as fast as possible

I have a transparent BufferedImage created with the following code(not relevant how it is created, I think): GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); ...

Where's Polygon.Double in Java?

Once again I'm doing Java graphics (Graphics2D) but I noticed there is no Polygon.Double or Polygon.Float classes whereas there is Rectangle2D.Float and Rectangle2D.Double class. Does anyone know why this is? I just need to draw a triangle using doubles as points. ...

Multiple Graphics2D Objects

I have a Graphics object of JPanel and that is working fine: import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import javax.swing.JPanel; public class GraphicsTest extends JPanel { private Graphics2D g2d; private String state; private int x, y; @Override ...

How to calculate the length of a Path2D in Java?

I have some paths represented by Path2D. The Path consist of multiple CubicCurve2D or Line2D segments that are connected to each other. I would like to calculate or get the length from the start to the end of a Path. How can I calculate it or get it? Is it possible? I have checked the API documentation, but couldn't find any useful metho...

How can I draw on JPanel using another quadrant for the coordinates?

I would like to draw some shapes on a JPanel by overriding paintComponent. I would like to be able to pan and zoom. Panning and zooming is easy to do with AffineTransform and the setTransform method on the Graphics2D object. After doing that I can easyli draw the shapes with g2.draw(myShape) The shapes are defined with the "world coordin...

Java Crossword Application - what package to use?

Hi guys, I'm about to create a java crossword application but I am unsure of what packages to use to draw the crossword grid. I know you can manually draw grids with Graphics2D etc. but I'm not sure if this is the easiest way to do it as I'll need text fields in the grid squares. Anyone have any suggestions as to creating the crossword...

Java Graphics2D DrawString....

Hey guys I have a little issue here. I have a panel where I am drawing a string. This is a game so I keep redrawing the score in order to update it. However when I draw it again it is drawn on top of the previous score so it looked all garbled up. Any ideas how to fix this? comp2d.drawString(GetScore(Score),ScoreX,ScoreY); ...

Using Graphics2D to overlay text on a BufferedImage and return a BufferedImage

I have checked similarly named questions, but they don't answer this use case. Basically, I was to overlay some text (text) at a given coordinate (x,y) I have the below function in a package; protected BufferedImage Process2(BufferedImage image){ Graphics2D gO = image.createGraphics(); gO.setColor(Color.red); gO.setFont(new...

How can I rotate an image using Java/Swing and then set its origin to 0,0?

I'm able to rotate an image that has been added to a JLabel. The only problem is that if the height and width are not equal, the rotated image will no longer appear at the JLabel's origin (0,0). Here's what I'm doing. I've also tried using AffineTransform and rotating the image itself, but with the same results. Graphics2D g2d = (Gra...

Java: Detecting image format, resize (scale) and save as JPEG

This is the code I have, it actually works, not perfectly but it does, the problem is that the resized thumbnails are not pasting on the white Drawn rectangle, breaking the images aspect ratio, here is the code, could someone suggest me a fix for it, please? Thank you import java.awt.Color; import java.awt.Graphics2D; import java.awt.I...

How to set AffineTransform to rotate instead of shear?

I use the AffineTransform when drawing with Graphics2D. I use it to transform a Shape before drawing it. rx and ry are supposed to be rotation but when drawing the shapes are sheared not rotated. How can I enforce rotation? I tried using the default constructor then calling the rotate, scale and translate but the shapes looked nothing li...

Cannot get right height of text in java.awt.BufferdImage/Graphics2D

Im creating a servlet that renders a jpg/png with a given text. I want the text to be centered on the rendered image. I can get the width, but the height i'm getting seems to be wrong Font myfont = new Font(Font.SANS_SERIF, Font.BOLD, 400); BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB); Graphics2D g = i...

Java2D: Fill a convex rounded polygon (QuadCurves)

Hi, If I have a QuadCurve like this (+ = node): + + \ ./ +--⁻⁻ And I fill it in Java 2D the result is something like this: (x = colored) +xxxxxxxxx+ \xxxxxx./ +--⁻⁻ But I want to color the other side: + + x\ ./x xxx +--⁻⁻xx xxxxxxxxxxx This succeeds by drawing a rectangle around the curve in th...