java-2d

How to save current state of graphics on JPanel [duplicate]

房东的猫 提交于 2021-02-19 03:28:33
问题 This question already has answers here : How to serialize Java 2D Shape objects as XML? (2 answers) Closed 6 years ago . I have a program that lets the user add rectangles and circles to JPanel using Graphics. What I want to be able to do is save the current state of the current JPanel (i.e. all of the shapes and their locations) into a file and be able to load that file back and restore that state. I have a Shapes class that extends JPanel and does all of the drawing and keeps track of the

Which amongst PixelGrabber vs getRGB is faster?

荒凉一梦 提交于 2021-01-28 12:00:34
问题 I was trying to implement Hill Cipher algorithm on an image by grabbing its pixels. And it turns out that small images were loading fine. But with bigger images (8MP or 12MP), the loading is slow; and as a result writing the image is slow too. I was grabbing each pixel using bufferedImage.getRGB(x,y) , modifying it using the algorithm, and writing the pixels simultaneously using bufferedImage.setRGB(x,y,rgb) . Now, I need some suggestions to make the loading/writing faster. I was wondering if

How to draw a curve that passes through three points?

南笙酒味 提交于 2021-01-27 10:44:04
问题 I'm trying to create a curve that passes through three given points in Java (I'm drawing the curves through a class that extends JPanel). How can I make it? 回答1: You should look into something like Catmull-Rom splines, which are basically curves that pass through a number of control points (in your case your three points). Here is an example I found after a quick google: http://www.mvps.org/directx/articles/catmull/ Hope this helps :) 回答2: A circle will pass through three points on a plane.

How to draw a curve that passes through three points?

牧云@^-^@ 提交于 2021-01-27 10:43:19
问题 I'm trying to create a curve that passes through three given points in Java (I'm drawing the curves through a class that extends JPanel). How can I make it? 回答1: You should look into something like Catmull-Rom splines, which are basically curves that pass through a number of control points (in your case your three points). Here is an example I found after a quick google: http://www.mvps.org/directx/articles/catmull/ Hope this helps :) 回答2: A circle will pass through three points on a plane.

Custom JComponent (A Line) Doesn't Show Up On JPanel

China☆狼群 提交于 2020-06-26 12:26:52
问题 It shows the line without jpanel on jframe, but it doesn't when I add it to jpanel. I've tried setting the layout manager of jpanel to null but no result. I want to use JComponents for drawing lines because I want them clickable. Main.java file: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import javax

Custom JComponent (A Line) Doesn't Show Up On JPanel

£可爱£侵袭症+ 提交于 2020-06-26 12:24:59
问题 It shows the line without jpanel on jframe, but it doesn't when I add it to jpanel. I've tried setting the layout manager of jpanel to null but no result. I want to use JComponents for drawing lines because I want them clickable. Main.java file: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import javax

How can I add a button to paintComponent panel in java?

孤者浪人 提交于 2020-06-23 18:36:13
问题 The app is an option menu which shows a drawing depending on which button the user presses. I have a container panel with a CardLayout which manages the other panels. I want to show the drawing in my paintComponent Panel, but I also want a button at the bottom of the screen so the user can go back to the main menu. I tried adding it like this but the button didn't appear: CardLayout c1 = new CardLayoutl; JPanel cont1 = new JPanel(); cont1.setLayout(c1); JPanel panel1 = new JPanel(); panel1

Java GUI Rotation and Translation of Rectangle

孤街醉人 提交于 2020-01-23 08:06:30
问题 I am trying to draw a rectangle in JPanel that would translate and then rotate itself to mimic the movement of a car. I have been able to make the rectangle translate and rotate, however it rotates around the origin of (0,0). I'm very pleased that I was able to have the rectangle move and rotate as I am very new to Java GUI, but I can not seem to get how to have the rectangle rotate around itself, because I experimented more with it, and when I initialized the rectangle and rotate it 45