How to display graph generated with graphstream inside a JavaFX GUI Component? - java

I've been trying to display Graph generated with GraphStream in a GUI which is made using JavaFX.Here is what I have so far
Graph graphInit = new Graph(); //class that handles graph population
SingleGraph graph = new SingleGraph("Graph"); //GraphStream Class
SwingNode graphViewer = new SwingNode(); //JavaFX Component to display Swing elements
graph = graphInit.genGraph(graph);
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
View view = viewer.addDefaultView(false); // false indicates "no JFrame".
graphViewer.setContent((JComponent) view);
graphHook.getChildren().add(graphViewer);
The graphViewer is a SwingNode Component.
I think GraphStream doesn't support JavaFX yet, but I'm not sure. Also, I found gs-fx which I don't know how to use.
Any help would be appreciated. If there is some other library like GraphStream to generate the graph and display it in GUI, that would work too.

public void imagesie(Graph g){
FileSinkImages pic = new FileSinkImages(OutputType.PNG, Resolutions.HD1080);
g.display();
pic.setLayoutPolicy(LayoutPolicy.COMPUTED_FULLY_AT_NEW_IMAGE);
try {
pic.setAutofit(true);
pic.writeAll(g, "sample.png");
File file = new File("sample.png");
Image image = new Image(file.toURI().toString());
imageview.setImage(image);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}

Related

Getting ImageIcon- image path in runtime

I'm trying to get the image file path or file name of an ImageIcon. I've created a screen in my Java gui app, which contains properties for a custom JButton (extends JButton). From that screen I'm setting some of the main button properties, as if it is enabled, focusable and its ImageIcon. The problem is that as if now, whenever I use this button class, which is in every screen btw, I'm loading all possible images for ImageIcons when the exdened JButton class is used. That causes the screen to freeze before any of the components are shown, while the images are loaded from classpath. In order to change that, in the settings screen, where I hava a JComboBox, containing all images for icons, there, at least that's what I can think of, should be a way to get only the name of the chosen ImageIcon- image path.
The buttons properties are stored in a properties file and that's where I intend to store the .png images names if I can get to them. The idea is to set the image name and when the button is loaded to the screen to look for and load only the image it's supposed to.
Here is a snipet of the button class now; The images are way more, but for demo purposes, I think those are enough. I'd be very grateful if anyone can help with this matter.
public class CustomButton extends JButton {
static Properties FieldProp;
public CustomButton (String text, String name) {
FieldProp = new LoadProperties().loadMainProp();
this.setText(text);
this.setName(name);
Image imgdel=null;
Image imgsmbl=null;
Image imgsmrd=null;
Image imgsmgr=null;
Image imgadd=null;
Image imgauto=null;
Image imgauto1=null;
Image imgavail=null;
Image imgbarc=null;
Image imgdb=null;
Image imgdoc=null;
Image imgexc=null;
Image imgexc1=null;
try {
imgdel = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/delete.png")));
imgsmbl = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_blue.png")));
imgsmrd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_red.png")));
imgsmgr = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_green.png")));
imgadd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/add_plus.png")));
imgauto = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation.png")));
imgauto1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation1.png")));
imgavail = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/available.png")));
imgbarc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/barcode.png")));
imgdb = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/db.png")));
imgdoc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/doc.png")));
imgexc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/excel.png")));
imgexc1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/import.png")));
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}catch (Exception e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}
ImageIcon delIcon = new ImageIcon(imgdel);
ImageIcon blIcon = new ImageIcon(imgsmbl);
ImageIcon rdIcon = new ImageIcon(imgsmrd);
ImageIcon grIcon = new ImageIcon(imgsmgr);
ImageIcon addIcon = new ImageIcon(imgadd);
ImageIcon autoIcon = new ImageIcon(imgauto);
ImageIcon autoIcon1 = new ImageIcon(imgauto1);
ImageIcon availIcon = new ImageIcon(imgavail);
ImageIcon barcIcon = new ImageIcon(imgbarc);
ImageIcon dbIcon = new ImageIcon(imgdb);
ImageIcon docIcon = new ImageIcon(imgdoc);
ImageIcon excIcon = new ImageIcon(imgexc);
ImageIcon excIcon1 = new ImageIcon(imgexc1);
Object[] items =
{
noIcon,
delIcon,
blIcon,
rdIcon,
grIcon,
addIcon,
autoIcon,
autoIcon1,
availIcon,
barcIcon,
dbIcon,
docIcon,
excIcon,
excIcon1
};
try {
int iconPosition = Integer.parseInt(FieldProp.getProperty(this.getName() + "Icon"));
String iconProp = FieldProp.getProperty(this.getName() + "Icon");
if (!iconProp.equals("0")) {
this.setIcon((ImageIcon) items[iconPosition]);
}
}catch (Exception e){
e.printStackTrace();
}
try {
this.setEnabled((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Enabled"))));
this.setFocusable((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Focusable"))));
}catch(Exception e){}

How to embed the broadcast from the camera in the container in Codename1?

In my application there is a video button. Here's the code.
#Override
protected void onGUI1_Button1Action (Component c, ActionEvent event){
try {
String value = Capture.captureVideo();
if (value != null) {
final Form previous = Display.getInstance().getCurrent();
Form preview = new Form("Preview");
preview.setLayout(new BorderLayout());
MediaPlayer pl = new MediaPlayer();
if (!value.startsWith("file:/")) {
value = "file:/" + value;
}
pl.setDataSource(value);
preview.addComponent(BorderLayout.CENTER, pl);
preview.setBackCommand(new Command("Back") {
public void actionPerformed(ActionEvent evt) {
previous.showBack();
}
});
preview.show();
}
} catch (Exception ex) {
Log.e(ex);
Dialog.show("Error", "" + ex, "OK", null);
}
}
I picked up this code from github. I don't want to broadcast the video on full screen. I need a video from camera was built into some container. That container must cover only part of the screen. I have built a GUI and put a container(Media Player) into some part of screen.
How to change the code for this purpose?
You can place a camera view finder right into your app with a new cn1lib: https://github.com/codenameone/CameraKitCodenameOne
Overlaying native widgets has been possible for a year or so by now.
Original answer which was correct when it was written is below:
Embedding camera or overlaying component on the preview screen is not yet available in codename one.
This could be done using native interface with peer component. Have a look at how Native map was implemented here

How to read a video frame by frame?

i would like to read a Mp4 file in java8-64bit frame by frame and write each frame as a jpg to my harddisk. my first attempt was to use JavaFX 2.2 media player to play the file
on a View component. i thought maybe there would be an option to register an observer to get an event each time a new frame was loaded and ready to be painted on the component surface but seems there is no such method.
it would be enough to grab just those frames/pixels that got painted on the component.
Can this be done by using the media player? the reason why i use the media player is bcs it was the simplest solution i got workin. i tryed vlcj, just 32bit, and gstreamer but without luck :(
what i got so far:
public class VideoGrabber extends extends JFrame {
// code for scene setup omitted
final MediaView view = createMediaView(...)
// some other stuff happens here
// now start the video
view.getMediaPlayer().seek(Duration.ZERO);
view.getMediaPlayer().play();
view.getMediaPlayer().setOnEndOfMedia(new Runnable()
{ // save image when done
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_BGR
view.paint(img.getGraphics());
ImageIO.write(img, "JPEG", new File("pic-"+System.currentTimeMillis()+".jpg"));
});
// somewhere else to create
private MediaView createMediaView(String url)
{
final Media clip = new Media(url);
final MediaPlayer player = new MediaPlayer(clip);
final MediaView view = new MediaView(player);
view.setFitWidth(VID_WIDTH);
view.setFitHeight(VID_HEIGHT);
return view;
}
is there somehow a way to do the following:
player.setOnNextFrameReady(final Event evt) { writeImage(evt.getFrame()) };
Thanks!
You can use the snapshot() of MediaView.
First connect a mediaPlayer to a MediaView component, then use mediaPlayer.seek() to seek the video position. And then you can use the following code to extract the image frame:
int width = mediaPlayer.getMedia().getWidth();
int height = mediaPlayer.getMedia().getHeight();
WritableImage wim = new WritableImage(width, height);
MediaView mv = new MediaView();
mv.setFitWidth(width);
mv.setFitHeight(height);
mv.setMediaPlayer(mediaPlayer);
mv.snapshot(null, wim);
try {
ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", new File("/test.png"));
} catch (Exception s) {
System.out.println(s);
}
Marvin Framework provides methods to process media files frame by frame. Below it is shown a simple video processing example that highlights the path of a snooker ball.
At the left the video is played frame by frame with an interval of 30ms between frames. At the right the video is played frame by frame, but keeping all positions of the white ball through a simple image processing approach.
The source code can be checked here and the video here.
Below the essential source code to request media file frames using Marvin:
public class MediaFileExample implements Runnable{
private MarvinVideoInterface videoAdapter;
private MarvinImage videoFrame;
public MediaFileExample(){
try{
// Create the VideoAdapter used to load the video file
videoAdapter = new MarvinJavaCVAdapter();
videoAdapter.loadResource("./res/snooker.wmv");
// Start the thread for requesting the video frames
new Thread(this).start();
}
catch(MarvinVideoInterfaceException e){e.printStackTrace();}
}
#Override
public void run() {
try{
while(true){
// Request a video frame
videoFrame = videoAdapter.getFrame();
}
}catch(MarvinVideoInterfaceException e){e.printStackTrace();}
}
public static void main(String[] args) {
MediaFileExample m = new MediaFileExample();
}
}
Frameworks like ffmpeg come with command line tools to split a video into individual frames (i.e. images in a folder).
See this question: ffmpeg split avi into frames with known frame rate
The answers contains links to Java frameworks which wrap ffmpeg.

Java ImagIO.write() changes quality during save

I am writing an image library for fun and i came across a problem that i can't seem to solve. The class is pretty simple: take a picture, process it, display it through JFrame, and finally save it as a BufferedImage (javax.imageio.ImageIO). Here is what my picture looks like through the JFrame (this is my ColorEnhance class... on the Drustan nebula):
Here is what the saved version (a png, but all types ImageIO.write() supports look the same):
I'm not sure where the change occurs, but when I run this through my blur method entire lines appear from nothing in the png... Anyways, here is some code:
public void writeToFile(BufferedImage finalPic, String nameToAppend)
{
String temp=fileName.replace(".", nameToAppend+".");
String ext=fileName.substring(fileName.indexOf(".")+1);
File file=new File(temp);
try
{
ImageIO.write(finalPic, ext.toUpperCase(), file);
System.out.println("Successfully saved to: "+temp);
} catch (Exception e) { e.getMessage(); }
}
public void displayImage(String titleName)
{
ImageIcon icon = new ImageIcon(newPic);
JFrame frame = new JFrame(titleName);
JLabel label = new JLabel(icon);
label.setIcon(icon);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setSize(WIDTH, HEIGHT+22);
frame.setVisible(true);
}
One last thing is that the save works for some processing classes better than others, if you need to see any more code just ask, thanks
Try using PNGImageEncoder from Apache XML Graphics Commons:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
PNGImageEncoder encoder = new PNGImageEncoder(new FileOutputStream("file.png"), null);
encoder.encode((RenderedImage) image);

Component Position (Vaadin)

I have a simple portlet with a few components : 3 Button objects, 1 Slider, 1 MenuBar and a picture assigned to a Label (generated by servlet). Now when I switch between pictures for a Label (I have more of them), I want the picture Label to be placed at the old picture Label object's position:
My picture Label is in the left corner of the portlet. The Button objects, MenuBar, and the Slider are under the picture Label when I select another picture Label the new picture Label is being drawn under the other components (under the Button objects , MenuBar , Slider ) so the Button objects... are top and the picture Label is at the bottom of the portlet
for example, I change the background of the picture Label by selecting the color in the menu :
newItem1.addItem("Blue",new Command(){
public void menuSelected(MenuItem selectedItem){
if(pictureA.isVisible()){
pictureB.setVisible(false);
pictureC.setVisible(false);
window.removeComponent(pictureA);
pictureA= new Label("<img src=http://localhost:8888/portlet/KiviatDiagramm?background=blue", Label.CONTENT_XHTML);
window.addComponent(pictureA);
} else {
window.showNotification("", Notification.TYPE_WARNING_MESSAGE);
}
}
});
UPDATE :
I have switched from Label objects to embedded images (Embedded) (which is a lot better) I have tried to reassign the resource on the Embedded object with the new color but it doesn't work, here is what I've done :
public void init() {
URL PictureAUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureA");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL PictureBUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureB");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL pictureCUrl= null;
try {
pictureCUrl= new URL("http://localhost:8888/portlet/pictureC");
} catch (MalformedURLException e) {
e.printStackTrace();
}
final Embedded pictureA = new Embedded("", new ExternalResource(pictureAURL));
pictureA .setType(Embedded.TYPE_IMAGE);
final Embedded pictureB = new Embedded("", new ExternalResource(pictureBURL));
pictureB .setType(Embedded.TYPE_IMAGE);
final Embedded pictureC = new Embedded("", new ExternalResource(pictureCURL));
pictureC .setType(Embedded.TYPE_IMAGE);
newItem.addItem("ColorBlue", new Command(){
public void menuSelected(MenuItem selectedItem) {
if(!pictureA.equals(pictureB )){
Resource newPictureResource = new ExternalResource("http://localhost:8888/portlet/pictureA?background=blue");
newPictureResource.setType(Embedded.TYPE_IMAGE);
pictureA.setResource(newPictureResource);
}
else {
window.showNotification("Please select pictureA");
}
}
});
rickthomas is correct, you should use the replaceComponent method. I'm pretty sure that the main problem here is that after you have removed the picture, you call addComponent(pictureA) which actually adds the component to the end of the component list. If you don't have a reference to the old picture and it's the first component, then you can use this:
window.replaceComponent(window.getComponentIterator().next(), newPicture);
In addition to that, you don't have to write HTML to show images. You can use Embedded.
If the images are in your classpath, you can use the following:
Embedded newPicture = new Embedded("", new ClassResource("my-picture.png", myApplication));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
If they are found somewhere else, use this:
URL url = new URL("http://localhost:8888/portlet/KiviatDiagramm?background=blue");
Embedded newPicture = new Embedded("", new ExternalResource(url));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
This might solve your problem.
Looking at the Vaadin API javadoc,
I found this
public void replaceComponent(Component oldComponent,Component newComponent)
I haven't tested it... but it should work.

Categories