GNUPlot disappears immediately after its creation - java

I have created the gnuplot, but the problem is that it disappears immediately. I tried different solutions proposed in other threads, but none of them worked. Solution 1: comment the line bf.append("quit").append(NL); in the file GNUPlotParameters.java. Solution 2: put the line gp.setPersist(true); before gp.plot();.
DataSetPlot plotdata = new DataSetPlot(Xvals);
plotdata.setTitle("");
GNUPlot gp = new GNUPlot("C:\\Program Files (x86)\\gnuplot\\bin\\pgnuplot.exe");
gp.addPlot(plotdata);
gp.plot();
gp.setPersist(true);
So, how to solve this problem?
P.S. I'm running this code on Windows 7.

Unfortunately this seems to be a bug in gnuplot under windows.
See this bug report.
If you want this type of functionality, you have two options:
use CygWin version of gnuplot
save the result to a file, or use the JPlot swing component.
EDIT: There is a new version of JavaPlot which should fix this issue.

Look into the -persist option to pass to gnuplot, you could probably change the third line in your code to
GNUPlot gp = new GNUPlot("C:\\Program Files (x86)\\gnuplot\\bin\\pgnuplot.exe -persist");
or
GNUPlot gp = new GNUPlot("C:\\Program Files (x86)\\gnuplot\\bin\\pgnuplot.exe /noend");
Here is my source.

Related

JAVA-OpenCV: Video Writer not opening

I want to save the video I'm capturing with opencv in Android and I've chosen to use the openCV VideoWriter class (if there's a better way for Android please let me know).
The problem is that I'm not being capable of opening the object.
This is what I'm trying
videoWriter = new VideoWriter("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
25.0D, new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
videoWriter.open("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
25.0D, new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
I keep getting videoWriter.isOpened()=false when it should be true.
Does anyone know what I'm doing wrong? Thanks in advance
The problem was the video path! It should be fully stated, like:
pathSavedVideoFolder = getExternalFilesDir(null).getPath();
filenameRawVideo = pathSavedVideoFolder + "/SavedVideo.avi";
After this you just need to refresh in order to see the file inside "Android/data/YOUR PROJECT/files" folder.

Eclipse RCP 4.x - Defining workspace location

I know that this question was asked many times, but I didn't find an exact answer which would fulfill my desires :)
Long story short:
I've got simple E4 application, product project, feature and main plugin with simple trim window.
Works, after exporting works too.
Now. I add lifeCycleURI property, create bundleclass for it and create simple dialog with Text area and a Button. Run it\export it and it works, before running main Trim Window dialog is shown. Fine.. Cool etc.
But I want to enter location eg. C:\TEST and after clicking button I want it to be my workspace area for the application (with .metedata and so on). HOW ???
Of course I've tried with :
Location instanceLocation = Platform.getInstanceLocation();
instanceLocation.set(new URL("file", null, "C:\TEST"), false);
But... It says that I can't change location cause it is already set... Tried to use above in Activator. The same. Tried to add
-data #noDefault in products Launching Arguments ... The same...
I always try to accomplish my tasks by myself but this.... this... ehh... Help ?
You should be able to do this in the #PostContextCreate method of the life cycle class. Don't specify the '-data' argument
#PostContextCreate
public void postContextCreate()
{
Location instanceLoc = Platform.getInstanceLocation();
// Stop if location is set
if (instanceLoc.isSet())
return;
File file = new File("C:\\TEST");
instanceLocation.set(file.toURL(), false);
}
Note: You need '\\' in your file path.
This is adapted from code which I use in my e4 RCP.
If you are currently testing the application from within Eclipse you will need to clear the workspace location in the 'Run Configuration' for the application. Open 'Run > Run Configurations', find your application and clear the 'Location' field on the 'Main' tab.

Set the initial directory in SWT FileDialog

I'm working on an Eclipse RCP project and need to let the user select some file.
For convenience, based on some conditions, the initial directory of the file choosing dialog should be set prior to opening it.
As I'm bound to Eclipse RCP / SWT, I am working with the org.eclipse.swt.widgets.FileDialog.
The documentation of this FileDialog points out to use the setFilterPath(String string)-method which should do exactly what I need (see documentation).
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String [] {"*.html"});
dialog.setFilterPath("c:\\temp");
String result = dialog.open();
Unfortunately it is not working, at least not "every time".
I have currently no installation to check on it, but I'm quite sure that the feature would work totally fine on a Windows 200/XP/Vista machine.
I am working with a Windows 7 machine and I think I am suffering from the behaviour described here for lpstrInitialDir.
At least, this is exactly the behaviour I am facing: The path is good the first time I open the dialog, but the second time, the path is initially set to the last chosen path.
This seems to be convenient in most cases, but it is not in mine.
Can this be right?
If so, have I any chance on changing the behaviour according to my needs?
Thanks for any helping answer!
I ran into the same problem on Windows 10 and found a solution that seems to be working for me. A code snippet from the DirectoryDialog led to the right direction:
if (filterPath != null && filterPath.length() > 0) {
String path = filterPath.replace('/', '\\');
char[] buffer = new char[path.length() + 1];
path.getChars(0, path.length(), buffer, 0);
if (COM.SHCreateItemFromParsingName(buffer, 0, COM.IID_IShellItem, ppv) == OS.S_OK) {
IShellItem psi = new IShellItem(ppv[0]);
/*
* SetDefaultDirectory does not work if the dialog has
* persisted recently used folder. The fix is to clear the
* persisted data.
*/
fileDialog.ClearClientData();
fileDialog.SetDefaultFolder(psi);
psi.Release();
}
}
The FileDialog misses this statement 'fileDialog.ClearClientData()'. My solution is to execute the following code before setting the path and open the dialog:
long [] ppv = new long [1];
if (COM.CoCreateInstance(COM.CLSID_FileOpenDialog, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IFileOpenDialog, ppv) == OS.S_OK) {
IFileDialog fileDialog = new IFileDialog(ppv[0]);
fileDialog.ClearClientData();
fileDialog.Release();
}
Now you can set the filterpath without Windows messing things up.
I found a simple Solution for the Problem you described (I had the exact same Problem).
Just rearrange the your code like this:
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterPath("c:\\temp"); // This line is switched with the following line
dialog.setFilterExtensions(new String [] {"*.html"});
String result = dialog.open();
Somehow the Order of the methods called is relevant.
Are you using the same FileDialog object when you re-open it?
I ran a few quick tests and found that, if you re-set the filterPath, the dialog opens in the correct location.
If I open the same object again, it starts in the previously selected location.

Runnable JAR file won't

I created this card game in Java. What it does it it presents one card face up and 4 more cards face down. You wager from 1 to 100 coins and try to pick a higher card from the face down cards. If you pick a higher card, your wager is doubled and you can choose to go double or nothing on another round.
The program uses 3 .java files in one package:
HigherNumber: Main class, contains the bulk of the code.
Deck: Contains definition for a class representing a deck of cards.
Card: Contains definition for a class representing an individual card.
So naturally, this program uses a lot of pictures, to represent the cards. In my original implementation, I just passed ImageIcon a string to represent the location of the cards. So like, for the icon for a face down card,
faceDown = new ImageIcon("multimedia/redBack.gif");
When I did this, the program ran perfectly when run through Eclipse. So I used Eclipse to Export to a runnable JAR file. This JAR file then ran without a problem, except if I moved the JAR file anywhere else, none of the images showed up.
So I researched and found out about using URLs to combat this. I reworked the program to use URLs, so now I have stuff like this:
//Set URL for default faceDown icon.
faceDownURL = this.getClass().getResource(pictureRoot +"redBack.gif");
//Set location for default back face of cards.
faceDown = new ImageIcon(faceDownURL);
Now it runs fine in Eclipse, but I cannot get the exported runnable JAR to work. When run from Windows, it just kinda blinks and does nothing. When I run through the command line, I get this:
C:\Documents and Settings\mstabosz>java -jar C:\Temp\HigherNumber.jar
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at higherNumber.Card.setImage(Card.java:150)
at higherNumber.Card.<init>(Card.java:36)
at higherNumber.Deck.<init>(Deck.java:22)
at higherNumber.HigherNumber.<init>(HigherNumber.java:16)
at higherNumber.HigherNumber.main(HigherNumber.java:857)
Trying to follow this code, it looks like the source of the problem is in the Card class at line 150. At line 150, the class is in the setImage() function, which is building a string called iconName to be used to set the image for each card as it is created. It then returns an ImageIcon to the Card class's constructor.
//Set up the icon for the card.
this.cardIcon = setImage();
Line 150 is the return statement. Here are the statements that create the URL cardIconURL which is used in the ImageIcon.
//Create a URL based on the constructed string.
URL cardIconURL = this.getClass().getResource(iconName);
return new ImageIcon(cardIconURL);
I just don't get what's going wrong here. The program worked fine as a runnable JAR when I was using Strings instead of URLs. It works fine when run through Eclipse. It doesn't work as a runnable JAR now.
I did read up on something called manifests, which I had trouble understanding. I did have Eclipse generate a manifest for this program:
Manifest-Version: 1.0
Main-Class: higherNumber.HigherNumber
What am I missing?
i use something like:
URL myurl = this.getClass().getResource("file.png");
myIconImage = Toolkit.getDefaultToolkit().getImage(myurl);
you're doing:
return new ImageIcon(cardIconURL);
Maybe try my second line. Also i store the images in the jar.
Okay it looks like I got the image files in the runnable JAR by dragging and dropping the "multimedia" folder which contains the pictures into the "highernumber" package in Eclipse. I'm still getting the NullPointerException though.

How to play sound in JavaFX?

I'm experimenting with JavaFX making a small game.
I want to add sound.
How?
I tried MediaPlayer with media defined with relative source attribute like:
attribute media = Media{
source: "{__FILE__}/sound/hormpipe.mp3"
}
attribute player = MediaPlayer{
autoPlay:true
media:media
}
It doesn't play.
I get
FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: file: ... Sound.class/sound/hormpipe.mp3
Just a guess, but is that file "hornpipe.mp3" and not "hormpipe.mp3" (with an m)?
var player = javafx.scene.media.MediaPlayer {
repeatCount: javafx.scene.media.MediaPlayer.REPEAT_FOREVER
media: Media { source: "{\_\_DIR\_\_}clip.wav"
};
};
player.play();
You have to incluye the audio file in the build/compiled directory so Netbeans can pack it into the jar file.
Just a guess, but I think your {__FILE__} will expand to the name of your file. Try replacing it with {__DIR__}.
Also note that {__DIR__} includes the trailing /, so try this instead:
attribute media = Media{
source: "{__DIR__}sound/hormpipe.mp3"}
EDIT: I did some digging, and apparently, the source of a Media object has to be either a remote URL, or an absolute file path, since media files aren't allowed in JARs (something I hope gets changed with future releases, since I really like JavaFX and want to be able to make desktop apps with it). See: JavaFX FAQs.
This worked for me:
MediaPlayer audio = new MediaPlayer(
new Media(
new File("file.mp3").toURI().toString()));
Source file should be in project's root directory (not src, not dist).
OK, having used this question to get MP3 audio working (kinda), I've learned the following (not much).
1) Audio for compressed formats is very platform dependent. My continually upgraded Mint 17.1->18 machine plays mp3 fine using Media and MediaPlayer. Fresh installs of Mint 18 won't (with the dev tools).
So use .wav files.
Media sound=new Media(new File("noises/roll.wav").toURI().toString());
MediaPlayer mediaPlayer=new MediaPlayer(sound);
mediaPlayer.play();
2) One of the things you need to be aware of with Media/MediaPlayer is that in order to play multiple times (repeatedly or all at once ie, on a button press/whatever in a game) you have to spawn N number of MediaPlayer objects, and each one will play once and then stop.
So use javafx.scene.media.AudioClip
AudioClip soundMyNoise = new AudioClip(new File("noises/roll.wav").toURI().toString());
soundMyNoise.play();
AudioClip also has its issues, which include storing the raw audio data in RAM all at once instead of buffering. So there is the possibility of excessive memory use.
No matter which method you end up going with, one thing to be critically aware of was mentioned by daevon earlier - the path issue. With NetBeans, you have NetBeansProjects/yourproject/src/yourproject/foo.java. The sounds in the example above go in NetBeansProjects/yourproject/noises/roll.wav

Categories

Resources