I learned to use snapshot to make an Image object out of a node. Having multiple Groups that hold various strokes, I'm now trying to create a single Image with strokes from both groups. For this purpose I'm using the following code:
Group strokes1;
Group strokes2;
WriteableImage im = null;
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
params.setViewport(new Rectangle2D(0, 0, 400, 400));
im = strokes1.snapshot(params, im);
im = strokes2.snapshot(params, im);
The documentation for the snapshot function says that
"If the image is non-null, the node will be rendered into the existing image."
However, the resulting Image im only contains strokes from strokes2. What am I doing wrong?
One way to achieve your goal is to use setComposite() method on the Graphics2D of the BufferedImage to be converted, this solution is discussed briefly here and here.
this gist provide a runnable complete example of the approach.
Related
I have about 9000 areas (i.e. 9000 lines) I have sourced in a CSV file.
There are 6 location related values in each line.
1) I, therefore, have 6 arraylists holding about 9000 values each (doing this in background Async Task) . The size of each of these array lists says "6227" or something like that - so I need to troubleshoot if some values are not being added or is there an arraylist size limitation?
2) Now, I am trying to create 9000 markers with the associated values in the title and snippet section. Please point me to a good tutorial on creating a custom marker with text views. I went to some and couldn't understand anything.
3) My third question is simple: How to efficiently handle this? I am a newcomer and I hate to say that most of the tutorials I have seen on clustering or hiding are impossible to understand. Please provide an understandable description of how to handle this problem. I am begging you.
This is how I collect the data from my CSV file; This is in the background task of AsynTask; And on PostExecute, I pass these values to the method that actually plots the marker on the Google Map.
String mLine = reader.readLine();
while (mLine != null) {
String[] coord = mLine.split(",");
Names.add(coord[0]);
city.add(coord[1]);
country.add(coord[2]);
Code.add(coord[3]);
arrLat=Double.parseDouble(coord[4]);
arrLong=Double.parseDouble(coord[5]);
arrLong=Double.parseDouble(coord[1]);
arrRadius=Double.parseDouble(coord[2]);*/
LatLng thisLoc = new LatLng(arrLat,arrLong);
coordinates.add(thisLoc);
mLine = reader.readLine();
}
For the arraylist size limitation, it should hold up to Integer.MAX_VALUE, you may refer to this link.
I would recommend Clusterer for this particular problem. You may refer to this github sample of MarkerClusterer in which every method has a description, and should be easier to understand. Then, using Viewport Marker Manager to optimize your app's performance. This turns off the markers that are not within the bounds of the screen especially when the user is zooming.
Lastly, in customizing marker with Textview, this link might be helpful. What it does is generate a bitmap and attach it to a marker.
This is the sample code for the custom marker as taken from the link:
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(200, 50, conf);
Canvas canvas = new Canvas(bmp);
canvas.drawText("TEXT", 0, 50, paint); // paint defines the text color, stroke width, size
mMap.addMarker(new MarkerOptions()
.position(clickedPosition)
//.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
.anchor(0.5f, 1)
);
Good luck!
I am trying to add tool tip to every image I draw on the JPanel. But each time a new Image is added, the tool tip text gets over written because I am using this.setToolTipText(text); for every image.
As seen in the image, the second traffic light image shows tool tip that reads traffic light 3 because that was the last image added.
Can someone please tell me another way to add unique tool tips? I searched for possible solutions but could not find any.
//Drawing the blockage on the road
g.drawImage(blockageImage, bestMatchRUnit.getX(), bestMatchRUnit.getY(), 5, 5, this);
this.setToolTipText("Blockage: " + blockageIndex); //setting the tooltip
blockageIndex++;
The easiest way is to display a JLabel with an ImageIcon on your panel. Then you can add individual tool tip messages to each JLabel.
Or, the other approach is to override the getToolTipText(...) method of your JPanel. Then you will need to keep an ArrayList of a custom Object that contains two pieces of data:
the Rectangle to represent the size/location of each image
the tool tip for the image
Then in the getToolTipText(...) method you need to iterate through the ArrayList to find the image that contains the mouse point and the you need to return the tool tip message.
I have been researching this for a while on this site and have not came up with exactly what I am looking for. I am working with software in which I do not have the source for must pass that code an Image.class image. I have 2 images that I need to overlay the first one over the second and then pass that combined image to the software I am interfacing with. I must use the Java Language for all code.
Everything I have found is writing the 2 images directly. Is what I am trying to do possible and if so please can you help me get these results? A code stub would be wonderful. I surely appreciate all of the time you take in assisting me.
Basically, you can "paint" one image on to the other...The trick is getting one of them into the correct format...
Image image1 = ...;
Image image2 = ...;
BufferedImage buffer1 = new BufferedImage(image1.getWidth(this), image1.getHeight(this), BufferedImage.TYPE_ARGB);
Graphics2D g = buffer.createGraphics();
g.drawImage(image1, 0, 0, this);
g.drawImage(image2, 0, 0, this);
g.dispose();
This will overlay image2 over image1. You can either assign buffer1 to image1 and pass it to you other class or simple pass buffer1 as BufferedImage extends from java.awt.Image
I need to convert from PNG to JPG.
However, iMagick adds a black background to it.
I saw this question which is for PHP, and tried to write the same for java like this:
// create the a jpg image
ConvertCmd cmd = new ConvertCmd();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage(brandingURL);
op.format("JPEG");
op.composite();
op.background("white");
op.addImage(imageLocation);
//op.transparent();
// execute the operation
cmd.run(op);
But still, the image comes out with a black background.
What am I missing?
I had to write the code like this:
Info imageInfo = new Info(brandingURL, true);
IMOperation op = new IMOperation();
op.addImage(brandingURL);
op.size(imageInfo.getImageWidth(), imageInfo.getImageHeight());
op.addImage("xc:white", "c://write//test.jpeg");
op.addImage("c://write//test.jpeg");
CompositeCmd composite = new CompositeCmd();
composite.run(op);
The call to background should not be necessary. Accroding to the documentation, the default background is white, which suugests to me that maybe one of your pictures has a black background that overwrites/blocks the default (maybe the the one at brandingURL?).
Quote from the above linked documentation for ImageMagick:
-background color
Set the background color.
The color is specified using the format described under the -fill option. The default background color (if none is specified or found in the image) is white.
If you are using it precisely because one of the images does specify a (black) background, I suggest you move the background call to either before adding that picture or to the end of the operation (Not sure how ImageMagick operates wrt to this)
I've been working on an assignment, and have all the requirements completed. The project is to compare the differences in runtime between a linear search algorithm and a binary search. I have a graph class that puts out the results of those searches in a xy graph.
The graph object is a Turtle class that extends JFrame. Is there any way I can convert that graph object to a bitmap and save it for future printing?
The professor requires printouts of the results. Since I don't want a printout of every time the program is run, I would prefer to save the graphics results in a designated folder, rather than using screen-grab.
Unfortunately, I haven't come up with any answers on Google or here. Is something like this even possible?
Another approach is to tell your Component to paint() itself into a BufferedImage, as seen in this complete example. The Container returned by a JFrame's getContentPane() method is suitable. In summary:
Component component = f.getContentPane();
BufferedImage image = new BufferedImage(…);
component.paint(image.getGraphics());
ImageIO.write(image,…);
You can pass the bounds of the area into the Robot.createScreenCapture(Rectangle) method to create a BufferedImage of the area. The easiest way to save the screenshot as an image file is to use the ImageIO class.