graphics2d

Post overriding the paint method of the components in java

时光毁灭记忆、已成空白 提交于 2019-12-20 05:49:23
问题 In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing). This is usually (maybe allways - I'm not sure) done when you are creating the component for example: JPanel jPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //... my implementation of paint, some transfromations, rotation, etc }

Java Graphics2D floating point accurate drawOval alternative?

老子叫甜甜 提交于 2019-12-20 03:10:04
问题 So I am trying to draw an arc and put a circle around its round endpoint, but I am having issues due to rounding to the nearest pixel. This is visible in some but not all cases. Is there a way to draw circles using floating points and anti-aliasing to eliminate this rounding error? You can run this code to see the problem. I have drawn arcs of 0 length (appearing as large dots) instead of full arcs for clarity. import java.awt.*; import javax.swing.*; public class Example extends JFrame {

Issues Creating a Pen Tool with Java's Path2D

☆樱花仙子☆ 提交于 2019-12-20 02:40:08
问题 I've been attempting to create a pen tool for my Java drawing program using the Path2D class in conjunction with mouse listeners, but I've had baffling results. The tool will work for several seconds, but then the entire application will freeze and will have to be closed. (No exceptions occur here; the program just freezes). Here's an SSCCE that demonstrates the issue: import java.awt.BasicStroke; import java.awt.event.MouseAdapter; import java.awt.Color; import java.awt.Dimension; import

Draw an infinite line through 2 points?

落爺英雄遲暮 提交于 2019-12-20 02:29:44
问题 I'm looking for a way to draw an infinite line (a line with no end, also known as a ray) through 2 points. I can draw a line between 2 points with Line2D, no problem here. Next the infinite part needed a look. With my simple mind I thought, lets multiply the x and y coordinates from the second point with 100 and redraw the line. This works, but only in simple cases. For example here is a case in which it produces lines with different angles: g.setColor(Color.red); g2.setStroke(new BasicStroke

Java: Getting a font with a specific height in pixels

烈酒焚心 提交于 2019-12-19 17:45:49
问题 It’s easy to determine the rendered height of a font using FontMetrics , but what about the other way around? How can I obtain a font that will fit into a specific height in pixels? "Give me Verdana in a size that is 30 pixels high from ascender to descender." How do I ask Java for this? 回答1: Jen, I don't think there's a "direct" way to find a font by height; only an indirect way... by looping through the sizes, and testing the height of each is <= required height. If you're doing this once,

Line not appearing on JDesktopPane

橙三吉。 提交于 2019-12-19 12:28:39
问题 I want to draw line between two JPanels but line is not appearing on layeredPane. This is what i have done please go through it, compilable.Do try and correct this code. I have tried on drawing lines on Internal frames this way but it is not working for JPanels. package build; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point;

Image Quality Gets Ruined In Java Graphics2D Rotate

China☆狼群 提交于 2019-12-19 10:16:27
问题 I am experiencing an issue with rotating an Image with the Graphics2D rotate method. Here's an image of the issue I'm having: As I move the ball, the image gets completely distorted as it rotates. Here's my rotate method: public static void rotate(BufferedImage img, Rectangle rect, int degrees) { Graphics2D g = (Graphics2D) img.createGraphics(); g.rotate(degrees, rect.x + rect.width/2, rect.y + rect.height/2); g.drawImage(img, rect.x, rect.y, rect.width, rect.height, null); g.dispose(); } Is

Drawing slices of a circle in java?

被刻印的时光 ゝ 提交于 2019-12-19 09:53:25
问题 I want to represent a timer by having a filled circle which is fully drawn over the course of the timer by segment. I.e. If the circle is filled in every 1 second for a timer of 4 seconds the first will show a quarter of a circle then a half then three-quarters and finally a full circle. Is there a way to draw these slices of a circle in java? I've looked into arbitrary shapes in the graphics API but not sure if this is the right way to go or if there is something written into the language

Drawing slices of a circle in java?

我怕爱的太早我们不能终老 提交于 2019-12-19 09:53:05
问题 I want to represent a timer by having a filled circle which is fully drawn over the course of the timer by segment. I.e. If the circle is filled in every 1 second for a timer of 4 seconds the first will show a quarter of a circle then a half then three-quarters and finally a full circle. Is there a way to draw these slices of a circle in java? I've looked into arbitrary shapes in the graphics API but not sure if this is the right way to go or if there is something written into the language

Set BufferedImage to be a color in Java

拈花ヽ惹草 提交于 2019-12-19 05:04:49
问题 I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don't know how to create the background. I am using a nested 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 to set the color in a more efficient way? 回答1: Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height) .