java-2d

Is it possible in Java2D to move the coordinate system?

Hi, I need a 2D coordinate system to which renders a user space coordinate system to swing components on the screen. Now that is exactly what Java2D does. But what I need further is to move the relative position of the screen and the coordinate system to get a kind of scrolling. In Java 2D the default offspring (0,0) is in the upper le...

Fastest way to compare pixel values between two BufferedImages?

I have a BufferedImage of type TYPE_INT_BGR. I need to do a pixel-by-pixel comparison to another BufferedImage to compute the "distance" between the two images. I have something that works, but is slow. I get a pixel from the "reference" image, break it up into RGB bytes with: int pixel = referenceImage.getRGB(col, row); int ...

How to generate events from graphics generated by Java2D

I have made an Ellipse with the help of java.awt.geom.Ellipse2D Now, I want that whenever user clicks on that ellipse, an event is generated so that I can listen to that event and peform subsequent tasks based on the ellipse which generated that event. ...

Why does this code throw a java.lang.NullPointerException?

I have found a source code and i added it to my frame just for testing which it uses Java2D. But it thows an exception. I don't understand why. my class: package ClientGUI; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.RenderingH...

Drawing multiple circles with Java2D

I am trying to use Java2D to do some simple graphics programming. I've started easy, just trying to display a couple of circles in a JFrame. I was successful displaying a single circle, but when adding a second circle, only the last circle added to the JFrame is displayed. I use class Circle to define my circle and to override the pai...

Problem in generating the border of a rectangle in Java?

I am using java.awt.geom.Rectangle2D.Double class to generate a rectangle. I want to generate a rectangle which is filled with a color (say green) and have a border (outline). Now the problem is if I call g2.draw(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D then it doesn't fill the rectangle and when I call g2.f...

How to draw a directed arrow line in Java?

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. U...

Java 2D Graphics

Hi Im trying to get to grips wth java 2d graphics Ive basically got a JPanel with a backgrounfd image in it like so: public MapFrame(Plotting pl){ this.pl =pl; this.setPreferredSize(new Dimension(984,884)); this.setBorder(BorderFactory.createEtchedBorder()); try { getFileImage("stars.jpg"); } ...

Creating a composite Shape in Java 2D

Using Java 2D I've patched several Bezier curves (CubicCurve2D) together to create a "blob". The problem I now face is how to: Efficiently fill the blob with a given colour. Efficiently determine whether a given point lies inside the blob. I noticed thst CubicCurve2D implements Shape which provides numerous contains methods for dete...

Java - drawing many images with Graphics.drawImage() and 2-screen buffer strategy distorts and cuts images.

I am using a loop to invoke double buffering painting. This, together with overriding my only Panel's repaint method, is designed to pass complete control of repaint to my loop and only render when it necessary (i.e. some change was made in the GUI). This is my rendering routine: Log.write("renderer painting"); setNeedsRender...

Java 2D Shading / Filling

I have created a "blob" from Bezier curves (screenshot below) and would now like to shade it in such a way that it appears pseudo-3D, with darker shading on all "left" edges and lighter on all "right" edges, and perhaps pure white "light spots" on the surface itself. For example: I'd be interested in how to achieve the shading used in t...

Detecting self crossing in closed Bezier curves

I've created a "blob" shape by patching cubic Bezier curves together (screenshot below). I'd like to be able to detect the situation where a curve has crossed over either itself or another curve and was wondering if there's a recommended approach or known algorithm for doing this? One idea I had was to use a FlatteningPathIterator to d...

Game design in an OO manner

I'm designing a simple game, which uses Java 2D and Newtonian physics. Currently my main "game loop" looks something like: do { for (GameEntity entity : entities) { entity.update(gameContext); } for (Drawable drawable : drawables) { drawable.draw(graphics2d); } } while (gameRunning); When an entity is instructed to u...

Java swing small 2D game: how to modelize the view?

In a small java swing 2D game, what is the best solution for creating the board view? Use a component for the board and custom paint it and each square of the checker at once? Use a component for the board and create another component modelizing the square with its own paint component doing the job for the square only. Use a layout to ...

Using Java2D and ServletOutputStream not predictable?

We have some code on production which is effectively doing what this code does: http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_Can_I_use_Java2D_to_generate_d This works fine, however I have noticed some concerning behaviour. When a servlet is requested and some image data is returned to the browser via a Servl...

Java 2D: Moving a point P a certain distance closer to another point?

What is the best way to go about moving a Point2D.Double x distance closer to another Point2D.Double? Edit: Tried to edit, but so went down for maintenance. No this is not homework I need to move a plane (A) towards the end of a runway (C) and point it in the correct direction (angle a). Here is what I have so far, but it seems mes...

How can I create a hardware-accelerated image with Java2D?

I'm trying to create a fast image generator that does lots of 2d transformations and shape rendering, so I'm trying to use a BufferedImage and then acquire the Graphics2D object to perform all my drawing. My main concern now is to make is really fast so I'm creating a BufferedImage like this: GraphicsEnvironment ge = GraphicsEnvironment...

Swing / Java2D statistics and visualisation libraries

Hi everyone, I'm looking for a multifaceted Java2D / Swing visualisation library with which I can render different statistics. Specifically, I'm looking for timeline plotting (with a configurable scrolling and compressing timeline and the ability to chart events at certain points along the timeline), line charts, pie charts, and so on, b...

Resized image degrades in quality.

I resized an image using Java2D Graphics class. But it doesn't look right. BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); Is it possible to scale an image without intro...

Custom JComponent not displaying in Custom JPanel

I've tried the add() method but nothing is displayed when I try to add Test to GraphicsTest. How should I be adding it? Can someone show me? I've included the code I'm using. This is my way and it's not working. Can someone show me or make me aware of what the problem actually is? import java.awt.Color; import java.awt.Graphics; import...