Making screenshot of drawing application + drawing outlines of 2D shapes - java

I am making a drawing program, using the Graphics 2D objects (lines, rectangles and ovals, namely) by placing them on a panel. With that in mind, I have 2 questions:
1) How can I store the images currently portrayed on the panel as PNG, JPG or similar file onto disk?
2) I have added a drag function. How can I implement a function so that one can see the "outline" of the rectangle, line or oval, before it is actually put onto the canvas (but not placing the outline on the canvas after the mouse button has been released)? I can't see that any of the MouseListener methods can do such a thing.

1) How can I store the images
currently portrayed on the panel as
PNG, JPG or similar file onto disk?
You can create a BufferedImage and paint any component onto it. The Screen Image class does this for you.

2) How can I implement a function so that one can see the "outline" of the rectangle, line or oval?
In this example, the shape itself may be dragged, rather than its outline, but the draw() method of class Node may be modified as desired. A rectangular outline is used for selection, as on a desktop.

1) ImageIO
http://www.java-tips.org/java-se-tips/javax.imageio/how-to-save-a-bufferedimage-to-a-png-file.html
2) Can't think of an answer for 2.

Related

JavaFX: how to clear a drawing without affecting background

Very simple question, but I could not find the answer in JavaFX docs or StackOverflow:
I have a JavaFX Canvas filled with a graph (various calls to strokeLine(), not the issue here). I need to be able to draw a rectangle over this graph, then simply clear the rectangle, without affecting the graph in the background. (Like an undo operation).
Code to draw the rectangle ('p' and 'e' are points):
gc.rect(p.getX(), p.getY(), e.getX()-p.getX(), e.getY()-p.getY());
gc.stroke();
The most obvious answer would be to use the clearRect() method, but the problem is that it clears also the portion of the graph in the background...
So the question is: how do I clear a drawing that was made with stroke(), without affecting the other drawings in the background?
You can't do this with one canvas.
Canvas only store the result of your painting operation.
This is the interest of the canvas you can stroke million times the same line and it will only store and represent the result and doesn't consume more memory.
So you if you need to draw Something over your chart you should put an other canvas over the chart and draw on the second canvas.
It might be more straight forward and much more the JavaFX-way of doing things if you just put your canvas into a Group and then just add a Rectangle node to the Group which you can remove at any time if you want.
This can be acheaved by taking snapshot(s) of your Canvas, using the .snapshot(SnapshotParameters params, WritableImage image) method. Basicly, every time you draw something on your Canvas, you take a snapshot of it and store it somewhere (for example in a ArrayList). Then you can use those snapshots to create a 'undo' operation, by using the . drawImage(Image img, double x, double y) method of Canvas's GraphicsContext, in which you would pass the snapshot you want to go back to as the Image parameter.

Creating a small images from JPanel

I'm writing simple slide program in Java. In that program, I draw lines, ellipses, rectangles and etc in each slide. What is more, like a powerpoint I want to show all of my slides' symbolic small pictures on the JList.
How should I create small images from all elements in JPanel?
Thanks.
You might look at capturing a panel's image using Screen Image, discussed here.
Addendum: See also ComponentImageCapture.
If I understood your task correct, you must inherit from JPanel and overload method paintComponent(Graphics g). Inside you can write something like g.drawLine(0,0, 10, 10)

SVG beyond bounds of sprite in AndEngine

I am using SVG extension in andengine.
This is how i load in my svg textures
this.hugoRegion = SVGBitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.hugoBuildableTextureAtlas, this, "Hugo_Sprite.svg",Width,Height,Columns, Rows);
As you see i have the the tiled region.
Everything works fine until i attach the player sprite that uses the svg to the scene and it extends the sprite beyond the image. If it collides with another sprite it collides like a meter away from the sprite's body.
For example her is a sprite, where the red box is thats how far the sprite is extended. I want it to WRAP exactly around the image..
Anyone ever ran into this issue?
I dont think that would be possible .... you need to create the svg such that it wraps the sprite, programmically that would be tough to achieve...
2nd way would be, if you dont want to re-create the svg then, i guess create a rectangle in your class which extends sprite, keep the rectangle dimensions such that it wraps the sprite and then implement the collides with function with the rectangle instead of whole svg.

can we zoom in zoom out the drawing we draw on the Canvas?

We have an old (more than 10yrs old) Java Swing applicatin which draws lots of circles and connections (lines) between those circles on a JCanvas (a subclass of JComponent) based on lab data.
Because the data becames bigger and bigger, we cannot display the entire drawing now. We have put the JCavans into a JScrollPane but it is not convenience to scroll the drawing.
Can we add zoom in zoom out for it? if yes, how? I know we can zoom image but the drawing on Canvas is an image?
thanks,
EDIT:
we draw those circles and line with Graphics within paintComponent(Graphics g) method.
You could apply a scaling Transform to the Graphics2D object passed to the paintComponent method. You can learn how to use it in the Java 2D programming trail.
Without knowing anything about your application it's hard to provide useful advice (adding a code snippet or better yet a cutdown example app would be helpful to show how things are being drawn), but I'll give it a shot:
Why don't you multiply the x,y and width,height values by a scaling factor before you draw each circle/line? I assume that somewhere your canvas is using a Graphics object to draw each shape?

java changing attributes of shapes programmatically

I want to create a number of straight lines connecting small circle shapes. then I want to change the color and width of the lines from within my program. I was thinking of using Canvas to do this but there does not seem to be a way to access individual shapes drawn on canvas to change their attributes after they are drawn. What would be an easy way to implement this?
First of all, what version of Java and which UI toolkit? AWT's Canvas is very dumb, it will not even "remember" what you have painted; when you minimize and restore the window, it will send a paint() event because it wants to be repainted.
The easiest way (if you are using AWT and stuck to Canvas) is to have a List of your shapes (either one list for all or one for circles and one for lines, or whatever you like) and make your paint method draw all of them. Then update the objects in your list. When you are done updating, call repaint() on your canvas and it will call paint() for you again.
You don't paint shapes onto a Canvas if you're using Graphics and Graphics2D functions like drawRect, drawPolygon, drawOval, etc. Once they're drawn, they don't exist as shapes anymore. You just have an image with filled-in pixels.
As mihi said, you may have to keep track of the shapes you're trying to draw, then regenerate your image if it changes. Perhaps you could also "unpaint" a shape you're trying to change by painting over it in the background color and repainting the changed shape.

Categories