graphics2d

Moving Shapes along an arc path with java graphics

核能气质少年 提交于 2020-01-03 23:06:55
问题 Pardon my naivety, its my first time here and first time dealing with animation of graphics in java. I'm trying to accomplish an animation of star shapes that moves along a sort of arc(trying to simulate an orbit on a 2d wise). The orbit Action is used with a Timer to animate the stars. Simply put, I have drawn several stars at various positions in a jpanel. The translation of the stars y position depends on how far that star is away from the the x axis of decline which is initialized to 300

How to create a Graphics2D instance?

徘徊边缘 提交于 2020-01-03 08:57:33
问题 What's the easiest way in Java SE 7 to obtain an instance just to plot a few points for debugging? Desktop environment. 回答1: You could use a BufferedImage : BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = image.createGraphics(); 回答2: The easiest and safest way is to use to cast the Graphics reference in paintComponent and cast it as needed. That way the Object is correctly initialized. This reference can be passed to other custom

Graphics2D Drawing Performance

可紊 提交于 2020-01-03 02:37:05
问题 I am trying some things out with manually drawing "things" with a Java Graphics2D object within a Swing component and as I reach about >2000 squares that I order the object to draw it gets really slow. I have no clue whether or not this is "common". Are 2000 objects to render really "a lot"? Is the Graphics2D object just not very performant? Should I just stop where I am now and rather switch to JOGL before I try out more complex stuff and it is too late? 回答1: I wrote a Java Spirograph GUI

Java - Draw text in the center of an image

夙愿已清 提交于 2020-01-02 04:08:19
问题 I need to write text in the center of an image. The text to write is not always the same. The code I'm using is here: // Here I first draw the image g.drawImage(img, 22, 15, 280, 225, null); // I get the text String text = photoText.getText(); // Set the text color to black g.setColor(Color.black); // I draw the string g.drawString(text, 79.5F, 220.0F); The problem is that the text isn't at the center of the image, what can I do? I only need to draw the text at the horizontal center. 回答1:

How can I implement java.awt.Composite efficiently?

无人久伴 提交于 2020-01-01 09:22:29
问题 Background: I need to be able to create imagery in "disabled" look. The commonly suggested approach is to convert images to grayscale and show the grayscaled image. The drawback is that it only works with images, making it cumbersome to show graphics where you do not have immediate access to an image in disabled state. Now I thought this could be done on the fly with java.awt.Composite (and then I would not need to know how for example an Icon is implemented to render it disabled). Only there

Creating a space for Graphics2D drawings

故事扮演 提交于 2019-12-31 07:33:08
问题 I want to draw a simple board made of Graphics2D rectangles but I also want to have one JButton under this board. I know the exact dimensions of this board in pixels and I was trying to deal with getContentPane() method and BoxLayout, like this: frame.getContentPane().add(board); frame.getContentPane().add(Box.createRigidArea(new Dimension(bWidth, bHeight))); frame.getContentPane().add(new JButton("Start")); frame.pack(); But RigidArea isn't truly invisible and it overrides my drawings. Could

Creating a space for Graphics2D drawings

荒凉一梦 提交于 2019-12-31 07:32:20
问题 I want to draw a simple board made of Graphics2D rectangles but I also want to have one JButton under this board. I know the exact dimensions of this board in pixels and I was trying to deal with getContentPane() method and BoxLayout, like this: frame.getContentPane().add(board); frame.getContentPane().add(Box.createRigidArea(new Dimension(bWidth, bHeight))); frame.getContentPane().add(new JButton("Start")); frame.pack(); But RigidArea isn't truly invisible and it overrides my drawings. Could

Rotating a polygon in Java

流过昼夜 提交于 2019-12-31 05:29:04
问题 The program I am writing draws multiple stars on the screen and gives them random directions and speeds. The stars will bounce off the edges of the panel and stay inside. I need to have the stars rotating as they are moving. I have tried so many things and I cannot figure it out. Below is the code I use to draw the stars and move them. Additional Information: -the stars are in a collection called "stars" -there are two classes that I wrote, "Star" and "MyJPanel" Set the points: (in Star class

Rotating a polygon in Java

血红的双手。 提交于 2019-12-31 05:29:02
问题 The program I am writing draws multiple stars on the screen and gives them random directions and speeds. The stars will bounce off the edges of the panel and stay inside. I need to have the stars rotating as they are moving. I have tried so many things and I cannot figure it out. Below is the code I use to draw the stars and move them. Additional Information: -the stars are in a collection called "stars" -there are two classes that I wrote, "Star" and "MyJPanel" Set the points: (in Star class

How do I specify fallback fonts in Java2D/Graphics2D

最后都变了- 提交于 2019-12-30 06:12:12
问题 I'm using g.drawString(str, x, y) to draw a String with a Graphics2D object g . The current font of g does not cover all the characters of str (I have e.g. Chinese chars in there). On Mac OS X, a fallback font seems to be automatically used, but not on Windows, where black square outlines appear instead of the wanted characters. Why is the behavior different depending on the platform? How do I specify a fallback font (or several fallback fonts) in case of missing characters? (For instance,