Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package vteaexploration.plotgatetools; import vteaexploration.plottools.panels.XYChartPanel; import ij.IJ; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JPanel; import org.jfree.chart.plot.XYPlot; /** * * @author vinfrais */ public class graphcanvas extends JFrame { /** * Creates new form graphcanvas */ public graphcanvas() { initComponents(); final XYChartPanel cpd = new XYChartPanel(); JPanel chart = new JPanel(); chart = cpd.getChartPanel(); final DataPlotter SelectionPainter = new DataPlotter(); // SelectionPainter.addMouseListener(new MouseListener(){ // // @Override // public void mouseClicked(MouseEvent e) { // SelectionPainter.repaint(); // int x = e.getX(); // int y = e.getY(); // Point2D p = e.getPoint(); // Rectangle2D plotArea = cpd.getChartPanel().getScreenDataArea(); // XYPlot plot = (XYPlot) cpd.getChartPanel().getChart().getPlot(); // your plot // double chartX = plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge()); // double chartY = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge()); // // IJ.log("Current real x position: " + x); // IJ.log("Current real y position: " + y); // IJ.log("Current scaled x position: " + chartX); // IJ.log("Current scaled y position: " + chartY); // } // // @Override // public void mousePressed(MouseEvent e) { // // } // // @Override // public void mouseReleased(MouseEvent e) { // // } // // @Override // public void mouseEntered(MouseEvent e) { // // } // // @Override // public void mouseExited(MouseEvent e) { // // } // }); SelectionPainter.drawGraphic(); SelectionPainter.setSize(GraphicsLayered.getPreferredSize()); SelectionPainter.setLocation(0, 0); SelectionPainter.setBackground(new Color(0, 0, 0, 0)); SelectionPainter.setOpaque(true); GraphicsLayered.setOpaque(true); //SelectionPane.add(SelectionPainter); GraphicsLayered.add(SelectionPainter, 1); GraphicsLayered.add(chart, 5); this.pack(); this.setVisible(true); } class DataPlotter extends JPanel { public void drawGraphic() { repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.red); g2.drawRect(50, 50, 100, 100); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { GraphicsLayered = new javax.swing.JLayeredPane(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout GraphicsLayeredLayout = new javax.swing.GroupLayout(GraphicsLayered); GraphicsLayered.setLayout(GraphicsLayeredLayout); GraphicsLayeredLayout.setHorizontalGroup(GraphicsLayeredLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 620, Short.MAX_VALUE)); GraphicsLayeredLayout.setVerticalGroup(GraphicsLayeredLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 484, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(GraphicsLayered)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(GraphicsLayered)); pack(); }// </editor-fold>//GEN-END:initComponents /** * @param args the command line arguments */ // public static void main(String args[]) { // /* Set the Nimbus look and feel */ // //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. // * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html // */ // try { // for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { // if ("Nimbus".equals(info.getName())) { // javax.swing.UIManager.setLookAndFeel(info.getClassName()); // break; // } // } // } catch (ClassNotFoundException ex) { // java.util.logging.Logger.getLogger(graphcanvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (InstantiationException ex) { // java.util.logging.Logger.getLogger(graphcanvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (IllegalAccessException ex) { // java.util.logging.Logger.getLogger(graphcanvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (javax.swing.UnsupportedLookAndFeelException ex) { // java.util.logging.Logger.getLogger(graphcanvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } // //</editor-fold> // // /* Create and display the form */ // java.awt.EventQueue.invokeLater(new Runnable() { // public void run() { // new graphcanvas().setVisible(true); // } // }); // } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLayeredPane GraphicsLayered; // End of variables declaration//GEN-END:variables }