Manipulating Pixels(Drawing) in Java - java

This is not a typical "what library do i use?" or "is there an API for that". Using lines of code I create not implementing them through a library of sorts. Is there any documentation on how to actually create a window, and within that window draw a line from pixels?
I just want to understand how its done, and not learn through a library which does it for me.
Previously tried:
JavaFx
Swing

If you want to draw a line in a window, you'll ultimately have to create a BufferedImage, set pixels in that BufferedImage according to some drawing algorithm (like Bresenham's), then put the BufferedImage in the window.

Related

Convert Shape from awt to Shape in javafx

I want to display Shape on Pane in JavaFx. I am using oracle JGeometry object selected from spatial database, it has a method createShape() but it returns java.awt.Shape.
Is it possible to create JavaFx object also so easily? Maybe I am just blind but I haven't found anything and don't want to program too much stuff on top. Thank you
Java only supports a handful of shapes, so it is not very difficult to write a little converter but if all you want to do is to draw the AWT shapes on a JavaFX canvas, then maybe this library can be helpful: http://www.jfree.org/fxgraphics2d/

Is it possible to store constructed images for display later using Java Swing?

I am a C/C++ programmer trying my hand at Java for the first time. I'm currently working on a program that reads in a bunch of data and builds a map. I want to give the user the option to toggle various features of the map using check boxes.
In the Win32 API, I was able to accomplish this by pre-building the features on transparent bitmaps and then BitBlt()ing them over top one another. Does Java Swing support something similar? I imagine I'm not the only person who has ever wanted to do this. Building the features is relatively slow, so I only want to generate the layers once and then block copy them to the JPanel I'm using as a display.
Thanks in advance!
You could dynamically create BufferedImage objects, with alpha channels, then only paint this on a frame if the checkbox is checked.
You can store images in Swing using the BufferedImage class, and then use that to later draw the final image.
http://docs.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html
The final image can later be painted on to the JPanel (probably by overriding the JPanel's paintComponent method) by using the alpha values of the images.

Draw an Image in arbitrary corners

So i have a normal image. I defined Click- and Drag-Listeners on each corner of the rectangular image. I want to freely transform each of the corners and paint it on the screen.
The AffineTransform class provides transformation possibilities but i couldn't find a way to realize this.
I have implemented the same function in android with the Matrix.setPolytoPoly method. However there is no equivalent in java swing.
Thanks!
Edit:
I would like to illustrate this process with the following image:
This should happen when i drag one corner to the upper left. Now the problem that i have is, that i don't believe that one can model such a behaviour with a pure 3x3 matrix in an easy way. I looked at the android native code to watch the behaviour, but it's actually pretty deep in the native code.
Any idea of how to do that?
Your update shows that the transformation is not affine, as parallel lines do not remain parallel. Java Advanced Imaging (JAI) provides the required projective capabilities through the abstract class Warp.

Buffered Image for Image Tiles

I want to create a game where I want to have many image tiles which will respond to the arrow keys. Should I use BufferedImage to create every individual tile?
Refer to: Java Game Playing Area Difficulty
RobotChase is a tile-based game that uses BufferedImage in this way. Alternatives include these:
Implement the Icon interface, as shown in the examples cited here.
Set a component's text to a suitable Unicode glyph, as shown here.

Zoom in Java Swing application

I am looking for ways to zoom in a Java Swing application. That means that I would like to resize all components in a given JPanel by a given factor as if I would take an screenshot of the UI and just applied an "Image scale" operation. The font size as well as the size of checkboxes, textboxes, cursors etc. has to be adjusted.
It is possible to scale a component by applying transforms to a graphics object:
protected Graphics getComponentGraphics(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
g2d.scale(2, 2);
return super.getComponentGraphics(g2d);
}
That works as long as you don't care about self-updating components. If you have a textbox in your application this approach ceases to work since the textbox updates itself every second to show the (blinking) cursor. And since it doesn't use the modified graphics object this time the component appears at the old location. Is there a possibility to change a components graphics object permanently? There is also a problem with the mouse click event handlers.
The other possibility would be to resize all child components of the JPanel (setPreferredSize) to a new size. That doesn't work for checkboxes since the displayed picture of the checkbox doesn't change its size.
I also thought of programming my own layout manager but I don't think that this will work since layout managers only change the position (and size) of objects but are not able to zoom into checkboxes (see previous paragraph). Or am I wrong with this hypothesis?
Do you have any ideas how one could achieve a zoomable Swing GUI without programming custom components? I looked for rotatable user interfaces because the problem seems familiar but I also didn't find any satisfying solution to this problem.
Thanks for your help,
Chris
You could give a try to the JXLayer library.
There are several tools in it, which could help you to make a zoom. Check the examples shown here. I would recommend you to read more about the TransformUI, from this library. From the example, it seems like it could help solving your problem.
Scaling the view is easy enough; transforming mouse coordinates is only slightly more difficult. Here's an elementary example. I'd keep JComponents out, although it might make sense to develop an analogous ScaledComponent that knows about the geometry. That's where #Gnoupi's suggestion of using a library comes in.
hey you can try this if you want to zoom a image like any other image viewer the use a JPanel draw an image using drawImage() method now create a button and when you click the button increase the size of the panel on the frame it appears as if the image is being viewed in Zoom
You might find Piccolo2D.java API useful: http://code.google.com/p/piccolo2d/
It is very simple.
It touts in particular its smooth zooming. You essentially make a "canvas" that can contain various elements, and can then zoom by just holding right-click and panning the mouse back and forth.
I worked on a team that used it to create this: http://sourceforge.net/apps/mediawiki/guitar/index.php?title=WebGuitar#EFG.2FGUI_Visualizer
The nodes you see there are clickable links themselves.
Since Java 9, there are VM arguments (actually meant to be used for high dpi scaling) that can render a application with a higher scaling factor:
java -Dsun.java2d.uiScale=2.0 -jar MyApplication.jar
Or:
java -Dsun.java2d.win.uiScaleX=2.0 -Dsun.java2d.win.uiScaleY=2.0 -jar MyApplication.jar

Categories

Resources