📄 equationdisplay.java
字号:
/**
* Copyright (c) 2006, Sun Microsystems, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package equation;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.GeneralPath;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;
public class EquationDisplay extends JComponent implements PropertyChangeListener {
private static final Color COLOR_BACKGROUND = Color.WHITE;
private static final Color COLOR_MAJOR_GRID = Color.GRAY.brighter();
private static final Color COLOR_MINOR_GRID = new Color(220, 220, 220);
private static final Color COLOR_AXIS = Color.BLACK;
private static final float STROKE_AXIS = 1.2f;
private static final float STROKE_GRID = 1.0f;
private static final float COEFF_ZOOM = 1.1f;
private List<DrawableEquation> equations;
protected double minX;
protected double maxX;
protected double minY;
protected double maxY;
private double originX;
private double originY;
private double majorX;
private int minorX;
private double majorY;
private int minorY;
private boolean drawText = true;
private Point dragStart;
private NumberFormat formatter;
private ZoomHandler zoomHandler;
private PanMotionHandler panMotionHandler;
private PanHandler panHandler;
public EquationDisplay(double originX, double originY,
double minX, double maxX,
double minY, double maxY,
double majorX, int minorX,
double majorY, int minorY) {
if (minX >= maxX) {
throw new IllegalArgumentException("minX must be < to maxX");
}
if (originX < minX || originX > maxX) {
throw new IllegalArgumentException("originX must be between minX and maxX");
}
if (minY >= maxY) {
throw new IllegalArgumentException("minY must be < to maxY");
}
if (originY < minY || originY > maxY) {
throw new IllegalArgumentException("originY must be between minY and maxY");
}
if (minorX <= 0) {
throw new IllegalArgumentException("minorX must be > 0");
}
if (minorY <= 0) {
throw new IllegalArgumentException("minorY must be > 0");
}
if (majorX <= 0.0) {
throw new IllegalArgumentException("majorX must be > 0.0");
}
if (majorY <= 0.0) {
throw new IllegalArgumentException("majorY must be > 0.0");
}
this.originX = originX;
this.originY = originY;
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
this.majorX = majorX;
this.minorX = minorX;
this.majorY = majorY;
this.minorY = minorY;
this.equations = new LinkedList<DrawableEquation>();
this.formatter = NumberFormat.getInstance();
this.formatter.setMaximumFractionDigits(2);
panHandler = new PanHandler();
addMouseListener(panHandler);
panMotionHandler = new PanMotionHandler();
addMouseMotionListener(panMotionHandler);
zoomHandler = new ZoomHandler();
addMouseWheelListener(zoomHandler);
}
@Override
public void setEnabled(boolean enabled) {
if (isEnabled() != enabled) {
//super.setEnabled(enabled);
if (enabled) {
addMouseListener(panHandler);
addMouseMotionListener(panMotionHandler);
addMouseWheelListener(zoomHandler);
} else {
removeMouseListener(panHandler);
removeMouseMotionListener(panMotionHandler);
removeMouseWheelListener(zoomHandler);
}
}
}
public boolean isDrawText() {
return drawText;
}
public void setDrawText(boolean drawText) {
this.drawText = drawText;
}
public void addEquation(AbstractEquation equation, Color color) {
if (equation != null && !equations.contains(equation)) {
equation.addPropertyChangeListener(this);
equations.add(new DrawableEquation(equation, color));
repaint();
}
}
public void removeEquation(AbstractEquation equation) {
if (equation != null) {
DrawableEquation toRemove = null;
for (DrawableEquation drawable: equations) {
if (drawable.getEquation() == equation) {
toRemove = drawable;
break;
}
}
if (toRemove != null) {
equation.removePropertyChangeListener(this);
equations.remove(toRemove);
repaint();
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void propertyChange(PropertyChangeEvent evt) {
repaint();
}
protected double yPositionToPixel(double position) {
double height = (double) getHeight();
return height - ((position - minY) * height / (maxY - minY));
}
protected double xPositionToPixel(double position) {
return (position - minX) * (double) getWidth() / (maxX - minX);
}
protected double xPixelToPosition(double pixel) {
double axisV = xPositionToPixel(originX);
return (pixel - axisV) * (maxX - minX) / (double) getWidth();
}
protected double yPixelToPosition(double pixel) {
double axisH = yPositionToPixel(originY);
return (getHeight() - pixel - axisH) * (maxY - minY) / (double) getHeight();
}
@Override
protected void paintComponent(Graphics g) {
if (!isVisible()) {
return;
}
Graphics2D g2 = (Graphics2D) g;
setupGraphics(g2);
paintBackground(g2);
drawGrid(g2);
drawAxis(g2);
drawEquations(g2);
paintInformation(g2);
}
protected void paintInformation(Graphics2D g2) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -