In this code points can be moved along a vertical line:
| Код | import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.text.NumberFormat; import javax.swing.*; public class Translation extends JPanel { JLabel xLabel; JLabel yLabel; NumberFormat nf; double[] data = { 16.0, 32.9, 7.4, 18.9, 12.3 }; final int Y_GRADS = 5; final int PAD = 35; final int SPAD = 3; final int TICK = 2; boolean caught = false; double maxVal; int pointChanged = -1; public Translation() { nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(1); nf.setMaximumFractionDigits(1); maxVal = getMaxValue(); addMouseMotionListener(sweep); addMouseMotionListener(myMouseMotionListener); addMouseListener(myMouseListener); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawAxes(g2); plotData(g2); } private void plotData(Graphics2D g2) { for(int j = 0; j < data.length; j++) { Point2D.Double p = modelToView(j, data[j]); if(j < data.length-1) { Point2D.Double next = modelToView(j+1, data[j+1]); g2.setPaint(Color.blue); g2.draw(new Line2D.Double(p, next)); } g2.setPaint(Color.red); g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4)); } } private Point2D.Double modelToView(double x, double y) { double h = getHeight(); Point2D.Double p = new Point2D.Double(); p.x = PAD + x*(getWidth() - 2*PAD)/(data.length-1); p.y = h-PAD - y*(h - 2*PAD)/maxVal; return p; } public Point2D.Double viewToModel(double x, double y) { double h = getHeight(); Point2D.Double p = new Point2D.Double(); p.x = (x - PAD) * (data.length-1) / (getWidth() - 2*PAD); p.y = (h - PAD - y) * maxVal / (h - 2*PAD); return p; } private void drawAxes(Graphics2D g2) { int w = getWidth(); int h = getHeight(); double xInc = (double)(w - 2*PAD)/(data.length-1); double yInc = (double)(h - 2*PAD)/Y_GRADS; // Abcissa g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); // Tick marks double y1 = h-PAD, y2 = y1+TICK; for(int j = 0; j <= data.length; j++) { double x = PAD + j*xInc; g2.draw(new Line2D.Double(x, y1, x, y2)); } // Labels Font font = g2.getFont().deriveFont(14f); g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); LineMetrics lm = font.getLineMetrics("0", frc); float sx, sy = h - PAD + SPAD + lm.getAscent(); for(int j = 0; j < data.length; j++) { String s = String.valueOf(j); float width = (float)font.getStringBounds(s, frc).getWidth(); sx = (float)(PAD + j*xInc - width/2); g2.drawString(s, sx, sy); } // Ordinate g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // Tick marks double x1 = PAD, x2 = PAD-TICK; for(int j = 0; j <= Y_GRADS; j++) { double y = PAD + j*yInc; g2.draw(new Line2D.Double(x1, y, x2, y)); } // Labels for(int j = 0; j <= data.length; j++) { String s = nf.format(maxVal*(1.0 - (double)j/data.length)); float width = (float)font.getStringBounds(s, frc).getWidth(); sx = (float)(PAD - SPAD - width); sy = (float)(PAD + j*yInc + lm.getAscent()/3); g2.drawString(s, sx, sy); } } private double getMaxValue() { double max = -Double.MAX_VALUE; for(int j = 0; j < data.length; j++) { if(data[j] > max) { max = data[j]; } } return max; } MouseMotionListener sweep = new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { Point2D.Double p = viewToModel(e.getX(), e.getY()); xLabel.setText(nf.format(p.x)); yLabel.setText(nf.format(p.y)); } }; MouseMotionListener myMouseMotionListener = new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { //checkForCaught(e); } }; private void checkForCaught(MouseEvent e) { Point2D.Double p = viewToModel(e.getX(), e.getY()); System.out.println(p.x+" "+p.y); double dx = 0.5;//further calculateDx(); double dy = 0.5;// for (int i=0;i<data.length;i++) { if ( (p.x>(i-dx)) && (p.x<(i+dx)) && (p.y>(data[i]-dy))&& (p.y<(data[i]+dy))) { System.out.println("Caught"); caught = true; pointChanged = i; } } } MouseListener myMouseListener = new MouseAdapter() { public void mouseReleased(MouseEvent e) { Point2D.Double p = viewToModel(e.getX(), e.getY()); if (caught) { data[pointChanged] = p.y; caught = false; System.out.println("data changed"); redraw(); } } public void mousePressed(MouseEvent e) { System.out.println("Pressed"); checkForCaught(e); } }; public void redraw() { paintComponent(this.getGraphics()); } private JPanel getLast() { xLabel = new JLabel(); yLabel = new JLabel(); Dimension d = new Dimension(45, 25); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); addComponents(new JLabel("x ="), xLabel, panel, gbc, d, true); addComponents(new JLabel("y ="), yLabel, panel, gbc, d, false); return panel; } private void addComponents(JComponent c1, JComponent c2, Container c, GridBagConstraints gbc, Dimension d, boolean b) { gbc.weightx = b ? 1.0 : 0; gbc.anchor = GridBagConstraints.EAST; c.add(c1, gbc); c2.setPreferredSize(d); gbc.weightx = b ? 0 : 1.0; gbc.anchor = GridBagConstraints.WEST; c.add(c2, gbc); } public static void main(String[] args) { Translation test = new Translation(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.getContentPane().add(test.getLast(), "Last"); f.setSize(500,400); f.setLocation(200,200); f.setVisible(true); } }
|
|