private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) treePopup.show(e.getComponent(), e.getX(), e.getY()); }
private MouseEvent translateEvent(MouseEvent e) { Dimension stretchedDimensions = client.getStretchedDimensions(); Dimension realDimensions = client.getRealDimensions(); int newX = (int) (e.getX() / (stretchedDimensions.width / realDimensions.getWidth())); int newY = (int) (e.getY() / (stretchedDimensions.height / realDimensions.getHeight())); return new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiersEx(), newX, newY, e.getClickCount(), e.isPopupTrigger(), e.getButton()); } }
@Override public void mouseClicked(MouseEvent e) { if( SwingUtilities.isRightMouseButton(e) ){ @SuppressWarnings("unchecked") JList<PathPrinter> list = (JList<PathPrinter>)e.getSource(); int row = list.locationToIndex(e.getPoint()); list.setSelectedIndex(row); popup.show(list, e.getX(), e.getY()); } }
public void mousePressed (MouseEvent event) { movingIndex = overIndex; moveAll = event.isControlDown(); if (moveAll) { moveAllProportionally = event.isShiftDown(); moveAllPrevY = event.getY(); } }
JFrame f = new JFrame("AnimationTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); timer.start(); this.setOpaque(false); this.setPreferredSize(new Dimension(WIDE, HIGH)); this.addMouseListener(new MouseHandler()); this.addComponentListener(new ComponentHandler()); this.repaint(); JTextField field = new JTextField("test"); Dimension d = field.getPreferredSize(); field.setBounds(e.getX(), e.getY(), d.width, d.height); add(field);
@Override public void mouseMoved(MouseEvent e) { Point point = e.getPoint(); int width = getWidth(); int height = getHeight(); if (e.getX() >= xOffset && e.getY() >= yOffset) { int column = (e.getX() - xOffset) / cellWidth; int row = (e.getY() - yOffset) / cellHeight; repaint(); addMouseMotionListener(mouseHandler); protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); int width = getWidth(); int height = getHeight(); g2d.setColor(Color.BLUE); g2d.fill(cell);
addMouseListener(mouseAdapter); addMouseMotionListener(mouseAdapter); Floor2 mainPanel = new Floor2(w, h, gridDiv); g2.setColor(LINE_COLOR); for (List<Ellipse2D> ellipses : ellipseList) { Point2D p2d1 = new Point2D.Double(ellipses.get(0).getCenterX(), ellipses.get(0).getCenterY()); g.drawImage(bufImage, 0, 0, this); g2.setStroke(initStroke); for (int i = 1; i < grid; i++) { int x = i * divisionSize; g2.drawLine(x, 0, x, getSize().height); if (e.getButton() != MouseEvent.BUTTON1) { return; p1 = e.getPoint(); return; drawingLine = new Line2D.Double(p1, e.getPoint());
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); setPreferredSize(new Dimension(500, 500)); final JFrame f = new JFrame("Test"); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { drag = true; dragLocation = e.getPoint(); addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { if (drag) { if (dragLocation.getX()> getWidth()-10 && dragLocation.getY()>getHeight()-10) { System.err.println("in"); setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())), (int)(getHeight()+(e.getPoint().getY()-dragLocation.getY()))); dragLocation = e.getPoint(); f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add(this,BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); g.setColor(Color.red); g.fillRect(0, 0, getWidth(), getHeight());
addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int tabNumber = getUI().tabForCoordinate(DraggableTabbedPane.this, e.getX(), e.getY()); Image totalImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics totalGraphics = totalImage.getGraphics(); totalGraphics.setClip(bounds); setDoubleBuffered(false); paintComponent(totalGraphics); graphics.drawImage(totalImage, 0, 0, bounds.width, bounds.height, bounds.x, bounds.y, bounds.x + bounds.width, bounds.y+bounds.height, DraggableTabbedPane.this); repaint(); currentMouseLocation = e.getPoint(); repaint(); int tabNumber = getUI().tabForCoordinate(DraggableTabbedPane.this, e.getX(), 10); g.drawImage(tabImage, currentMouseLocation.x, currentMouseLocation.y, this); tabs.addTab("Four", new JButton("Four")); test.add(tabs); test.setVisible(true);
Graphics2D g2d = bImage.createGraphics(); g2d.setBackground(Color.white); g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT); g2d.dispose(); imageLabel.addMouseListener(myMouseAdapter); imageLabel.addMouseMotionListener(myMouseAdapter); imageLabel.setBorder(BorderFactory.createEtchedBorder()); Graphics2D g2 = bImage.createGraphics(); g2.setBackground(Color.white); g2.clearRect(0, 0, BI_WIDTH, BI_HEIGHT); g2.dispose(); imageLabel.repaint(); btnPanel.add(saveImageBtn); btnPanel.add(clearImageBtn); setLayout(new BorderLayout()); add(imageLabel, BorderLayout.CENTER); add(btnPanel, BorderLayout.SOUTH); pointList.add(e.getPoint()); imageLabel.repaint(); pointList.add(e.getPoint()); imageLabel.repaint();
private JFrame frame = new JFrame(); setBackground(Color.gray); label.setOpaque(true); label.setBackground(Color.RED); label.setForeground(Color.WHITE); setBackground(Color.black); setLayout(new FlowLayout(FlowLayout.RIGHT)); add(label); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { pX = me.getX(); pY = me.getY(); frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY); public void mouseDragged(MouseEvent me) { frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY);
redLabel.setOpaque(true); redLabel.setBackground(Color.red.brighter().brighter()); redLabel.setPreferredSize(LABEL_SIZE); panelGrid[4][3].add(redLabel); panelGrid[1][1].add(blueLabel); addMouseListener(myMouseAdapter); clickedPanel = (JPanel) backingPanel.getComponentAt(me.getPoint()); int x = me.getPoint().x - dragLabelWidthDiv2; int y = me.getPoint().y - dragLabelHeightDiv2; return; int x = me.getPoint().x - dragLabelWidthDiv2; int y = me.getPoint().y - dragLabelHeightDiv2; JPanel droppedPanel = (JPanel) backingPanel.getComponentAt(me.getPoint()); if (droppedPanel == null) { JFrame frame = new JFrame("DragLabelOnLayeredPane"); frame.getContentPane().add(new DragLabelOnLayeredPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
this.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 32)); this.addMouseListener(new MouseAdapter() { mousePt = e.getPoint(); repaint(); this.addMouseMotionListener(new MouseMotionAdapter() { int dx = e.getX() - mousePt.x; int dy = e.getY() - mousePt.y; textPt.setLocation(textPt.x + dx, textPt.y + dy); mousePt = e.getPoint(); repaint(); public void paintComponent(Graphics g) { super.paintComponent(g); int w2 = g.getFontMetrics().stringWidth(TITLE) / 2; g.drawString(TITLE, textPt.x - w2, textPt.y); JFrame f = new JFrame(TITLE); f.add(new MouseDragTest()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
addMouseListener(myMouseAdapter); addMouseMotionListener(myMouseAdapter); int x2 = p2.x; int y2 = p2.y; g.drawLine(x1, y1, x2, y2); p1 = p2; g.setColor(CURRENT_POINTS_COLOR); if (currentPointList != null && currentPointList.size() > 1) { Point p1 = currentPointList.get(0); public void mousePressed(MouseEvent mEvt) { currentPointList = new ArrayList<Point>(); currentPointList.add(mEvt.getPoint()); repaint(); currentPointList.add(mEvt.getPoint()); repaint(); currentPointList.add(mEvt.getPoint()); pointsList.add(currentPointList); currentPointList = null;
final JFrame frame = new JFrame(); frame.setPreferredSize(new Dimension(600, 400)); final JToolBar toolBar = new JToolBar(); button.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { popup.show(e.getComponent(), e.getX(), e.getY()); toolBar.add(button); frame.getContentPane().add(toolBar, BorderLayout.NORTH); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true);
table.addMouseListener( new MouseAdapter() if (e.isPopupTrigger()) int row = source.rowAtPoint( e.getPoint() ); int column = source.columnAtPoint( e.getPoint() ); popup.show(e.getComponent(), e.getX(), e.getY()); table.setPreferredScrollableViewportSize(table.getPreferredSize()); getContentPane().add( new JScrollPane(table) ); JPopupMenu popup = (JPopupMenu)c.getParent(); JTable table = (JTable)popup.getInvoker(); System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn()); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true );
this.setPreferredSize(new Dimension(640, 480)); this.addMouseListener(mouseHandler); this.addMouseMotionListener(mouseHandler); g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); g.drawLine(p1.x, p1.y, p2.x, p2.y); public void mousePressed(MouseEvent e) { drawing = true; p1 = e.getPoint(); p2 = p1; repaint(); public void mouseReleased(MouseEvent e) { drawing = false; p2 = e.getPoint(); public void mouseDragged(MouseEvent e) { if (drawing) { p2 = e.getPoint(); this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0)); this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA)); this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0)); JFrame f = new JFrame("LinePanel"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
private JFrame f = new JFrame("FullScreenTest"); private Action exit = new AbstractAction(EXIT) { f.dispatchEvent(new WindowEvent( f, WindowEvent.WINDOW_CLOSING)); this.add(b); f.getRootPane().setDefaultButton(b); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT); this.getActionMap().put(EXIT, exit); this.addMouseMotionListener(new MouseAdapter() { FullScreenTest.this.setToolTipText( "("+ e.getX() + "," + e.getY() + ")"); GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice dev = env.getDefaultScreenDevice(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setBackground(Color.darkGray); f.setResizable(false); f.setUndecorated(true); f.add(this); f.pack(); dev.setFullScreenWindow(f);