📄 scatter.java
字号:
if(xStart > xEnd && yStart > yEnd) g.drawRect(xEnd,yEnd,xStart-xEnd,yStart-yEnd);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3){
popup.show(p,e.getX(),e.getY());
}
}
public void mousePressed(MouseEvent e) {
// Controllo che la pressione del mouse risulti all'interno dell'area del grafico
if((e.getX() - UpperLeftGraph.x)>=0 && (e.getY() - UpperLeftGraph.y)>=0 &&
(e.getX() - UpperLeftGraph.x)<Scatter.this.WIDTH &&(e.getY() - UpperLeftGraph.y)<Scatter.this.HEIGHT){
// Start visualizzazione zoom area
if(e.getButton() == MouseEvent.BUTTON1){
xStart = e.getX();
yStart = e.getY();
zoomming = true;
}
}
}
public void mouseReleased(MouseEvent e) {
if(xStart == e.getX() && yStart == e.getY()){
zoomming = false;
return;
}
// Controllo che il mouse venga rilasciato all'interno del grafico
if((e.getX() - UpperLeftGraph.x)>=0 &&(e.getY() - UpperLeftGraph.y)>=0 &&
(e.getX()- UpperLeftGraph.x)<Scatter.this.WIDTH &&(e.getY() - UpperLeftGraph.y)<Scatter.this.HEIGHT &&
zoomming && e.getButton() == MouseEvent.BUTTON1){
xEnd = e.getX();
yEnd = e.getY();
zoomming = false;
double rangeX = xMax - xMin;
double rangeY = yMax - yMin;
if(xStart < xEnd && yStart < yEnd){
xMin = xMin + rangeX / (Scatter.this.WIDTH -2) * (xStart - UpperLeftGraph.x);
yMax = yMax - rangeY /(Scatter.this.WIDTH -2) * (yStart - UpperLeftGraph.y);
xMax = xMin + rangeX / (Scatter.this.WIDTH -2) * (xEnd - xStart);
yMin = yMax - rangeY / (Scatter.this.WIDTH -2) * (yEnd - yStart);
}
if(xStart < xEnd && yStart > yEnd){
xMin = xMin + rangeX / (Scatter.this.WIDTH -2) * (xStart - UpperLeftGraph.x);
yMax = yMax - rangeY / (Scatter.this.WIDTH -2) * (yEnd - UpperLeftGraph.y);
xMax = xMin + rangeX / (Scatter.this.WIDTH -2) * (xEnd - xStart);
yMin = yMax - rangeY / (Scatter.this.WIDTH -2) * (yStart - yEnd);
}
if(xStart > xEnd && yStart < yEnd){
xMin = xMin + rangeX / (Scatter.this.WIDTH -2) * (xEnd - UpperLeftGraph.x);
yMax = yMax - rangeY / (Scatter.this.WIDTH -2) * (yStart - UpperLeftGraph.y);
xMax = xMin + rangeX / (Scatter.this.WIDTH -2) * (xStart - xEnd);
yMin = yMax - rangeY / (Scatter.this.WIDTH -2) * (yEnd - yStart);
}
if(xStart > xEnd && yStart > yEnd){
xMin = xMin + rangeX / (Scatter.this.WIDTH -2) * (xEnd - UpperLeftGraph.x);
yMax = yMax - rangeY / (Scatter.this.WIDTH -2) * (yEnd - UpperLeftGraph.y);
xMax = xMin + rangeX / (Scatter.this.WIDTH -2) * (xStart - xEnd);
yMin = yMax - rangeY / (Scatter.this.WIDTH -2) * (yStart - yEnd);
}
first = true;
Scatter.this.repaint();
this.repaint();
return;
}else{
// Annullo lo zoom
zoomming = false;
this.repaint();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if((e.getX() - UpperLeftGraph.x)>=0 &&(e.getY() - UpperLeftGraph.y)>=0 &&
(e.getX()- UpperLeftGraph.x)<Scatter.this.WIDTH &&(e.getY() - UpperLeftGraph.y)<Scatter.this.HEIGHT &&
zoomming){
xEnd = e.getX();
yEnd = e.getY();
p.repaint();
}
}
public void mouseMoved(MouseEvent e) {
}
}
protected class ScatterPopupMenu extends JPopupMenu{
public JMenuItem restore;
public JMenuItem saveAs;
public JMenu point;
private AbstractAction commonActionSize = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Size 1")){
pointSize = 1;
}
if(e.getActionCommand().equals("Size 2")){
pointSize = 2;
}
if(e.getActionCommand().equals("Size 3")){
pointSize = 3;
}
if(e.getActionCommand().equals("Size 4")){
pointSize = 4;
}
if(e.getActionCommand().equals("Size 5")){
pointSize = 5;
}
first = true;
Scatter.this.repaint();
}
};
public ScatterPopupMenu(){
restore = new JMenuItem("Original view");
saveAs = new JMenuItem("Save as...");
point = new JMenu("Point size");
for(int i = 0 ; i < 5; i++){
JMenuItem m = new JMenuItem();
m.setAction(commonActionSize);
m.setText("Size " + Integer.toString((i+1)));
point.add(m);
}
this.add(restore);
this.addSeparator();
this.add(point);
this.addSeparator();
this.add(saveAs);
addListeners();
}
private void addListeners(){
restore.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
xMin = Scatter.this.model.getMatrix().getVariables()[xVar].getUniStats().getMinValue();
xMax = Scatter.this.model.getMatrix().getVariables()[xVar].getUniStats().getMaxValue();
yMin = Scatter.this.model.getMatrix().getVariables()[yVar].getUniStats().getMinValue();
yMax = Scatter.this.model.getMatrix().getVariables()[yVar].getUniStats().getMaxValue();
first = true;
zoomming = false;
Scatter.this.p.repaint();
Scatter.this.repaint();
}
});
saveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Scatter.this.showScreenShotDialog();
}
});
}
}
/**
* Custom file chooser class
*/
protected class PlotImagesFileChooser extends JFileChooser {
protected PlotImagesFileFilter defaultFilter;
/**
* Creates a File chooser in the appropriate directory user deafault.
* @param defaultFilter default file filter
*/
public PlotImagesFileChooser(PlotImagesFileFilter defaultFilter) {
super(new File(System.getProperty("user.dir")));
this.defaultFilter = defaultFilter;
}
/**
* Overrides default method to provide a warning if saving over an existing file
*/
public void approveSelection() {
// Gets the choosed file name
String name = getSelectedFile().getName();
String parent = getSelectedFile().getParent();
if (getDialogType() == OPEN_DIALOG) {
super.approveSelection();
}
if (getDialogType() == SAVE_DIALOG) {
PlotImagesFileFilter used = ((PlotImagesFileFilter)this.getFileFilter());
if (!name.toLowerCase().endsWith(used.getExtension())) {
name = name + used.getExtension();
setSelectedFile(new File(parent, name));
}
if (getSelectedFile().exists()) {
int resultValue = JOptionPane.showConfirmDialog(this,
"<html>File <font color=#0000ff>" + name + "</font> already exists in this folder.<br>Do you want to replace it?</html>",
"JMT - Warning",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (resultValue == JOptionPane.OK_OPTION) {
getSelectedFile().delete();
super.approveSelection();
}
} else {
super.approveSelection();
}
}
}
}
/**
* Inner class used to create simple file filters with only extension check
*/
protected class PlotImagesFileFilter extends javax.swing.filechooser.FileFilter {
private String extension, description;
/**
* Creates a new filefilter with specified extension and description
* @param extension extension of this filter (for example ".jmt")
* @param description description of this filter
*/
public PlotImagesFileFilter(String extension, String description) {
this.extension = extension;
this.description = description;
}
/**
* Whether the given file is accepted by this filter.
*/
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith(extension) || f.isDirectory();
}
/**
* The description of this filter
* @see javax.swing.filechooser.FileView#getName
*/
public String getDescription() {
return description + " (*" + extension + ")";
}
/**
* Gets extension of this filter
* @return extension of this filter
*/
public String getExtension() {
return extension;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -