graphics2d

Draw ring with given thickness, position, and radius. (Java2D)

吃可爱长大的小学妹 提交于 2020-01-14 04:35:14
问题 I need to draw a ring, with given thickness, that looks something like this: The center must be transparent, so that it doesn't cover previously drawn shapes. (or other rings) I've tried something like this: //g is a Graphics2D object g.setColor(Color.RED); g.drawOval(x,y,width,height); g.setColor(Color.WHITE); g.drawOval(x+thickness,y+thickness,width-2*thickness,height-2*thickness); which draws a satisfactory ring, but it covers other shapes; the interior is white, not transparent. How can I

java rotate rectangle around the center

北战南征 提交于 2020-01-13 09:35:10
问题 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? 回答1: I haven't tried

Java applet repaint a moving circle

谁说胖子不能爱 提交于 2020-01-13 07:07:33
问题 I've just moved over from Pygame so Java 2D in an applet is a little new to me, especially when it comes to repainting the screen. In pygame you can simply do display.fill([1,1,1]) but how do I do this in an applet in Java? I understand the use of repaint() but that doesn't clear the screen - any moving object is not 'removed' from the screen so you just get a long line of painted circles. Here's my code that I've been testing with: package circles; import java.applet.Applet; import java.awt

Why wont squares show up after repaint()?

天涯浪子 提交于 2020-01-11 07:20:09
问题 I posted this question a bit earlier and was told to make it SSCCE so here goes (if I can make any improvements feel free to let me know): I'm wondering why when my button "confirm" is clicked the old squares disappear and the redrawn squares do not appear on my GUI (made with swing). The Squares class draws 200 spaced out squares with an ID (0, 1, 2, or 3 as String) inside obtained from a different class (for the purpose of this question, let's assume it is always 0 and not include that

How can I change the size of a figure made in Java Graphics2D with a slider?

北城以北 提交于 2020-01-10 06:16:38
问题 I'm trying to make a program with java using Graphics 2D that paints polygons between 3 and 8 sides and that I can resized with a slider but I do not know how to do for change it size with the slider evenly. Here is a example of my pentagon drawn if (sides == 5){ g.drawLine(110+x,135-y, 10+x,205-y); g.drawLine(10+x,205-y, 48+x, 320-y); g.drawLine(48+x,320-y, 170+x,320-y); g.drawLine(170+x,320-y, 205+x,205-y); g.drawLine(205+x,205-y, 110+x,135-y); } Now I want to change its size evenly. (I can

How can I change the size of a figure made in Java Graphics2D with a slider?

痴心易碎 提交于 2020-01-10 06:15:26
问题 I'm trying to make a program with java using Graphics 2D that paints polygons between 3 and 8 sides and that I can resized with a slider but I do not know how to do for change it size with the slider evenly. Here is a example of my pentagon drawn if (sides == 5){ g.drawLine(110+x,135-y, 10+x,205-y); g.drawLine(10+x,205-y, 48+x, 320-y); g.drawLine(48+x,320-y, 170+x,320-y); g.drawLine(170+x,320-y, 205+x,205-y); g.drawLine(205+x,205-y, 110+x,135-y); } Now I want to change its size evenly. (I can

Java ARGB to JPG

一曲冷凌霜 提交于 2020-01-10 04:16:10
问题 How can I save BufferedImage with TYPE_INT_ARGB to jpg? Program generates me that image: And it's OK, but when I save it in that way: ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(byteStream); try { ImageIO.write(buffImg, "jpg", bos); // argb byteStream.flush(); byte[] newImage = byteStream.toByteArray(); OutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\test.jpg")); out.write(newImage); out.close(); }

Java: Why doesn't this JPanel paint properly?

十年热恋 提交于 2020-01-06 14:51:28
问题 I have a 2D array. I want each pixel to be represented by a total of four in the actual image. I've tried various piece of code but none seem to work and I don't really understand how it works either. So far I have: panel = new JPanel() { @Override public void paint(Graphics g) { Rectangle rect = g.getClipBounds(); g.setColor(Color.white); g.fillRect(rect.x, rect.y, rect.width, rect.height); for (int i = 0; i < m.width(); i++) { for (int j=0; j < m.height(); j++) { g.setColor(Color.red); g

Java: Why doesn't this JPanel paint properly?

99封情书 提交于 2020-01-06 14:50:07
问题 I have a 2D array. I want each pixel to be represented by a total of four in the actual image. I've tried various piece of code but none seem to work and I don't really understand how it works either. So far I have: panel = new JPanel() { @Override public void paint(Graphics g) { Rectangle rect = g.getClipBounds(); g.setColor(Color.white); g.fillRect(rect.x, rect.y, rect.width, rect.height); for (int i = 0; i < m.width(); i++) { for (int j=0; j < m.height(); j++) { g.setColor(Color.red); g

Graphics2D line and shape drawing issues (rendered in wrong place)

老子叫甜甜 提交于 2020-01-05 04:08:47
问题 I've drawn three arrows using Graphics2D. three drawLine s draw(Shape) fill(Shape) Here's what it looks like enlarged: I cannot understand two things: Why is the filled one smaller and shifted? Secondly, why do arrows 1. and 3. look different? Both consist of 3 antialiased lines. Shouldn't they (potentially) differ only in vertices? Here's the whole code: import javax.swing.*; import java.awt.*; public class ShapeTest extends JPanel { public static void main(String [] args) { JFrame frame =