src/org/math/plot/canvas/PlotCanvas.java

00001 package org.math.plot.canvas;
00002 
00003 import java.awt.Color;
00004 import java.awt.Graphics;
00005 import java.awt.Graphics2D;
00006 import java.awt.Image;
00007 import java.awt.RenderingHints;
00008 import java.awt.event.ComponentEvent;
00009 import java.awt.event.ComponentListener;
00010 import java.awt.event.MouseEvent;
00011 import java.awt.event.MouseListener;
00012 import java.awt.event.MouseMotionListener;
00013 import java.awt.event.MouseWheelEvent;
00014 import java.awt.event.MouseWheelListener;
00015 import java.awt.image.BufferedImage;
00016 import java.awt.image.RenderedImage;
00017 import java.io.File;
00018 import java.io.IOException;
00019 import java.util.ArrayList;
00020 import java.util.HashMap;
00021 import java.util.LinkedList;
00022 import java.util.Set;
00023 
00024 import javax.imageio.ImageIO;
00025 import javax.swing.ImageIcon;
00026 import javax.swing.JPanel;
00027 
00028 import org.math.plot.components.DatasFrame;
00029 import org.math.plot.components.LegendPanel;
00030 import org.math.plot.components.SetScalesFrame;
00031 import org.math.plot.plotObjects.Base;
00032 import org.math.plot.plotObjects.BaseDependant;
00033 import org.math.plot.plotObjects.BasePlot;
00034 import org.math.plot.plotObjects.Plotable;
00035 import org.math.plot.plots.Plot;
00036 import org.math.plot.render.AbstractDrawer;
00037 import org.math.plot.utils.Array;
00038 
00039 import static java.lang.Math.*;
00040 
00041 import static org.math.plot.plotObjects.Base.*;
00042 
00048 public abstract class PlotCanvas extends JPanel implements MouseListener, MouseMotionListener, ComponentListener, BaseDependant, MouseWheelListener {
00049 
00050         //public int[] panelSize = new int[] { 400, 400 };
00051 
00052         public Base base;
00053 
00054         protected AbstractDrawer draw;
00055 
00056         protected BasePlot grid;
00057 
00058         public LegendPanel linkedLegendPanel;
00059 
00060         public LinkedList<Plot> plots;
00061 
00062         public LinkedList<Plotable> objects;
00063 
00064         // ///////////////////////////////////////////
00065         // ////// Constructor & inits ////////////////
00066         // ///////////////////////////////////////////
00067 
00068         public PlotCanvas() {
00069                 initPanel();
00070                 initBasenGrid();
00071                 initDrawer();
00072         }
00073 
00074         public PlotCanvas(Base b, BasePlot bp) {
00075                 initPanel();
00076                 initBasenGrid(b, bp);
00077                 initDrawer();
00078         }
00079 
00080         public PlotCanvas(double[] min, double[] max) {
00081                 initPanel();
00082                 initBasenGrid(min, max);
00083                 initDrawer();
00084         }
00085 
00086         public PlotCanvas(double[] min, double[] max, String[] axesScales, String[] axesLabels) {
00087                 initPanel();
00088                 initBasenGrid(min, max, axesScales, axesLabels);
00089                 initDrawer();
00090         }
00091 
00092         public void attachLegend(LegendPanel lp) {
00093                 linkedLegendPanel = lp;
00094         }
00095 
00096         private void initPanel() {
00097                 objects = new LinkedList<Plotable>();
00098                 plots = new LinkedList<Plot>();
00099 
00100                 setDoubleBuffered(true);
00101 
00102                 //setSize(panelSize[0], panelSize[1]);
00103                 //setPreferredSize(new Dimension(panelSize[0], panelSize[1]));
00104                 setBackground(Color.white);
00105 
00106                 addComponentListener(this);
00107                 addMouseListener(this);
00108                 addMouseMotionListener(this);
00109                 addMouseWheelListener(this);
00110         }
00111 
00112         public abstract void initDrawer();
00113 
00114         public void initBasenGrid(double[] min, double[] max, String[] axesScales, String[] axesLabels) {
00115                 base = new Base(min, max, axesScales);
00116                 grid = new BasePlot(base, axesLabels);
00117                 // grid.getAxe(0).getDarkLabel().setCorner(0.5,-1);
00118                 // grid.getAxe(1).getDarkLabel().setCorner(0,-0.5);
00119         }
00120 
00121         public abstract void initBasenGrid(double[] min, double[] max);
00122 
00123         public abstract void initBasenGrid();
00124 
00125         public void initBasenGrid(Base b, BasePlot bp) {
00126                 base = b;
00127                 grid = bp;
00128 
00129         }
00130 
00131         // ///////////////////////////////////////////
00132         // ////// set actions ////////////////////////
00133         // ///////////////////////////////////////////
00134 
00135         public void setActionMode(int am) {
00136                 ActionMode = am;
00137         }
00138 
00139         public void setNoteCoords(boolean b) {
00140                 allowNoteCoord = b;
00141         }
00142 
00143         public void setEditable(boolean b) {
00144                 allowEdit = b;
00145         }
00146 
00147         public boolean getEditable() {
00148                 return allowEdit;
00149         }
00150 
00151         public void setNotable(boolean b) {
00152                 allowNote = b;
00153         }
00154 
00155         public boolean getNotable() {
00156                 return allowNote;
00157         }
00158 
00159         // ///////////////////////////////////////////
00160         // ////// set/get elements ///////////////////
00161         // ///////////////////////////////////////////
00162 
00163         public LinkedList<Plot> getPlots() {
00164                 return plots;
00165         }
00166 
00167         public Plot getPlot(int i) {
00168                 return (Plot) plots.get(i);
00169         }
00170 
00171         public int getPlotIndex(Plot p) {
00172                 for (int i = 0; i < plots.size(); i++)
00173                         if (getPlot(i) == p)
00174                                 return i;
00175                 return -1;
00176         }
00177 
00178         public LinkedList<Plotable> getPlotables() {
00179                 return objects;
00180         }
00181 
00182         public Plotable getPlotable(int i) {
00183                 return (Plotable) objects.get(i);
00184         }
00185 
00186         public BasePlot getGrid() {
00187                 return grid;
00188         }
00189 
00190         public String[] getAxisScales() {
00191                 return base.getAxesScales();
00192         }
00193 
00194         public void setAxisLabels(String... labels) {
00195                 grid.setLegend(labels);
00196                 repaint();
00197         }
00198 
00199         public void setAxisLabel(int axe, String label) {
00200                 grid.setLegend(axe, label);
00201                 repaint();
00202         }
00203 
00204         public void setAxisScales(String... scales) {
00205                 base.setAxesScales(scales);
00206                 setAutoBounds();
00207         }
00208 
00209         public void setAxiScale(int axe, String scale) {
00210                 base.setAxesScales(axe, scale);
00211                 setAutoBounds(axe);
00212         }
00213 
00214         public void setFixedBounds(double[] min, double[] max) {
00215                 base.setFixedBounds(min, max);
00216                 resetBase();
00217                 repaint();
00218         }
00219 
00220         public void setFixedBounds(int axe, double min, double max) {
00221                 base.setFixedBounds(axe, min, max);
00222                 resetBase();
00223                 repaint();
00224         }
00225 
00226         public void includeInBounds(double... into) {
00227                 base.includeInBounds(into);
00228                 grid.resetBase();
00229                 repaint();
00230         }
00231 
00232         public void includeInBounds(Plot plot) {
00233                 base.includeInBounds(Array.min(plot.getData()));
00234                 base.includeInBounds(Array.max(plot.getData()));
00235                 resetBase();
00236                 repaint();
00237         }
00238 
00239         public void setAutoBounds() {
00240                 if (plots.size() > 0) {
00241                         Plot plot0 = this.getPlot(0);
00242                         base.setRoundBounds(Array.min(plot0.getData()), Array.max(plot0.getData()));
00243                 } else { // build default min and max bounds
00244                         double[] min = new double[base.dimension];
00245                         double[] max = new double[base.dimension];
00246                         for (int i = 0; i < base.dimension; i++) {
00247                                 if (base.getAxeScale(i).equalsIgnoreCase(LINEAR)) {
00248                                         min[i] = 0.0;
00249                                         max[i] = 1.0;
00250                                 } else if (base.getAxeScale(i).equalsIgnoreCase(LOGARITHM)) {
00251                                         min[i] = 1.0;
00252                                         max[i] = 10.0;
00253                                 }
00254                         }
00255                         base.setRoundBounds(min, max);
00256                 }
00257                 for (int i = 1; i < plots.size(); i++) {
00258                         Plot ploti = this.getPlot(i);
00259                         base.includeInBounds(Array.min(ploti.getData()));
00260                         base.includeInBounds(Array.max(ploti.getData()));
00261                 }
00262                 resetBase();
00263                 repaint();
00264         }
00265 
00266         public void setAutoBounds(int axe) {
00267                 if (plots.size() > 0) {
00268                         Plot plot0 = this.getPlot(0);
00269                         base.setRoundBounds(axe, Array.min(plot0.getData())[axe], Array.max(plot0.getData())[axe]);
00270                 } else { // build default min and max bounds
00271                         double min = 0.0;
00272                         double max = 0.0;
00273                         if (base.getAxeScale(axe).equalsIgnoreCase(LINEAR) | base.getAxeScale(axe).equalsIgnoreCase(STRINGS)) {
00274                                 min = 0.0;
00275                                 max = 1.0;
00276                         } else if (base.getAxeScale(axe).equalsIgnoreCase(LOGARITHM)) {
00277                                 min = 1.0;
00278                                 max = 10.0;
00279                         }
00280                         base.setRoundBounds(axe, min, max);
00281                 }
00282 
00283                 for (int i = 1; i < plots.size(); i++) {
00284                         Plot ploti = this.getPlot(i);
00285                         base.includeInBounds(axe, Array.min(ploti.getData())[axe]);
00286                         base.includeInBounds(axe, Array.max(ploti.getData())[axe]);
00287                 }
00288                 resetBase();
00289                 repaint();
00290         }
00291 
00292         public void resetBase() {
00293                 // System.out.println("PlotCanvas.resetBase");
00294                 draw.resetBaseProjection();
00295                 grid.resetBase();
00296 
00297                 for (int i = 0; i < objects.size(); i++) {
00298                         if (objects.get(i) instanceof BaseDependant) {
00299                                 ((BaseDependant) (objects.get(i))).resetBase();
00300                         }
00301                 }
00302                 repaint();
00303         }
00304 
00305         // ///////////////////////////////////////////
00306         // ////// add/remove elements ////////////////
00307         // ///////////////////////////////////////////
00308 
00309         public void addLabel(String text, Color c, double... where) {
00310                 addPlotable(new org.math.plot.plotObjects.Label(text, c, where));
00311         }
00312 
00313         public void addBaseLabel(String text, Color c, double... where) {
00314                 addPlotable(new org.math.plot.plotObjects.BaseLabel(text, c, where));
00315         }
00316 
00317         public void addPlotable(Plotable p) {
00318                 objects.add(p);
00319                 // resetBase();
00320                 repaint();
00321         }
00322 
00323         public void removePlotable(Plotable p) {
00324                 objects.remove(p);
00325                 repaint();
00326         }
00327 
00328         public void removePlotable(int i) {
00329                 objects.remove(i);
00330                 repaint();
00331         }
00332 
00333         public int addPlot(Plot newPlot) {
00334                 plots.add(newPlot);
00335                 if (linkedLegendPanel != null)
00336                         linkedLegendPanel.updateLegends();
00337                 if (plots.size() == 1)
00338                         setAutoBounds();
00339                 else
00340                         includeInBounds(newPlot);
00341                 return plots.size() - 1;
00342         }
00343 
00344         public void setPlot(int I, Plot p) {
00345                 plots.set(I, p);
00346                 if (linkedLegendPanel != null)
00347                         linkedLegendPanel.updateLegends();
00348                 repaint();
00349         }
00350 
00351         public void changePlotData(int I, double[]... XY) {
00352                 getPlot(I).setData(XY);
00353                 repaint();
00354         }
00355 
00356         public void changePlotName(int I, String name) {
00357                 getPlot(I).setName(name);
00358                 if (linkedLegendPanel != null)
00359                         linkedLegendPanel.updateLegends();
00360                 repaint();
00361         }
00362 
00363         public void changePlotColor(int I, Color c) {
00364                 getPlot(I).setColor(c);
00365                 if (linkedLegendPanel != null)
00366                         linkedLegendPanel.updateLegends();
00367                 repaint();
00368         }
00369 
00370         public void removePlot(int I) {
00371                 plots.remove(I);
00372                 if (linkedLegendPanel != null)
00373                         linkedLegendPanel.updateLegends();
00374                 if (plots.size() != 0) {
00375                         setAutoBounds();
00376                 }
00377 
00378         }
00379 
00380         public void removePlot(Plot p) {
00381                 plots.remove(p);
00382                 if (linkedLegendPanel != null)
00383                         linkedLegendPanel.updateLegends();
00384                 if (plots.size() != 0) {
00385                         setAutoBounds();
00386                 }
00387 
00388         }
00389 
00390         public void removeAllPlots() {
00391                 plots.clear();
00392                 if (linkedLegendPanel != null)
00393                         linkedLegendPanel.updateLegends();
00394                 clearNotes();
00395         }
00396 
00397         public void addVectortoPlot(int numPlot, double[][] v) {
00398                 getPlot(numPlot).addVector(v);
00399         }
00400 
00401         /*public void addQuantiletoPlot(int numPlot, boolean _symetric, double[]... q) {
00402                 getPlot(numPlot).addQuantiles(q, _symetric);
00403         }*/
00404 
00405         public void addQuantiletoPlot(int numPlot, int numAxe, double rate, boolean symetric, double[] q) {
00406                 getPlot(numPlot).addQuantile(numAxe, rate, q, symetric);
00407         }
00408 
00409         public void addQuantiletoPlot(int numPlot, int numAxe, double rate, boolean symetric, double q) {
00410                 getPlot(numPlot).addQuantile(numAxe, rate, q, symetric);
00411         }
00412 
00413         public void addQuantilestoPlot(int numPlot, int numAxe, double[][] q) {
00414                 getPlot(numPlot).addQuantiles(numAxe, q);
00415         }
00416 
00417         public void addQuantilestoPlot(int numPlot, int numAxe, double[] q) {
00418                 getPlot(numPlot).addQuantiles(numAxe, q);
00419         }
00420 
00421         public void addGaussQuantilestoPlot(int numPlot, int numAxe, double[] s) {
00422                 getPlot(numPlot).addGaussQuantiles(numAxe, s);
00423         }
00424 
00425         public void addGaussQuantilestoPlot(int numPlot, int numAxe, double s) {
00426                 getPlot(numPlot).addGaussQuantiles(numAxe, s);
00427         }
00428 
00429         // ///////////////////////////////////////////
00430         // ////// call for toolbar actions ///////////
00431         // ///////////////////////////////////////////
00432 
00433         public void toGraphicFile(File file) throws IOException {
00434 
00435                 Image image = createImage(getWidth(), getHeight());
00436                 paint(image.getGraphics());
00437                 image = new ImageIcon(image).getImage();
00438 
00439                 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
00440                 Graphics g = bufferedImage.createGraphics();
00441                 g.drawImage(image, 0, 0, Color.WHITE, null);
00442                 g.dispose();
00443 
00444                 try {
00445                         ImageIO.write((RenderedImage) bufferedImage, "PNG", file);
00446                 } catch (IllegalArgumentException ex) {
00447                 }
00448         }
00449 
00450         public void displaySetScalesFrame() {
00451                 new SetScalesFrame(this);
00452         }
00453 
00454         public void displayDatasFrame(int i) {
00455                 DatasFrame df = new DatasFrame(this, linkedLegendPanel);
00456                 df.panels.setSelectedIndex(i);
00457         }
00458 
00459         public void displayDatasFrame() {
00460                 displayDatasFrame(0);
00461         }
00462 
00463         boolean mapset = false;
00464 
00465         public void resetMapData() {
00466                 for (int i = 0; i < grid.getAxis().length; i++) {
00467                         grid.getAxis()[i].setStringMap(null);
00468                         setAxiScale(i, Base.LINEAR);
00469                 }
00470                 mapset = false;
00471         }
00472 
00473         public double[][] mapData(Object[][] data) {
00474                 //System.out.println("mapData:" + Array.cat(data));
00475 
00476                 double[][] mapeddata = new double[data.length][data[0].length];
00477 
00478                 if (!mapset) {
00479                         for (int j = 0; j < data[0].length; j++) {
00480                                 if (!Array.isDouble(data[0][j].toString())) {
00481                                         //System.out.println(data[0][j].toString() + " is not a double");
00482                                         setAxiScale(j, Base.STRINGS);
00483 
00484                                         ArrayList<String> string_array_j = new ArrayList<String>(data.length);
00485                                         for (int i = 0; i < data.length; i++)
00486                                                 string_array_j.add(data[i][j].toString());
00487 
00488                                         grid.getAxis(j).setStringMap(Array.mapStringArray(string_array_j));
00489                                         grid.getAxis(j).init();
00490 
00491                                         for (int i = 0; i < data.length; i++)
00492                                                 mapeddata[i][j] = grid.getAxis(j).getStringMap().get(data[i][j].toString());
00493 
00494                                         //System.out.println("Axe " + j + ":" + Array.toString(grid.getAxe(j).getStringMap()));
00495                                         initReverseMap(j);
00496                                 } else {
00497                                         //System.out.println(data[0][j].toString() + " is a double");
00498                                         //System.out.println("Axe " + j + ": double[]");
00499                                         for (int i = 0; i < data.length; i++)
00500                                                 mapeddata[i][j] = Double.valueOf(data[i][j].toString());
00501                                 }
00502                         }
00503                         mapset = true;
00504                 } else {
00505                         for (int j = 0; j < data[0].length; j++)
00506                                 if (!Array.isDouble(data[0][j].toString())) {
00507                                         //System.out.println(data[0][j].toString() + " is not a double");
00508                                         if (base.getAxeScale(j).equals(Base.STRINGS)) {
00509                                                 for (int i = 0; i < data.length; i++) {
00510                                                         if (!grid.getAxis(j).getStringMap().containsKey(data[i][j].toString())) {
00511                                                                 Set<String> s = grid.getAxis(j).getStringMap().keySet();
00512                                                                 ArrayList<String> string_array_j = new ArrayList<String>(s.size() + 1);
00513                                                                 string_array_j.addAll(s);
00514                                                                 string_array_j.add(data[i][j].toString());
00515                                                                 grid.getAxis(j).setStringMap(Array.mapStringArray(string_array_j));
00516 
00517                                                                 //System.out.println("Axe " + j + ":" + Array.toString(grid.getAxe(j).getStringMap()));
00518                                                                 initReverseMap(j);
00519                                                         }
00520                                                         mapeddata[i][j] = grid.getAxis(j).getStringMap().get(data[i][j].toString());
00521                                                 }
00522                                         } else {
00523                                                 throw new IllegalArgumentException("The mapping of this PlotPanel was not set on axis " + j);
00524                                         }
00525                                 } else {
00526                                         //System.out.println(data[0][j].toString() + " is a double");
00527                                         //System.out.println("Axe " + j + ": double[]");
00528                                         for (int i = 0; i < data.length; i++)
00529                                                 mapeddata[i][j] = Double.valueOf(data[i][j].toString());
00530                                 }
00531                 }
00532                 return mapeddata;
00533         }
00534 
00535         public Object[][] reverseMapedData(double[][] mapeddata) {
00536                 Object[][] stringdata = new Object[mapeddata.length][mapeddata[0].length];
00537 
00538                 for (int i = 0; i < mapeddata.length; i++)
00539                         stringdata[i] = reverseMapedData(mapeddata[i]);
00540 
00541                 return stringdata;
00542         }
00543 
00544         public Object[] reverseMapedData(double[] mapeddata) {
00545                 Object[] stringdata = new Object[mapeddata.length];
00546 
00547                 if (reversedMaps == null)
00548                         reversedMaps = new HashMap[grid.getAxis().length];
00549 
00550                 for (int j = 0; j < mapeddata.length; j++)
00551                         if (reversedMaps[j] != null)
00552                                 stringdata[j] = reversedMaps[j].get((Double) (mapeddata[j]));
00553                         else
00554                                 stringdata[j] = (Double) (mapeddata[j]);
00555 
00556                 return stringdata;
00557         }
00558 
00559         HashMap<Double, String>[] reversedMaps;
00560 
00561         private void initReverseMap(int j) {
00562                 if (reversedMaps == null)
00563                         reversedMaps = new HashMap[grid.getAxis().length];
00564 
00565                 if (grid.getAxis(j) != null)
00566                         reversedMaps[j] = Array.reverseStringMap(grid.getAxis(j).getStringMap());
00567         }
00568 
00569         // ///////////////////////////////////////////
00570         // ////// Paint method ///////////////////////
00571         // ///////////////////////////////////////////
00572 
00573         // anti-aliasing constant
00574         final protected static RenderingHints AALIAS = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
00575 
00576         public static Color NOTE_COLOR = Color.BLACK;
00577 
00578         public static Color EDIT_COLOR = Color.BLACK;
00579 
00580         public boolean allowEdit = true;
00581 
00582         public boolean allowNote = true;
00583 
00584         public boolean allowNoteCoord = true;
00585 
00586         protected double[] coordNoted;
00587 
00588         public void paint(Graphics gcomp) {
00589                 // System.out.println("PlotCanvas.paint");
00590 
00591                 Graphics2D gcomp2D = (Graphics2D) gcomp;
00592 
00593                 // anti-aliasing methods
00594                 gcomp2D.addRenderingHints(AALIAS);
00595                 gcomp2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
00596 
00597                 gcomp2D.setColor(getBackground());
00598                 gcomp2D.fillRect(0, 0, getSize().width, getSize().height);
00599 
00600                 draw.initGraphics(gcomp2D);
00601 
00602                 // draw plot
00603                 grid.plot(draw);
00604 
00605                 for (int i = 0; i < plots.size(); i++) {
00606                         getPlot(i).plot(draw);
00607                         if (linkedLegendPanel != null)
00608                                 linkedLegendPanel.nonote(i);
00609                 }
00610 
00611                 for (int i = 0; i < objects.size(); i++)
00612                         getPlotable(i).plot(draw);
00613 
00614                 // draw note
00615                 if (allowNote) {
00616                         if (allowNoteCoord && coordNoted != null) {
00617                                 draw.setColor(NOTE_COLOR);
00618                                 draw.drawCoordinate(coordNoted);
00619                                 draw.drawText(Array.cat(reverseMapedData(coordNoted)), coordNoted);
00620                         }
00621                         for (int i = 0; i < plots.size(); i++) {
00622                                 if (getPlot(i).noted) {
00623                                         if (linkedLegendPanel != null)
00624                                                 linkedLegendPanel.note(i);
00625                                         getPlot(i).note(draw);
00626                                         //return;
00627                                 }
00628                         }
00629                 }
00630         }
00631 
00632         // ///////////////////////////////////////////
00633         // ////// Listeners //////////////////////////
00634         // ///////////////////////////////////////////
00635 
00636         public final static int ZOOM = 0;
00637 
00638         public final static int TRANSLATION = 1;
00639 
00640         public int ActionMode;
00641 
00642         protected boolean dragging = false;
00643 
00644         protected int[] mouseCurent = new int[2];
00645 
00646         protected int[] mouseClick = new int[2];
00647 
00648         public void clearNotes() {
00649                 coordNoted = null;
00650                 repaint();
00651         }
00652 
00653         public void mousePressed(MouseEvent e) {
00654                 //System.out.println("PlotCanvas.mousePressed");
00655                 /*
00656                  * System.out.println("PlotCanvas.mousePressed"); System.out.println("
00657                  * mouseClick = [" + mouseClick[0] + " " + mouseClick[1] + "]");
00658                  * System.out.println(" mouseCurent = [" + mouseCurent[0] + " " +
00659                  * mouseCurent[1] + "]");
00660                  */
00661                 mouseCurent[0] = e.getX();
00662                 mouseCurent[1] = e.getY();
00663                 e.consume();
00664                 mouseClick[0] = mouseCurent[0];
00665                 mouseClick[1] = mouseCurent[1];
00666         }
00667 
00668         public void mouseDragged(MouseEvent e) {
00669                 //System.out.println("PlotCanvas.mouseDragged");
00670 
00671                 dragging = true;
00672                 /*
00673                  * System.out.println("PlotCanvas.mouseDragged"); System.out.println("
00674                  * mouseClick = [" + mouseClick[0] + " " + mouseClick[1] + "]");
00675                  * System.out.println(" mouseCurent = [" + mouseCurent[0] + " " +
00676                  * mouseCurent[1] + "]");
00677                  */
00678                 mouseCurent[0] = e.getX();
00679                 mouseCurent[1] = e.getY();
00680                 e.consume();
00681                 switch (ActionMode) {
00682                 case TRANSLATION:
00683                         draw.translate(mouseCurent[0] - mouseClick[0], mouseCurent[1] - mouseClick[1]);
00684                         mouseClick[0] = mouseCurent[0];
00685                         mouseClick[1] = mouseCurent[1];
00686                         repaint();
00687                         break;
00688                 case ZOOM:
00689                         repaint();
00690                         Graphics gcomp = getGraphics();
00691                         gcomp.setColor(Color.black);
00692                         gcomp.drawRect(min(mouseClick[0], mouseCurent[0]), min(mouseClick[1], mouseCurent[1]), abs(mouseCurent[0] - mouseClick[0]), abs(mouseCurent[1]
00693                                         - mouseClick[1]));
00694                         break;
00695                 }
00696                 //repaint();
00697         }
00698 
00699         public void mouseReleased(MouseEvent e) {
00700                 //System.out.println("PlotCanvas.mouseReleased");
00701 
00702                 /*
00703                  * System.out.println("PlotCanvas.mouseReleased"); System.out.println("
00704                  * mouseClick = [" + mouseClick[0] + " " + mouseClick[1] + "]");
00705                  * System.out.println(" mouseCurent = [" + mouseCurent[0] + " " +
00706                  * mouseCurent[1] + "]");
00707                  */
00708                 mouseCurent[0] = e.getX();
00709                 mouseCurent[1] = e.getY();
00710                 e.consume();
00711                 switch (ActionMode) {
00712                 case ZOOM:
00713                         if (abs(mouseCurent[0] - mouseClick[0]) > 10 && abs(mouseCurent[1] - mouseClick[1]) > 10) {
00714                                 int[] origin = { min(mouseClick[0], mouseCurent[0]), min(mouseClick[1], mouseCurent[1]) };
00715                                 double[] ratio = { abs((double) (mouseCurent[0] - mouseClick[0]) / (double) getWidth()),
00716                                                 abs((double) (mouseCurent[1] - mouseClick[1]) / (double) getHeight()) };
00717                                 draw.dilate(origin, ratio);
00718                                 repaint();
00719                         }
00720                         break;
00721                 }
00722                 //repaint();
00723         }
00724 
00725         public void mouseClicked(MouseEvent e) {
00726                 //System.out.println("PlotCanvas.mouseClicked");
00727 
00728                 /*
00729                  * System.out.println("PlotCanvas.mouseClicked"); System.out.println("
00730                  * mouseClick = [" + mouseClick[0] + " " + mouseClick[1] + "]");
00731                  * System.out.println(" mouseCurent = [" + mouseCurent[0] + " " +
00732                  * mouseCurent[1] + "]");
00733                  */
00734                 mouseCurent[0] = e.getX();
00735                 mouseCurent[1] = e.getY();
00736                 e.consume();
00737                 mouseClick[0] = mouseCurent[0];
00738                 mouseClick[1] = mouseCurent[1];
00739 
00740                 if (allowEdit) {
00741                         if (e.getModifiers() == MouseEvent.BUTTON1_MASK && e.getClickCount() > 1) {
00742                                 for (int i = 0; i < grid.getAxis().length; i++)
00743                                         if (grid.getAxis(i).isSelected(mouseClick, draw) != null) {
00744                                                 grid.getAxis(i).edit(this);
00745                                                 return;
00746                                         }
00747 
00748                                 for (int i = 0; i < plots.size(); i++)
00749                                         if (getPlot(i).isSelected(mouseClick, draw) != null) {
00750                                                 getPlot(i).edit(this);
00751                                                 return;
00752                                         }
00753                         }
00754                 }
00755 
00756                 if (!dragging && allowNote) {
00757                         for (int i = 0; i < plots.size(); i++) {
00758                                 double[] _coordNoted = getPlot(i).isSelected(mouseClick, draw);
00759                                 if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
00760                                         if (_coordNoted != null) {
00761                                                 getPlot(i).noted = !getPlot(i).noted;
00762                                         } else {
00763                                                 getPlot(i).noted = false;
00764                                         }
00765                                 } else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
00766                                         if (_coordNoted != null) {
00767                                                 if (coordNoted != null) {
00768                                                         boolean alreadyNoted = true;
00769                                                         for (int j = 0; j < _coordNoted.length; j++)
00770                                                                 alreadyNoted = alreadyNoted && _coordNoted[j] == coordNoted[j];
00771                                                         if (alreadyNoted) {
00772                                                                 coordNoted = null;
00773                                                         } else {
00774                                                                 coordNoted = _coordNoted;
00775                                                         }
00776                                                 } else {
00777                                                         coordNoted = _coordNoted;
00778                                                 }
00779                                         }
00780                                 }
00781                         }
00782                         repaint();
00783                 } else
00784                         dragging = false;
00785         }
00786 
00787         public void mouseEntered(MouseEvent e) {
00788         }
00789 
00790         public void mouseExited(MouseEvent e) {
00791         }
00792 
00793         public void mouseMoved(MouseEvent e) {
00794                 //System.out.println("PlotCanvas.mouseMoved");
00795                 /*
00796                  * System.out.println("PlotCanvas.mouseClicked"); System.out.println("
00797                  * mouseClick = [" + mouseClick[0] + " " + mouseClick[1] + "]");
00798                  * System.out.println(" mouseCurent = [" + mouseCurent[0] + " " +
00799                  * mouseCurent[1] + "]");
00800                  */
00801                 /*mouseCurent[0] = e.getX();
00802                 mouseCurent[1] = e.getY();
00803                 e.consume();
00804                 if (allowNote) {
00805                         for (int i = 0; i < plots.size(); i++) {
00806                                 double[] _coordNoted = getPlot(i).isSelected(mouseCurent, draw);
00807                                 if (_coordNoted != null) {
00808                                         getPlot(i).noted = !getPlot(i).noted;
00809                                 } else {
00810                                         getPlot(i).noted = false;
00811                                 }
00812                         }
00813                         repaint();
00814                 }*/
00815         }
00816 
00817         public void mouseWheelMoved(MouseWheelEvent e) {
00818                 //System.out.println("PlotCanvas.mouseWheelMoved");
00819                 /*
00820                  * System.out.println("PlotCanvas.mouseWheelMoved");
00821                  * System.out.println(" mouseClick = [" + mouseClick[0] + " " +
00822                  * mouseClick[1] + "]"); System.out.println(" mouseCurent = [" +
00823                  * mouseCurent[0] + " " + mouseCurent[1] + "]");
00824                  */
00825                 mouseCurent[0] = e.getX();
00826                 mouseCurent[1] = e.getY();
00827                 e.consume();
00828                 int[] origin;
00829                 double[] ratio;
00830                 // double factor = 1.5;
00831                 switch (ActionMode) {
00832                 case ZOOM:
00833                         if (e.getWheelRotation() == -1) {
00834                                 origin = new int[] { (int) (mouseCurent[0] - getWidth() / 3/* (2*factor) */), (int) (mouseCurent[1] - getHeight() / 3/* (2*factor) */) };
00835                                 ratio = new double[] { 0.666/* 1/factor, 1/factor */, 0.666 };
00836                         } else {
00837                                 origin = new int[] { (int) (mouseCurent[0] - getWidth() / 1.333/* (2/factor) */),
00838                                                 (int) (mouseCurent[1] - getHeight() / 1.333/* (2/factor) */) };
00839                                 ratio = new double[] { 1.5, 1.5 /* factor, factor */};
00840                         }
00841                         draw.dilate(origin, ratio);
00842                         repaint();
00843                         break;
00844                 }
00845         }
00846 
00847         public void componentHidden(ComponentEvent e) {
00848         }
00849 
00850         public void componentMoved(ComponentEvent e) {
00851         }
00852 
00853         public void componentResized(ComponentEvent e) {
00854                 //System.out.println("PlotCanvas.componentResized");
00855                 //panelSize = new int[] { (int) (getSize().getWidth()), (int) (getSize().getHeight()) };
00856                 draw.resetBaseProjection();
00857                 //System.out.println("PlotCanvas : "+panelSize[0]+" x "+panelSize[1]);
00858                 repaint();
00859                 linkedLegendPanel.componentResized(e);
00860         }
00861 
00862         public void componentShown(ComponentEvent e) {
00863         }
00864 
00865 }

Generated on Wed Sep 5 21:44:01 2007 for jmathplot by  doxygen 1.5.1