graphics2d

java rotate rectangle around the center

大兔子大兔子 提交于 2019-12-05 08:04:41
I would like to rotate a rectangle around its center point and it should remain in the place that it is supposed be drawn and rotate in that space this is my code: AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(45),rectangle.width/2, rectangle.height/2); Shape transformed = transform.createTransformedShape(rectangle); g2.fill(transformed) the rectangle is rotated but it is drawn at a different part of the screen, how can I correct this? I haven't tried this, but it seems you aren't getting the correct middle of the rectangle. Try: AffineTransform transform =

Java BufferedImage saves with unwanted background color

时光怂恿深爱的人放手 提交于 2019-12-05 06:43:17
Thanks ahead of time for the help Description: The program draws, displays, and saves an image. It works as following: the object itself extends Frame. In the constructor, the object creates a BufferedImage, and calls a method that draw onto that image. Then, it displays the image onto the Frame. Finally, it saves the image into a file (I don't care what format it uses). The main program creates the object, which does the rest. Problem: The saved file always has a colored background! This is especially wierd since the displayed image is fine. If I use "jpg" format with ImageIO.write(), the

Align text to the right in a TextLayout, using Java Graphics2D API

六眼飞鱼酱① 提交于 2019-12-05 05:15:10
问题 So, I'm using the code in Java tutorial to draw a piece of text, but I don't know how to align text to the right margin. I just included attstring.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); in the code for that case but it doesn't work. protected float drawParagraph (Graphics2D g2, String text, float width, float x, float y, Boolean dir){ AttributedString attstring = new AttributedString(text); attstring.addAttribute(TextAttribute.FONT, font); if (dir ==

Java graphics repaint problem

▼魔方 西西 提交于 2019-12-05 04:52:50
问题 Having trouble with a simple paint pad in java. Issues with getting my clear button to repaint. The array is clearing but not repainting. Can anyone spot my problem or is there any different way of generating a clear button for this code. public class DrawingPanel extends JPanel { private double x1=0; private double x2=0; private double y1=0; private double y2=0; private ArrayList<Shape> myArr = new ArrayList<Shape>(); //private ArrayList<Shape> clearMyArr = new ArrayList<Shape>();

Testing graphics generation with JUnit

﹥>﹥吖頭↗ 提交于 2019-12-05 03:04:18
I'm using Java's Graphics2D to generate a graphical representation of a graph. I'm also using ImageIO to write a PNG file. ( ImageIO.write(image, "png", out); ) I'm wondering how should I write JUnit tests to test whether the generated graphics is what is expected. I could pre-generate the PNG files but what if the font is a bit different on a different machine? You could try testing for specific, known features of the output e.g.: It there a white pixel at (100,100)? It the border completely black? Is the image the expected size? And/or you could write tests for some "aggregate properties"

Where's Polygon.Double in Java?

拜拜、爱过 提交于 2019-12-05 00:15:39
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. You can use a Path2D (or a Path2D.Float or Path2D.Double ) to get the same effect. 来源: https://stackoverflow.com/questions/2377551/wheres-polygon-double-in-java

How to rotate text with Graphics2D in Java?

99封情书 提交于 2019-12-04 22:51:48
I want to rotate text on a JPanel using Graphics2D.. My code is this: double paso=d.width/numeroBarras; double alto=datos[i].valor; Font fBarras=new Font("Serif", Font.PLAIN, 15); g2.setFont(fBarras); Rectangle2D barra=new Rectangle2D.Double(x,d.height-alto,paso,alto); //g2.fill(barra); x+=paso; g2.draw(barra); g2.rotate(-Math.PI/2); g2.setColor(Color.BLACK); g2.drawString(datos[i].titulo,(float)alto,(float)paso) This method must draw a rectangle and a text over the rectangle, but when i run this method all the graphic is rotated and i just want rotate the text .. Thanks :) The method

How to make an image blink in a random position?

你说的曾经没有我的故事 提交于 2019-12-04 22:17:21
I have an image inside the JApplet and I want it to appear in a random position. It will disappear after 1 second and appear again, in another random position. How do I implement 'blinking in a random position'? import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.net.URL; public class Random extends JApplet { Image ball; public void init() { try { URL pic = new URL(getDocumentBase(), "ball.gif"); ball = ImageIO.read(pic); } catch (Exception e) { e.printStackTrace(); } } public void paint(Graphics g) { if (ball != null) { g.drawImage(ball,50,50,50,50,this); } } }

Drawing a movable line on a JPanel

假如想象 提交于 2019-12-04 19:28:47
I'm writing a program which should allow the user to create a floor plan of a room - this means they can draw lines to represent walls etc. I am using a JPanel with Graphics 2D to do all the drawing stuff and currently I have a grid set as the background for the JPanel and I can draw lines based on the mouse movement(I have an enum for the mousestate with two states - DRAGGING and IDLE). However I want to add control points to the lines so that they can be moved around the JPanel once they are drawn and I can't figure out how to this. I know that I need rectangles to represent the points but I

how to read or extract graphical componenets such as square ,rect,line etc., from a pdf document using java?

十年热恋 提交于 2019-12-04 16:54:27
I am trying to extract all datas( such as square ,rect,line etc.,) from a pdf document which was generated using iText.But I'm not able extract the content rather than text and image.I want to extract graphical components mentioned above. npinti There seem to be 3 options for this (at least those are the ones I could find), I do not know what you exactly have, so I will paste all the 3, these are in increasing levels of difficulty) First Option: You could do something like so: (taken from here) PDDocument document = null; document = PDDocument.load(inFile); List pages = document