java-2d

Java2D: Increase the line width

走远了吗. 提交于 2019-11-27 11:24:44
I want to increase the Line2D width. I could not find any method to do that. Do I need to actually make a small rectangle for this purpose? You should use setStroke to set a stroke of the Graphics2D object. The example at http://www.java2s.com gives you some code examples. The following code produces the image below: import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class FrameTest { public static void main(String[] args) { JFrame jf = new JFrame("Demo"); Container cp = jf.getContentPane(); cp.add(new JComponent() { public void paintComponent(Graphics g) { Graphics2D

Draggable rectangles in Java 2D [duplicate]

ε祈祈猫儿з 提交于 2019-11-27 09:52:49
Possible Duplicate: how to drag object I need to draw some UML components (classes, packages etc) using Java 2D and then be able to drag them around. Is there a way to do this? I mean, to make a shape "draggable"? JHotDraw was designed as "a Java GUI framework for technical and structured Graphics." The linked JHotDraw Pattern Language: JHotDraw Domain Overview illustrates how to customize drawing editors. The sample org.jhotdraw.samples.draw.Main is a reasonable starting point, and JModeller is a simple UML editor built using the framework. Are you forced to swing? If not you might have look

Centering String in Panel

烂漫一生 提交于 2019-11-27 08:37:37
问题 I'm trying to center a String in a Panel. Currently I'm doing this: public void paintComponent(Graphics g) { super.paintComponent(g); int stringWidth = 0; int stringAccent = 0; int xCoordinate = 0; int yCoordinate = 0; // get the FontMetrics for the current font FontMetrics fm = g.getFontMetrics(); /** display new message */ if (currentMessage.equals(message1)) { removeAll(); /** Centering the text */ // find the center location to display stringWidth = fm.stringWidth(message2); stringAccent

Drawing the Cannabis Curve

江枫思渺然 提交于 2019-11-27 07:42:26
问题 Inspired by Cannabis Equation from the Math site (it refers to the Wolfram Research Cannabis Curve) I was wondering.. How would we draw this curve using Java-2D? 回答1: import java.awt.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.*; import java.io.File; /** Draws the CannabisCurve http://mathworld.wolfram.com/CannabisCurve.html Inspired by a question asked at: http://math.stackexchange.com/q/618786/38356 */ class CannabisCurve

How to draw circle on JPanel? Java 2D

依然范特西╮ 提交于 2019-11-27 07:28:13
问题 I have a JPanel for which I set some image as the background. I need to draw a bunch of circles on top of the image. Now the circles will be positioned based on some coordinate x,y, and the size will be based on some integer size. This is what I have as my class. import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; class ImagePanel extends JPanel { private Image img; CircleList cList; //added this public ImagePanel(Image img) { this.img = img;

How to draw a directed arrow line in Java?

流过昼夜 提交于 2019-11-27 07:27:11
I want to draw a directed arrow line through Java. At present, I am using java.awt.Line2D.Double class to draw a line g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); // g2 is an instance of Graphics2D g2.draw(new Line2D.Double(x1,y1,x2,y2)); But only the line appears and no directed arrow appears. BasicStroke.Join_BEVEL is used to draw a directed arrow. It is applied when two line segments meet. The line I am drawing meets the border of a rectangle but no directed arrow is drawn. Only a simple line is drawn. Is there anything I am missing? The bevel is drawn

Java2D Performance Issues

浪子不回头ぞ 提交于 2019-11-27 05:55:40
I'm having performance oddities with Java2D. I know of the sun.java2d.opengl VM parameter to enable 3D acceleration for 2D, but even using that has some weird issues. Here are results of tests I ran: Drawing a 25x18 map with 32x32 pixel tiles on a JComponent Image 1 = .bmp format, Image 2 = A .png format Without -Dsun.java2d.opengl=true 120 FPS using .BMP image 1 13 FPS using .PNG image 2 With -Dsun.java2d.opengl=true 12 FPS using .BMP image 1 700 FPS using .PNG image 2 Without acceleration, I'm assuming some kind of transformation is taking place with every drawImage() I do in software, and

Pacman open/close mouth animation

为君一笑 提交于 2019-11-27 05:23:22
I want to make the pacman open/close mouth animation using the easiest method. Here is my recent code: The problem is, nothing is happening? package ordner; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class PacMan implements ActionListener { private JFrame frame; private DrawPanel panel; private void initGui() { frame = new JFrame("Pacman"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new DrawPanel(); frame.add(panel); panel.setBackground(Color.BLACK); frame.setSize

Blinking in JFrame Java

白昼怎懂夜的黑 提交于 2019-11-27 04:48:55
问题 Hello guys I am doing a thread to update a ball over JFrame so I repaint the screen... and then paint the ball update its position .. and then draw the screen again ... draw the ball and the same cycle ... here is the code private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { Thread t = new Thread() { public void run() { while(true) { repaint(); b2.update(ob,2); b2.paint(ob.getGraphics()); b2.setT(b2.getT() + 1); try { Thread.sleep(50); } catch (InterruptedException ex) {

Component painting outside custom border

 ̄綄美尐妖づ 提交于 2019-11-27 03:35:59
问题 In this custom border class, I define a RoundRectangle2D shape. This object is used to paint the border. Unfortunately, since the paint method of a JComponent invokes paintComponent before paintBorder , setting the Graphics clip to the RoundRectangle2D shape has no effect; even if I issue a repaint . Therefore, the component will paint outside it's border, which is understandably undesirable. So, I was wondering: how do I get the component to paint exclusively inside a custom border? One