java-2d

Game design in an OO manner

左心房为你撑大大i 提交于 2019-11-29 19:59:51
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 update itself it will adjust its velocity and position based on the current forces applied to it. However, I need entities to exhibit other behaviour; e.g. if a "bad guy" is shot by a player the entity should be destroyed and removed from the game world. My question : What is the

How to Produce Physical Dimension of an Image drawn using Java

三世轮回 提交于 2019-11-29 18:52:24
I have drawn a square on JPanel in Swing Applet.now i want to print this square having physical dimension 50cm * 50 cm on to paper.it means the print measurement of the square is 50 * 50 cm in real world which i drawn on to panel of an applet .the printer uses 400 DPI to print. so how can i relate these to measurements ? in which file format i should save my drawing so it will have accurate measurement while printing , equals to printer DPI and quality of the image is maintained in print. Thank You Mihir Parekh You need to convert you centimeter value to inches, so you can obtain the Dots Per

Create a Trailing line of blood behind a player

放肆的年华 提交于 2019-11-29 18:16:42
I am currently working on a simple top down shooter. The object is a ball that slides around the screen, and I am trying to make a sort of wet-dragging affect. I am using Java Swing and just the default Graphics2d lib inside. This is what I have: and this is my goal: I need to know how I can make a curved line that has the ability to change alpha at the trailing end. I have searched online but I can only find non-dynamic solutions. (The tail needs to update as the player moves across the screen.) A simple solution might be to simple add each point to a List of Point s which before the player

Rotating Ball loses sharpness and colors

江枫思渺然 提交于 2019-11-29 18:15:33
package testIDE; import java.awt.BorderLayout; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import Utils.MyJFrame; public class ExampleClass { public static void main(String[] args) { JFrame ballRotate = new BallRotate(); } } class BallRotate extends MyJFrame { ArrayList<Integer> degree = new ArrayList<Integer>(); BufferedImage backGroundImage = getBufferedImage(

Draw on one side of a JPanel

你。 提交于 2019-11-29 18:00:42
I want to program an application that lets you draw circles with a mouse click on the left side of a JFrame , and all the points are getting "mirrored" to the right side. The first problem I encountered was that when I try to implement this draw-mechanic in my frame, no circles appear. public class Application{ int x,y; private JPanel container; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Application().gui(); } }); } public void gui() { int height = 250; int width = 700; JFrame jframe = new JFrame(); container = new

How to draw custom rounded rectangles in java?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 16:49:20
I know how to draw a rounded rectangle but I want to define roundness for each corner separately and draw something like the image below : There's probably a few ways to achieve this, but the easiest I can think of would be to, as Andrew has already hinted, would be to define your own Shape import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Path2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;

Rounding Inaccuracies When Combining Areas in Java?

江枫思渺然 提交于 2019-11-29 14:40:31
I'm working with Areas in Java. My test program draws three random triangles and combines them to form one or more polygons. After the Areas are .add() ed together, I use PathIterator to trace the edges. Sometimes, however, the Area objects will not combine as they should... and as you can see in the last image I posted, extra edges will be drawn. I think the problem is caused by rounding inaccuracies in Java's Area class (when I debug the test program, the Area shows the gaps before the PathIterator is used), but I don't think Java provides any other way to combine shapes. Any solutions?

how can I convert an RGB image to CMYK and vice versa in Java?

依然范特西╮ 提交于 2019-11-29 14:13:51
our web app let users download dynamically generated images in different formats (bmp, png and jpeg). Some of our users download the images for printing, thus we would like to allow them to choose between RGB or CMYK. Is there a way to specify the color model when creating a RenderedImage/BufferedImage? If not, what is the default color model and how can I change it to another? Code snippets are welcome :) Thanks, Olivier. It appears that it's not simple and you'll have to either load up a color profile to do it or extend the colorspace to support CYMK. Some image formats doesn't allow CMYK

How to create a big image file from many tiles in java?

孤者浪人 提交于 2019-11-29 10:21:49
My program produces 10 x 10 tiles images of 3000x3000 pixel, one by one (currently saved to 100 files named image_x_y.jpg ) I want to assemble these 100 images into one big image, without loading everything in memory. My goal is to create one big image file, of 30'000 * 30'000 pixels. I'm looking for a way to do this without using JAI (which cannot be installed from public maven repositories, I don't understand why) Is there a way to do this with pure java2D ? Or does another library exist, able to handle this ? My original idea was to create a very big buffered image, from a DataBuffer backed

Java: Friendlier way to get an instance of FontMetrics

ε祈祈猫儿з 提交于 2019-11-29 08:02:18
Is there a friendlier way to get an instance of FontMetrics than FontMetrics fm = Graphics.getFontMetrics(Font); I hate this way because of the following example: If you want to create in a game a menu and you want all the menuitems in the center of the screen you need fontmetrics. But, mostly, menuitems are clickable. So I create an array of Rectangles and all the rectangles fits around the items, so when the mouse is pressed, I can simply use for (int i = 0; i < rects.length; i++) if (rects[i].contains(mouseX, mouseY)) { ... } But to create the rects I also need FontMetrics for their