Posts Tagged “java/eclipse”

JFreeChart is a very handy charting API for Java. Here are some hints on how to use it correctly.

ScatterPlot Fast version verus XYPlot

The fast scatter plot is easily created but cannot change the data item shape size and color, it is always 1×1 pixel. The data format is a 2d float array


package temp;

import java.awt.RenderingHints;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.FastScatterPlot;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class ScatterPlotFast extends ApplicationFrame {
private float[][] data;

public ScatterPlotFast(final String title) {
super(title);
}

public void setData(float scatterData[][]) {
this.data = scatterData;
}

public void renderAndDisplay() {
final NumberAxis domainAxis = new NumberAxis("X");
domainAxis.setAutoRangeIncludesZero(false);
final NumberAxis rangeAxis = new NumberAxis("Y");
rangeAxis.setAutoRangeIncludesZero(false);
final FastScatterPlot plot = new FastScatterPlot(data, domainAxis, rangeAxis);
final JFreeChart chart = new JFreeChart("Fast Scatter Plot", plot);
// force aliasing of the rendered content..
chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderChartInPanel(chart);
}

private void renderChartInPanel(final JFreeChart chart) {
final ChartPanel panel = new ChartPanel(chart, true);
panel.setPreferredSize(new java.awt.Dimension(500, 270));
//      panel.setHorizontalZoom(true);
//    panel.setVerticalZoom(true);
panel.setMinimumDrawHeight(10);
panel.setMaximumDrawHeight(2000);
panel.setMinimumDrawWidth(20);
panel.setMaximumDrawWidth(2000);
setContentPane(panel);
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}
}

The slow version is a XYPlot and uses XYSeries to collect data tuplets.


package temp;

import java.awt.Color;
import java.awt.RenderingHints;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class ScatterPlotSlow extends ApplicationFrame {
private static final long serialVersionUID = -5045238125844119782L;
XYSeries data;

public ScatterPlotSlow(String title) {
super(title);
}

public void setData(float scatterData[][]) {
data = new XYSeries("Midi Note Values");
int size = 0;
if (scatterData.length > 0) {
size = scatterData[0].length;
for (int i = 0; i < size; i++) {
final double x = scatterData[0][i];
final double y = scatterData[1][i];
data.add(x, y);
}
}
}

public void renderAndDisplay() {
final XYDataset dataSet = new XYSeriesCollection(data);
final JFreeChart chart = ChartFactory.createScatterPlot("Slow Scatter Plot", "X", "Y",
dataSet, PlotOrientation.VERTICAL, true, false, false);
final XYPlot plot = chart.getXYPlot();
//dot renderer cannot be set because it has only 1 pixel dot size
//        plot.setRenderer(new XYDotRenderer());
plot.setBackgroundPaint(Color.WHITE);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot.getRenderer();
xylineandshaperenderer.setSeriesShape(0, new java.awt.Rectangle(-2, -2, 2, 2));
xylineandshaperenderer.setSeriesPaint(0, Color.BLUE);
// force aliasing of the rendered content..
chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderChartInPanel(chart);
}

private void renderChartInPanel(final JFreeChart chart) {
final ChartPanel panel = new ChartPanel(chart, true);
panel.setPreferredSize(new java.awt.Dimension(500, 270));
//      panel.setHorizontalZoom(true);
//    panel.setVerticalZoom(true);
panel.setMinimumDrawHeight(10);
panel.setMaximumDrawHeight(2000);
panel.setMinimumDrawWidth(20);
panel.setMaximumDrawWidth(2000);
setContentPane(panel);
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}
}

Comments No Comments »

EclipseAuto Format

Define a Formatter File once according to your preferred settings and use it to auto format in Eclipse after save. Enable the following settings for each project desired.

Comments Comments Off

Very handy Utils Library

http://ostermiller.org/utils/

Difference Java Streams and Readers/Writers

see http://openbook.galileocomputing.de/javainsel8/javainsel_14_004.htm#mj1d0203580263334d55e78601998800e5

Die Klassen InputStream und OutputStream bilden die Basisklassen für alle byte-orientierten Klassen und dienen somit als Bindeglied bei Methoden, die als Parameter ein Eingabe- und Ausgabe-Objekt verlangen. So ist ein InputStream nicht nur für Dateien denkbar, sondern auch für Daten, die über das Netzwerk kommen. Das Gleiche gilt für Reader und Writer; sie sind die abstrakten Basisklassen zum Lesen und Schreiben von Unicode-Zeichen und Unicode-Zeichenfolgen. Die Basisklassen geben abstrakte read()- oder write()-Methoden vor, die Unterklassen überschreiben, da nur sie wissen, wie etwas tatsächlich gelesen oder geschrieben wird.

DataInputStream - reads data primitives and also utf-8

BufferedStream there are four buffered stream classes used to wrap unbuffered streams: BufferedInputStream and BufferedOutputStream create buffered byte streams, while BufferedReader and BufferedWriter create buffered character streams.

InputStream - just byte wise no additional stuff

So the difference is streams are for byte wise read/write and readers/writers are for characters and primitives

Java fast FileIO using native IO (NIO)

http://stackoverflow.com/questions/2111749/fastest-way-of-processing-java-io-using-ascii-lines

Java – Application Freeze at JFileChooser

Obviously there seems to be a bug or a problem with file listings in Java sometimes. When creating a GUI instanciate a FileChooser always within a buttonPressed method instead of a global JFileChooser instance. The delay when pressing a button is more acceptable than a 5-6 sec delay when starting the apps GUI the first time. See http://forums.sun.com/thread.jspa?threadID=5436569

Comments Comments Off

OSGI Lazy Class Loading

Der OSGi Classloader lädt prinzipiell nur die Klassen, die benötigt werden (siehe Hakerl Activate this plug-in when one of its classes is loaded im Manifest). Es kann jedoch zu Problemen beim Classloaden kommen und es können Timeouts auftreten. Das OSGi Framework wirft eine nichtssagende BundleStatusException. Wenn man nun das besagte Hakerl im Manifest nicht setzt, werden alle Klassen beim Starten geladen und somit kein Lazy Loading durchgeführt.

Details dazu siehe

http://wiki.eclipse.org/Lazy_Start_Bundles

http://lubospeclipse.wordpress.com/eclipse-plugin-and-rcp-development-notes/

http://www.osgi.org/Specifications/HomePage

OSGI Awt and Swing issues

read about problems with OSGi and swing

http://lsd.luminis.nl/swing-and-osgi/

Comments Comments Off


Oscilliscope for Matlab (not Simulink)

http://www.oscilloscope-lib.com/

Using Java in Matlab
Attention to various settings when using Java in Matlab.

  • Java Version – Matlab 7.0 e.g. needs Java byte code compiled with 1.4. To get the Matlab VM Version use version -java
  • To find the *.jar or *.class in Matlab use which classpath.txt and edit classpath.txt This settings is read once on Matlab startup.
  • JNI DLL Reference – For using Java Native Interface the compiled Native.dll needs to be referenced in Matlab too. Use edit librarypath.txt to add to the library path. This settings is read once on Matlab startup.

Common pitfall when using Java Code in Matlab

  • Especially arrays work different index in Matlab starts with 1, see here
  • java.lang.float == Double in Matlab – For a float[] array i needed to convert the value with double(float x) else i did not get a valid value for computation. Some of this is explained here.
  • Read input without wait from the keyboard in Matlabs main thread, here and see the related tutorial.
  • There is even a good blog for Matlab related tasks and problems

Comments No Comments »

Although JAVA_HOME is pointing to the JDK, the problem with the ant task javac is that the javac command in tools.jar in %JDK%/lib is not found. Either copy tools.jar or do some registry change.
http://forums.sun.com/thread.jspa?threadID=556816

CPP Compiletask in Ant involves following settings (XML Code):








Visual Studio Environment Variables
Problems involving environment variables with the command line Visual Studio Compiler cl.exe can be found here. These must be set:

vs2005 c:\program files\microsoft visual studio 8\
psdk c:\program files\microsoft platform sdk\

First, go to vs2005 install path c:\program files\microsoft visual studio 8\vc, run vcvarsall.bat to set environment variable; then c:\program files\microsoft platform sdk\, run setenv command: eg: setenv /SRV32 /DEBUG. I use it to make the program work properly.

Using C:\Programme\Microsoft Visual Studio 8\VC\vcvarsall.bat adds the following to the PATH Var
c:\Programme\Microsoft Visual Studio 8\Common7\IDE
c:\Programme\Microsoft Visual Studio 8\VC\BIN
c:\Programme\Microsoft Visual Studio 8\Common7\Tools
c:\Programme\Microsoft Visual Studio 8\Common7\Tools\bin
c:\Programme\Microsoft Visual Studio 8\VC\PlatformSDK\bin
c:\Programme\Microsoft Visual Studio 8\SDK\v2.0\bin
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
c:\Programme\Microsoft Visual Studio 8\VC\VCPackages

Comments 1 Comment »

JODA is a very good API for working with DateTime paradigms in a much more comfortable way than the basic JDK functions do. See on Userfriendly for a good summary what JODA has to offer.

One good thing about JODA is its well organized Date and Time structures. For example from a Object with Time 19:33 you can getMinutes() == 33 or toStandardMinutes.getMinutes() == 19* 60 + 33 without having the conversion work. This works for all Years, Weeks, Month, Hours, Days, Minutes, Seconds, Milliseconds even the exceptions like differing february days every 4 years.

Formating Java Util Date (and TIme)
java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("HHmmssSSS");
String theFormattedTime = dateFormatter.format(new java.util.Date());

Comments No Comments »

For building Swing based application one might seriously have a look at the mature JGoodies libraries

JGoodies Bindings – for leveraging MVC based SW design by loose coupling domain logic such as jabva beans to Swing gui components
JGoodies Looks – with this library the Swing gui will also look good
JGoodies Forms – definite way to go to avoid Swings dull layout managers and easy and consistently place JComponents in a GUI window

http://www.jgoodies.com/articles/

[Edit]
Other projects that also features enhanced GUI elements and look and feel is listet here
http://www.tutego.com/java/additional-java-swing-components.htm

Comments No Comments »

As I’m concerned one drawback of Java is its horrible look & feel and handling of Swing GUI components. To use a better screen design, fancier icons and advanced GUI features, like transition effects of menus in MS Outlook.

Comments No Comments »

Bytecode engineering in Java is needed whenever a Class should be constructed and instanciated during runtime. JavAssist is a library that offers this kind of functionality. A textual class description of constructors, methods and member vars can be compiled and instanciated by runtime objects.

An instance can be created with with CtClass.toClass() .newInstance() . Loader cl.loadClass("net.digidop.TestRuntimeBean") does not work for me

However there is one problem. Subclasses cannot be built- e.g: AdvancedStringClass extends java.lang.String

Another interesting thing is, that only fullpath expressions are allowed in method descriptions – e.g write addPropertyChangeListener( java.beans.PropertyChangeListener pcl) instead of addPropertyChangeListener( PropertyChangeListener pcl)

Comments No Comments »