@Override public void mousePressed(MouseEvent e) { int mousePromille = (int) (((e.getX() * 1.0) / getWidth()) * 1000); selectedBlock = null; selectedThreshold = null; dragMin = 0 + thresholdMinimumSpacing; dragMax = 1000 - thresholdMinimumSpacing; for (int i = 0; i < thresholds.size(); i++) { Threshold t = thresholds.get(i); if (Math.abs(mousePromille - t.getValuePromille()) < thresholdSelectionWidth) { selectedThreshold = t; if (i < thresholds.size() - 1) dragMax = thresholds.get(i + 1).getValuePromille() - thresholdMinimumSpacing; thresholdPressed(t); repaint(); return; } dragMin = t.getValuePromille() + thresholdMinimumSpacing; } for (Block b : blocks) { int lowerP = (b.getLowerThreshold() == null) ? 0 : b.getLowerThreshold().getValuePromille(); int upperP = (b.getUpperThreshold() == null) ? 1000 : b.getUpperThreshold().getValuePromille(); if (mousePromille >= lowerP && mousePromille < upperP) { selectedBlock = b; blockPressed(b); repaint(); return; } } }
@Override public void mouseMoved(MouseEvent e) { int mousePromille = (int) (((e.getX() * 1.0) / getWidth()) * 1000); boolean found = false; for (Threshold t : thresholds) { if (Math.abs(mousePromille - t.getValuePromille()) < thresholdSelectionWidth) { setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); found = true; } } if (!found) setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); }
private void drawThresholds(Graphics2D g2) { Stroke defaultStroke = g2.getStroke(); int prevUpperEnd = 0; int prevUpperCount = 0; int prevX = 0; int prevLowerCount = 0; for (int i = 0; i < thresholds.size(); i++) { Threshold threshold = thresholds.get(i); int x = (int) ((threshold.getValuePromille() * 1.0 / (1000)) * getWidth()); if (threshold == selectedThreshold) { g2.setStroke(selectedThresholdStroke); g2.setColor(Color.blue); } else { g2.setStroke(thresholdStroke); g2.setColor(Color.black); } g2.drawLine(x, 0, x, getHeight()); FontMetrics fm = getFontMetrics(getFont()); g2.setColor(Color.GRAY); String upName = ""; if (thresholds.get(i).getUpCommand() != Command.getNothingCommand()) upName = thresholds.get(i).getUpCommand().getName(); int y = 15; if (prevUpperEnd >= x) y += 10 * prevUpperCount; else prevUpperCount = 0; prevUpperEnd = x + fm.stringWidth(upName); prevUpperCount++; g2.drawString(upName, x + 5, y); String downName = ""; if (thresholds.get(i).getDownCommand() != Command.getNothingCommand()) downName = thresholds.get(i).getDownCommand().getName(); y = getHeight() - 5; if (prevX >= x - fm.stringWidth(downName)) y -= 10 * prevLowerCount; else prevLowerCount = 0; prevX = x; prevLowerCount++; int width = fm.stringWidth(downName); g2.drawString(downName, (x - 5) - width, y); } g2.setStroke(defaultStroke); }
// <editor-fold desc="Mouse events"> @Override public void mouseDragged(MouseEvent e) { try { Robot rbt = new Robot(); int mousePromille = (int) (((e.getX() * 1.0) / getWidth()) * 1000); if (selectedThreshold != null) { if (mousePromille < dragMin) { rbt.mouseMove(e.getXOnScreen() + 1, e.getYOnScreen()); return; } if (mousePromille > dragMax) { rbt.mouseMove(e.getXOnScreen() - 1, e.getYOnScreen()); return; } selectedThreshold.setValuePromille((int) (((e.getX() * 1.0) / getWidth()) * 1000)); repaint(); } } catch (AWTException ex) { Logger.getLogger(ChannelSettingViewer.class.getName()).log(Level.SEVERE, null, ex); } }