graphics2d

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

Deadly 提交于 2019-12-10 19:04:28
问题 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.) I found JCommon and its text anchors which purport to do that, but I am having trouble getting it to work and the forum is heretofore unresponsive. I'm aware I could

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

久未见 提交于 2019-12-10 18:33:42
问题 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 = (Graphics2D)g; g2d.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2); super.paintComponent(g2d); If I have an image whose width is greater than its

Grey splash screen due to no repainting

只谈情不闲聊 提交于 2019-12-10 15:20:04
问题 I'm making a splash screen for my application. It's just static BufferedImage drawn by Graphics2D, inside JFrame with no decorations. My problem is: the window sometimes isn't drawn properly, it means it doesn't always contain my image, it's sometimes just grey. I tried already creating splash screen in second thread, but it didn't help. I could call splashScreen.repaint() every line, but it's nonsense... Here's my code: package SeriousSteve.MapEditor; import java.awt.Graphics2D; import java

How to animate Rectangle on a Path2D object in Graphics2D context

[亡魂溺海] 提交于 2019-12-10 05:39:06
问题 I have just started learning basics about Graphics2D class, So far I am able to draw different objects and implements ActionListener to actually move them on screen by onKeyPress . So far so good, While I thought of doing something more complicated. I want to give a path to my object and animate it on that particular path only. Something like, I will draw a line on sky and a plane should stick with that drawn line and keep it self to fly on that particular line. Now is it possible? I don't

How to make pixel perfect Line2D in - Graphics2D

这一生的挚爱 提交于 2019-12-10 02:33:54
问题 G'day, I have JPanel with some Line2D objects on it. Issue is when I draw this line it doesn't appear as I want them to. Lines are not smooth, It's hard to explain in word so I am posting an Image, Zoomed Area, How to make them look more polished rather than wrinkly. Thanks 回答1: The problem is likely that you don't have antialiasing turned on your Graphics context. Try the following line before you draw: graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE

Can you increase line thickness when using Java Graphics for an applet? I don't believe that BasicStroke works [duplicate]

倖福魔咒の 提交于 2019-12-09 14:45:52
问题 This question already has answers here : Java2D: Increase the line width (2 answers) Closed 6 years ago . I am having trouble adjusting line thickness. Can I do that in Graphics or do i have to do it in Graphics2D? If so, how do I alter the program to make it run? Thanks! import java.applet.Applet; import java.awt.*; public class myAppletNumberOne extends Applet { public void paint (Graphics page) { //Something here??? } } 回答1: Yes you have to do it in Graphics2D, but that's hardly an issue,

Moving a ball on the screen with buttons. Can't program the initial position.

▼魔方 西西 提交于 2019-12-08 20:06:31
So I'm doing this exercise, where I need to create a program that will be moving a little ball on the screen by pressing one of four buttons. I've completed it, but then I wanted to make the initial position to be in the center of the screen, so I assigned the values getWidth()/2 to xCoord and getHeight()/2 to yCoord (first I did it without the constructor, then when it didn't work I added the constructor and added repaint(), so the paintComponent() would be called) but the ball is still in the top left corner when I start the program. How can I fix this? P.S. I will appreciate any comments on

Java Graphics2D is Drawing One Pixel Off (Rounding error?)

偶尔善良 提交于 2019-12-08 18:07:34
I have been trying to make a program in Java using Graphics2D and PaintComponent. However, Java is not rounding some values correctly resulting in a few points being rendered a pixel different from where it supposed to be which gives an unclean image. You can see what I mean in the picture below. Here is my code. What can I change to fix this? Thank you! public void paintComponent( Graphics g ) { super.paintComponent( g ); g.setColor(Color.red); int orginX = getWidth()/2; int orginY = getHeight()/2; for(int i=0; i<=360; i+= 10) { double angle = Math.toRadians(i); double centerX = radius * Math

Porting Java class and methods to Android. (TextLayout, Font, Graphics2D, & more)

自闭症网瘾萝莉.ら 提交于 2019-12-08 17:03:58
问题 I've been toying around in Android and attempting to port over a Java app. Below are some questions regarding to issues I've run into and would like some guidance on. It is a rather large question (multiple questions rather). However, I'm not asking them blindly as I have researched what I could about them and attempted to put my understanding to use. I've put time into asking the questions in hopes that they are half-ways clear on what I'm wanting to achieve. I'll be offering half of my rep

BufferedImage.createGraphics() memory leak

北慕城南 提交于 2019-12-08 13:43:03
问题 The code below produces a memory leak. public static BufferedImage mergeImages2(BufferedImage base, Collection<Light> images) { BufferedImage output = new BufferedImage(base.getWidth(), base.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = output.createGraphics(); g.drawImage(base, 0, 0, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f)); for (Light l : images) { g.drawImage(l.LIGHT_IMAGE, l.x, l.y, null); l.LIGHT_IMAGE.flush(); } g.dispose(); output.flush