我尝试使用mousePressed,mouseDragged和mouseReleased在XYLineChart中进行矩形选择。 因此,当我按下按钮时,它允许我执行此操作:
private DeltaOnAreaSelected d;
d = new DeltaOnAreaSelected(this);
monChartPanel.addMouseListener(d);
monChartPanel.addMouseMotionListener(d);
现在,这里是我按下按钮的DeltaOnAreaSelected类:
public class DeltaOnAreaSelected implements MouseMotionListener, MouseListener{
private SpectrumController control;
private ChartPanel panel;
private JFreeChart chart;
private SpectrumPanel specPanel;
private SpectrumDataset dataset;
private Rectangle selection;
private Point anchor;
private Double freqMin = Double.NaN;
private Double freqMax = Double.NaN;
private Double powerMin = Double.NaN;
private Double powerMax = Double.NaN;
private Double powerPointMin = Double.NaN;
private Double powerPointMax = Double.NaN;
private Graphics2D g2d;
public DeltaOnAreaSelected(SpectrumController c){
control = c;
panel = c.getSpectrumChartPanel().getChartPanel();
chart = c.getSpectrumChartPanel().getChartPanel().getChart();
specPanel = c.getSpectrumPanel();
dataset = c.getSpectrumChartPanel().getSpectrumDataset();
}
private Double getPositionFreq(MouseEvent e){
Point2D p = panel.translateScreenToJava2D(e.getPoint());
Rectangle2D plotArea = panel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
return plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
}
private Double getPositionPower(MouseEvent e){
Point2D p = panel.translateScreenToJava2D(e.getPoint());
Rectangle2D plotArea = panel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
return plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
}
@Override
public void mousePressed(MouseEvent e) {
freqMin = getPositionFreq(e);
powerMax = getPositionPower(e);
anchor = e.getPoint();
selection = new Rectangle(anchor);
g2d = (Graphics2D)panel.getGraphics();
}
@Override
public void mouseDragged(MouseEvent e) {
selection.setBounds( (int)Math.min(anchor.x,e.getX()), (int)Math.min(anchor.y,e.getY()),
(int)Math.abs(e.getX()-anchor.x), (int)Math.abs(e.getY()-anchor.y));
if (selection!=null){
g2d.setColor(Color.red);
g2d.draw(selection);
}
panel.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
freqMax = getPositionFreq(e);
powerMin = getPositionPower(e);
selection = null;
panel.repaint();
powerPointMin = dataset.getCurveData().getMinY(freqMin, freqMax, powerMin, powerMax).getY();
powerPointMax = dataset.getCurveData().getMaxY(freqMin, freqMax, powerMin, powerMax).getY();
//Round the variables at 3 numbers
BigDecimal centerFreq = new BigDecimal((freqMax + freqMin)/2);
centerFreq = centerFreq.setScale(3, BigDecimal.ROUND_DOWN);
BigDecimal deltaFreq = new BigDecimal(freqMax-freqMin);
deltaFreq = deltaFreq.setScale(3, BigDecimal.ROUND_DOWN);
BigDecimal deltaPower = new BigDecimal(powerPointMax-powerPointMin);
deltaPower = deltaPower.setScale(3, BigDecimal.ROUND_DOWN);
//Display the delta on area selected
specPanel.getInfoPanel().getDeltaOnAreaText()
.setText("DELTA ON AREA SELECTED : \n"
+ "Center freq. (Hz) : " + centerFreq.doubleValue() + "\n"
+ "Delta freq. (Hz) : " + deltaFreq.doubleValue() + "\n"
+ "Delta power (dB) : " + deltaPower.doubleValue());
specPanel.getInfoPanel().getDeltaOnAreaText().setVisible(true);
}
@Override
public void mouseExited(MouseEvent e){
panel.removeMouseListener(this);
panel.removeMouseMotionListener(this);
}
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
}
我的问题是矩形显示为生涩,并且不会停留在释放鼠标的位置。 你有什么想法帮助我吗?
谢谢!