graphics2d

Java moving a circle in a gui with arrow keys

↘锁芯ラ 提交于 2019-12-01 11:18:39
问题 I am trying to move a circle left with a keyEvent. So far, the circle is drawn on the window but it does not move left! I feel like the problem is where I add the Window() constructor to the container. The is no output on the console to tell me that it is working. So I dont think it even reaches the KeyEvent class. Here is my code: import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import javax.swing.*; public class Window extends JPanel { private static Ellipse2D

Image Quality Gets Ruined In Java Graphics2D Rotate

僤鯓⒐⒋嵵緔 提交于 2019-12-01 10:53:57
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 there anything I can do to avoid the quality loss? So currently as the ball moves, you rotate the image

Java Graphics.fillPolygon: How to also render right and bottom edges?

谁说我不能喝 提交于 2019-12-01 09:10:21
When drawing polygons, Java2D leaves off the right and bottom edges. I understand why this is done. However, I would like to draw something that includes those edges. One thing that occurred to me was to follow fillPolygon with drawPolygon with the same coordinates, but this appears to leave a gap. (See the little triangular image at the bottom.) There are two possibilities, but I can't tell which. To enable antialiasing, I'm doing this: renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderHints.put(RenderingHints.KEY_RENDERING,

Drawing slices of a circle in java?

大城市里の小女人 提交于 2019-12-01 08:06:52
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 which can easily produce these type of shapes? Any help much appreciated. Yes, it's possible. Yes,

Java Graphics.fillPolygon: How to also render right and bottom edges?

好久不见. 提交于 2019-12-01 07:09:34
问题 When drawing polygons, Java2D leaves off the right and bottom edges. I understand why this is done. However, I would like to draw something that includes those edges. One thing that occurred to me was to follow fillPolygon with drawPolygon with the same coordinates, but this appears to leave a gap. (See the little triangular image at the bottom.) There are two possibilities, but I can't tell which. To enable antialiasing, I'm doing this: renderHints = new RenderingHints(RenderingHints.KEY

WPF Line, path ..etc custom drawing style

一笑奈何 提交于 2019-12-01 04:16:49
问题 In WPF is there a way that you can modify the way any path is drawn APART from Dash-Dot sequences? Say I want draw a triple line for any path I am drawing or small triangles,waves..etc on the drawing path itself. I have tried Brushes but it will not follow the Path . Please help thx 回答1: WPF's Geometry classes have all the primitives you need to accomplish this easily, but you will need to do it in code. When I need to do custom lines I usually construct a Drawing based on the Geometry , but

Getting string size in java (without having a Graphics object available)

一世执手 提交于 2019-12-01 03:24:21
I'm trying to write application which need to draw many strings using Graphics2D class in Java. I need to get sizes of each String object (to calculate exact position of each string). There is so many strings that it should be done before the paint() method is called and only once at the beginning of my program (so then I don't have Graphics2D object yet). I know that there is a method Font.getStringBounds() but it needs a FontRenderContext object as a parameter. When i tried to create my own object: FontRenderContext frc = new FontRenderContext(MyFont.getTransform(), true, true) and then

Set BufferedImage to be a color in Java

故事扮演 提交于 2019-12-01 02:46:24
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? Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height) . BufferedImage b_img = ... Graphics2D graphics = b_img.createGraphics(); graphics.setPaint ( new Color ( r, g,

How to obtain the state of grid cells based on a specific location on a 2D array

北城余情 提交于 2019-12-01 01:04:16
Consider a 2D grid with n rows and n columns (here 75x75). The symbols (tokens) are drawn in each cell on mouse click. The code below is used to draw grid lines and symbols within cells: class DrawCanvas extends JPanel{ @Override public void paintComponent(Graphics g){ super.paintComponent(g); setBackground(Color.WHITE); //Lines g.setColor(Color.BLACK); for(int ligne = 1; ligne < ROWS; ++ligne){ g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1, gridWidth, gridWidth, gridWidth); } for(int colonne = 1; colonne < COLS; ++colonne){ g.fillRoundRect(cellSize * colonne -

How do I draw graphics to PDF using iText?

99封情书 提交于 2019-11-30 22:26:31
I am trying to complete an example that draws graphics and writes them to PDF, but I keep getting errors that the PDF has no pages. if I add something simple with document.add() after opening it works fine, I just never see the graphics. Here is my code: Document document = new Document(); PdfWriter writer = new PdfWriter(); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", " attachment; filename=\"Design.pdf\""); writer = PdfWriter.getInstance(document, response.getOutputStream()); document.open(); PdfContentByte cb = writer.getDirectContent(); Graphics2D