java-2d

NullPointerException at java.awt.Window.access$700(Window.java:132) while painting JPanel

☆樱花仙子☆ 提交于 2019-12-24 06:47:50
问题 I'm trying to paint component inside paint(Graphics) method of JPanel . The following code snippet works just fine, a JButton is painted nicely in my JPanel : @Override public void paint(Graphics g) { super.paint(g); JButton btn = new JButton("hello"); Dimension dim = btn.getPreferredSize(); btn.setSize(dim.width, dim.height); btn.paint(g); // paint the button } The code snippet works perfectly also for other components ( JLabel , JTree , ...) except JPanel . The following code will cause

Write text along a curve in Java

邮差的信 提交于 2019-12-24 06:44:40
问题 I was wondering if it were possible to fit text along the curve of a circle. It would be great if there were a way to accomplish this in Java2D. 回答1: Draw text along a curve with just awt and swing libraries. 来源: https://stackoverflow.com/questions/1094844/write-text-along-a-curve-in-java

Override paintComponent in Netbeans GUI

风流意气都作罢 提交于 2019-12-24 04:31:42
问题 I've added a JPanel to my Netbeans generated GUI, and add a JPanel BoxThing that overrides paintComponent and draws a small red box, but it doesn't display, paintComponent never even gets invoked. If I instantiate my own JFrame and put a JPanel containing a BoxThing in it, it works fine. I've seen this question asked a few other times on random forums, and the people don't answer the question, instead they point to the custom painting tutorial, which obviously doesn't help. I tried with

How to rotate an ImageIcon() with actionPerformed()?

好久不见. 提交于 2019-12-24 01:37:30
问题 I am having difficulties rotating an imageicon by using the arrow keys. I currently have the following as my code import java.awt.event.*; import javax.swing.*; import java.awt.*; public class startGame extends JPanel implements ActionListener,KeyListener { Timer time = new Timer(5,this); int x=0,y=0,velX = 0,velY=0; Image car1; public static void main(String[] args) { startGame game = new startGame(); JFrame frame = new JFrame(); frame.setTitle("NEED FOR SPEED"); frame.setSize(800,800);

How do I draw thick lines with closely spaced points properly with Java2D graphics?

China☆狼群 提交于 2019-12-24 00:35:50
问题 I'm trying to draw maps using Java2D. When my map is zoomed out my roads are full of drawing artefacts. This is a small part of the screen when drawing a complete US state: This is a similar section of the road when zoomed closer in: The line style used is a solid blue line with width scaled to be equivalent to 2 pixels. I've tried various rendering hints and line joining rules and nothing seems to help. I'm using Open JDK 1.7 on a Mac running the OS/X 10.8 and this is also reproducible on a

Smooth Drawing using Java2d without the Opengl or Direct3d Pipelines?

故事扮演 提交于 2019-12-23 20:32:34
问题 I can't figure out a way to get smooth movement or animation of anything using Java2d when the opengl and direct3d pipelines are disabled (by invoking the vm with -Dsun.java2d.d3d=false and -Dsun.java2d.opengl=false) The quick and dirty code below demonstrates my problem. It draws a box that moves across the screen. The box location is updated about 60 times per second and the screen is redrawn as many times as possible. It uses the BufferStrategy class to implement double buffering; the flip

how java graphics repaint method actually works

穿精又带淫゛_ 提交于 2019-12-23 02:04:37
问题 I've just started working with java 2d graphics applications, on my studies repaint is redrawing our graphics wasting a lot of resources. but I want to know what repaint is, does and how to use it efficiently, thread safely and fast for many movable dynamic objects on my canvas? 回答1: I would start by having a read through Performing Custom Painting and Painting in AWT and Swing repaint makes a request to the RepaintManager to paint part or all of a component. The RepaintManager will decide

How to get absolute coordinates after transformation

∥☆過路亽.° 提交于 2019-12-22 17:55:05
问题 I am drawing Java 2D stuff like this: g2.translate( getWidth() / 2, getHeight() / 2 ); g2.rotate( angle ); g2.draw( new Ellipse2D.Double( -1, -1, 1, 1 ) ); Now I want to kow the coordinates of the ellipse on my sceen. Any idea how to get it? So I need the conversion from logical to physical space. 回答1: Get the AffineTransform from the Graphics2D object and use the transform(src, dst) method to go to screen coordinates (you can do this for any point). If you want the path of the ellipse you

Draw series of concentric circles with random settings

让人想犯罪 __ 提交于 2019-12-22 12:53:17
问题 I'm really stuck on how to go about programming this. Need to draw a series of 8 concentric circles using Java drawArc method with following conditions using import java.util.Random library Provide for starting the drawings at random location (i.e., the x-y cooridinate must be calculated randomly). Provide a random color for each circle Provide a random diameter for each circle My current code is able to get random random color for each circle but not clear how to meet other random conditions

Stretching Polygon to other Polygon with Java

眉间皱痕 提交于 2019-12-22 09:39:33
问题 My problem is that I have a rectangle presented with a small perspective, and I would like to stretch it back to be presented as a rectangle again. To represent it visually, I currently have within my image something like the red shape, and I have 4 Points (each corner of this shape). As result I would like to have something like the blue shape, and I already have the Rectangle object for it. I was wondering if there is a method to copy a polygon and draw it as another polygon stretched. I