I am a newbie in jfree and java programmin.I am in the process of creating simple XYline chart that uses multiple "home-made" range cursors.I created the cursors by the paint method of JApplet, and implemented mouse event listeners on them to make them mobile. Now the problem is that I don't seem to be able to get the the mouse cordinates that correpond to the position where any of my cursors hover.I get infinity as a value from my code below:
Code: Select all
import org.jfree.chart. * ;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot. * ;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt. * ;
import java.awt.event. * ;
import java.applet. * ;
import javax.swing. * ;
import java.awt.geom. * ;
import org.jfree.ui.RectangleEdge;
public class testcursor extends JApplet implements MouseListener, MouseMotionListener {
private JFreeChart chart;
private ChartPanel chartPanel;
Container content;
Graphics g;
int x1, y1; // Coords of top-left corner of the green cursor.
int x2, y2; // Coords of top-left corner of the blue cursor.
/* Some variables used during dragging */
boolean dragging; // Set to true when a drag is in progress.
boolean dragRedSquare; // True if green cursor is being dragged, false
// if blue cursor is being dragged.
int offsetX, offsetY; // Offset of mouse-click coordinates from
// top-left corner of the cursor that was
// clicked.
public testcursor() {
super.paint(g);
}
public void init() {
super.paint(g);
// Initialize the applet by putting the squares in a
// starting position, setting a background color, and
// setting the applet to listen for mouse events on itself.
x1 = 10;
y1 = 10;
x2 = 50;
y2 = 10;
XYSeries series = new XYSeries("XYGraph");
series.add(1, 1);
series.add(2, 2);
series.add(3, 1);
series.add(4, 9);
series.add(2, 40);
series.add(25, 60);
// Add the series to your data set
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
// Generate the graph
chart = ChartFactory.createXYLineChart("XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
// Draw the two cursors.
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(900, 600));
chartPanel.setPopupMenu(null);
chartPanel.addMouseListener(this);
chartPanel.addMouseMotionListener(this);
//add the chartPanel to the container (getContentPane is inherited from JApplet which AppletGraph extends).
// content = getContentPane();
// content.add(chartPanel);
addMouseListener(this);
addMouseMotionListener(this);
}
// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the browser.
public void stop() {
// no actions needed here now.
}
// The standard method that you have to use to paint things on screen
// This overrides the empty Applet method, you can't called it "display" for example.
public void paint(Graphics g) {
//method to draw text on screen
// String first, then x and y coordinate.
// Create a simple XY chart
int height1 = this.getHeight();
chart.draw((Graphics2D) g, getBounds());
chart.setAntiAlias(true);
g.setColor(Color.green);
g.fillRect(x1, y1, 4, height1 - 10);
g.setColor(Color.blue);
g.fillRect(x2, y2, 4, height1 - 10);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
/*if ( chart!=null ) {
chart.draw( (Graphics2D)g,getBounds()); //repaints the whole chart
}*/
}
public void mousePressed(MouseEvent evt) {
// Respond when the user presses the mouse on the applet.
// Check which cursor the user clicked, if any, and start
// dragging that cursor.
int height1 = this.getHeight();
int mouseX = evt.getX();
int mouseY = evt.getY();
System.out.println("mouse currently atx =" + mouseX + " and y = " + mouseY);
Point2D p = chartPanel.translateScreenToJava2D(new Point(mouseX, mouseY));
XYPlot plot = (XYPlot) chart.getPlot();
Rectangle2D plotArea = chartPanel.getScreenDataArea();
ValueAxis domainAxis = plot.getDomainAxis();
RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
ValueAxis rangeAxis = plot.getRangeAxis();
RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
float chartX = (float) domainAxis.java2DToValue(p.getX(), plotArea, domainAxisEdge);
float chartY = (float) rangeAxis.java2DToValue(p.getY(), plotArea, rangeAxisEdge);
System.out.println("Chart:mouse pressed x = " + chartX + ", y = " + chartY);
float my_x = chartX; // Save the x coord of the click
float my_y = chartY; // Save the y coord of the click
if (dragging) // Exit if a drag is already in progress.
return;
int x = evt.getX(); // Location where user clicked.
int y = evt.getY();
if (x >= x2 && x < x2 + 4 && y >= y2 && y < y2 + 150) {
// It's the blue square (which should be checked first,
// since it's in front of the red square.)
dragging = true;
dragRedSquare = false;
offsetX = x - x2; // Distance from corner of square to click point.
offsetY = y - y2;
} else if (x >= x1 && x < x1 + 4 && y >= y1 && y < y1 + 150) {
// It's the red square.
dragging = true;
dragRedSquare = true;
offsetX = x - x1; // Distance from corner of square to click point.
offsetY = y - y1;
}
}
public void mouseReleased(MouseEvent evt) {
// Dragging stops when user releases the mouse button.
dragging = false;
}
public void mouseDragged(MouseEvent evt) {
// Respond when the user drags the mouse. If a square is
// not being dragged, then exit. Otherwise, change the position
// of the square that is being dragged to match the position
// of the mouse. Note that the corner of the square is place
// in the same position with respect to the mouse that it had
// when the user started dragging it.
if (dragging == false) return;
int x = evt.getX();
//int y = evt.getY();
if (dragRedSquare) { // Move the red square.
x1 = x - offsetX;
// y1 = y - offsetY;
} else { // Move the blue square.
x2 = x - offsetX;
//y2 = y - offsetY;
}
repaint();
}
public void mouseMoved(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
}
Thanks in advance.
felbabz