graphics2d

Java anti fillRect (fill everything outside of said rectangle)

偶尔善良 提交于 2019-12-06 23:43:35
In Java, there is the Graphics2D.fillRect(x, y, width, height) function. In my program, I am looking for something similar, yet completely opposite. I need to fill everything on the screen except this certain x, y, width, height, sort of like an anti-fillRect. Is there a built in function that I am overlooking, or can you point me in the right direction to create one? Not required, but it would be a nice bonus if it could work with other Java 2D shapes. There are a few ways that might be achieved, the easiest might be to use java.awt.geom.Area which is a highly versatile Shape implementation,

Where's Polygon.Double in Java?

倖福魔咒の 提交于 2019-12-06 20:33:23
问题 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. 回答1: 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

Graphics2D Drawing Performance

我只是一个虾纸丫 提交于 2019-12-06 15:41:34
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? I wrote a Java Spirograph GUI that draws tens of thousands of line segments in 20 milliseconds or less. Make sure you are doing your

How to make an image blink in a random position?

徘徊边缘 提交于 2019-12-06 14:53:02
问题 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

Drawing a JComponent inside a JPanel

梦想与她 提交于 2019-12-06 14:10:23
I am trying to display a JComponent inside a JPanel. I am using null layout because location of the components can be changed during runtime and I have to control them. But the following code does not work. The JComponent only becomes visible on display if I explicity call the "paintComponent" method, which I think is not good practice. My JComponent Class public class MyIcon extends JComponent { private double xPos; private double yPos; private double radius = 30; public MyIcon(double xPos, double yPos) { this.xPos = xPos; this.yPos = yPos; this.setBounds((int)xPos, (int)yPos, (int)radius,

how to capture graphics primitives from Graphics2D into SVG

[亡魂溺海] 提交于 2019-12-06 13:15:37
I need to capture the graphics primitives from calls to java.awt.Graphics2D. The calls are made through an Open Source toolkit (Apache's PDFBox) and normally rendered in a JPanel. I would like to intercept these calls and translate them to SVG so I can build a non-graphics data model (e.g. tables, graphs, etc.). I'll be happy for any type of hack at this stage, which might include: replacing the Graphics2D library with MyGraphics2D Finding a Graphics2D that supported SVG output and capturing that intercepting the calls to Graphics2D (I'd prefer not because I have to manage the affine

Drawing multiple graphic2d components into JPanel

我们两清 提交于 2019-12-06 04:13:12
I've read a lot of tutorials on drawing Graphics2D components and adding to JPanel/JFrame but I can't find how to add multiple these components into one JPanel simply. My code below adds only 1 component (line) and I can't find why it isn't possible to add more. What am I doing wrong? Desired behaviour: there should be 3 red lines. My whole code: package Examples; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.BorderFactory; import

Java: Detecting image format, resize (scale) and save as JPEG

白昼怎懂夜的黑 提交于 2019-12-05 19:02:49
问题 This is the code I have, it actually works, not perfectly but it does, the problem is that the resized thumbnails are not pasting on the white Drawn rectangle, breaking the images aspect ratio, here is the code, could someone suggest me a fix for it, please? Thank you import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io

Java Printing Font Stretch

杀马特。学长 韩版系。学妹 提交于 2019-12-05 15:22:42
I just got the printer to work in java how I need it too, but there's one last problem I need to solve. When it prints, the font's width is rather stretched, and not crisp and clear like it should be. Here is my code my the actual drawing to the paper: FontMetrics metrics = graphics.getFontMetrics(font); int lineHeight = metrics.getHeight(); arrangePage(graphics, pageFormat, lineHeight); if (page > pageBreaks.length){ return NO_SUCH_PAGE; } Graphics2D g = (Graphics2D) graphics; g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g.setFont(font); int y = 0; int begin = 0; if

Java - Draw text in the center of an image

☆樱花仙子☆ 提交于 2019-12-05 08:37:54
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. trashgod Using a JLabel is less work, but FontMetrics , shown here , will let you manage the geometry directly.