public void updatePosition() { Point position = new Point(spaceOccupied.x, spaceOccupied.y); // Insert random behavior. During // each update, a sprite has about // one chance in 10 of making a // random change to its // motionVector. When a change // occurs, the motionVector // coordinate values are forced to // fall between -7 and 7. This // puts a cap on the maximum speed // for a sprite. if (rand.nextInt() % 10 == 0) { Point randomOffset = new Point(rand.nextInt() % 3, rand.nextInt() % 3); motionVector.x += randomOffset.x; if (motionVector.x >= 7) motionVector.x -= 7; if (motionVector.x <= -7) motionVector.x += 7; motionVector.y += randomOffset.y; if (motionVector.y >= 7) motionVector.y -= 7; if (motionVector.y <= -7) motionVector.y += 7; } // end if // Move the sprite on the screen position.translate(motionVector.x, motionVector.y); // Bounce off the walls boolean bounceRequired = false; Point tempMotionVector = new Point(motionVector.x, motionVector.y); // Handle walls in x-dimension if (position.x < bounds.x) { bounceRequired = true; position.x = bounds.x; // reverse direction in x tempMotionVector.x = -tempMotionVector.x; } else if ((position.x + spaceOccupied.width) > (bounds.x + bounds.width)) { bounceRequired = true; position.x = bounds.x + bounds.width - spaceOccupied.width; // reverse direction in x tempMotionVector.x = -tempMotionVector.x; } // end else if // Handle walls in y-dimension if (position.y < bounds.y) { bounceRequired = true; position.y = bounds.y; tempMotionVector.y = -tempMotionVector.y; } else if ((position.y + spaceOccupied.height) > (bounds.y + bounds.height)) { bounceRequired = true; position.y = bounds.y + bounds.height - spaceOccupied.height; tempMotionVector.y = -tempMotionVector.y; } // end else if if (bounceRequired) // save new motionVector setMotionVector(tempMotionVector); // update spaceOccupied setSpaceOccupied(position); } // end updatePosition()
/** * Internal MDI frames have offsets where a popup menu should be shown (in JDK 1.2). This method * sums up iteratively all x and y offsets of all parent compontents until the top parent * component is reached. */ private void adjustOffsets(Component comp, Point offsetPoint) { if (comp != null) { Point compLocation = comp.getLocation(); offsetPoint.translate(compLocation.x, compLocation.y); adjustOffsets(comp.getParent(), offsetPoint); } }
public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(new NimbusLookAndFeel()); Robot robot = new Robot(); robot.setAutoDelay(50); SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { createAndShowGUI(); } }); toolkit.realSync(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { Component previewPanel = Util.findSubComponent(cc, "javax.swing.colorchooser.DefaultPreviewPanel"); point = previewPanel.getLocationOnScreen(); } }); point.translate(5, 5); robot.mouseMove(point.x, point.y); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); }
public Element getLineAtPoint(MouseEvent me) { Point p = me.getLocationOnScreen(); Point pp = getLocationOnScreen(); p.translate(-pp.x, -pp.y); int pos = viewToModel(p); Element root = getDocument().getDefaultRootElement(); int e = root.getElementIndex(pos); if (e == -1) { return null; } return root.getElement(e); }
public void mousePressed(MouseEvent e) { requestFocus(); Point p = e.getPoint(); int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); p.translate(-(getWidth() / 2 - size / 2), -(getHeight() / 2 - size / 2)); if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) { // the two circular views: double radius = ((double) size) / 2.0; double x = p.getX() - size / 2.0; double y = p.getY() - size / 2.0; double r = Math.sqrt(x * x + y * y) / radius; double theta = Math.atan2(y, x) / (Math.PI * 2.0); if (r > 1) r = 1; if (mode == ColorPicker.BRI) { setHSB((float) (theta + .25f), (float) (r), bri); } else { setHSB((float) (theta + .25f), sat, (float) (r)); } } else if (mode == ColorPicker.HUE) { float s = ((float) p.x) / ((float) size); float b = ((float) p.y) / ((float) size); if (s < 0) s = 0; if (s > 1) s = 1; if (b < 0) b = 0; if (b > 1) b = 1; setHSB(hue, s, b); } else { int x2 = p.x * 255 / size; int y2 = p.y * 255 / size; if (x2 < 0) x2 = 0; if (x2 > 255) x2 = 255; if (y2 < 0) y2 = 0; if (y2 > 255) y2 = 255; if (mode == ColorPicker.RED) { setRGB(red, x2, y2); } else if (mode == ColorPicker.GREEN) { setRGB(x2, green, y2); } else { setRGB(x2, y2, blue); } } }
private static JButton getButton(JList<String> list, Point pt, int index) { Component c = list.getCellRenderer().getListCellRendererComponent(list, "", index, false, false); Rectangle r = list.getCellBounds(index, index); c.setBounds(r); // c.doLayout(); //may be needed for mone LayoutManager pt.translate(-r.x, -r.y); Component b = SwingUtilities.getDeepestComponentAt(c, pt.x, pt.y); if (b instanceof JButton) { return (JButton) b; } else { return null; } }
/** * Show the window. * * @param container - Window of JFrame to show */ private void showPopup(Window container) { if (visibleComponent.isEnabled()) { Point pt = visibleComponent.getLocationOnScreen(); pt.translate(0, visibleComponent.getHeight()); container.setLocation(pt); container.toFront(); ApplicationManager.setCurrentlySelectedField(fieldName); if (container instanceof OntologySelector) { ((OntologySelector) container).makeVisible(); } else { container.setVisible(true); container.requestFocusInWindow(); } } }
public void run() { while (running) { try { Thread.sleep(SPEED); } catch (InterruptedException e) { } synchronized (pos) { pos.translate(dx, dy); } if (pos.x < 0 || pos.x > Cwiczenie5_4.SIZE.width - SIZE) dx = -dx; if (dy > 0 && bar.bump(pos)) dy = -dy; if (pos.y < 0) dy = -dy; if (pos.y > Cwiczenie5_4.SIZE.height - SIZE) { running = false; } } }
/** Used internally */ public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension d = getSize(); super.addNotify(); if (fComponentsAdjusted) return; // Adjust components according to the insets Insets ins = getInsets(); setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height); Component components[] = getContentPane().getComponents(); for (int i = 0; i < components.length; i++) { Point p = components[i].getLocation(); p.translate(ins.left, ins.top); components[i].setLocation(p); } fComponentsAdjusted = true; }
private void dispatchEvent(MouseEvent me) { if (rect != null && rect.contains(me.getX(), me.getY())) { Point pt = me.getPoint(); pt.translate(-offset, 0); comp.setBounds(rect); comp.dispatchEvent( new MouseEvent( comp, me.getID(), me.getWhen(), me.getModifiers(), pt.x, pt.y, me.getClickCount(), me.isPopupTrigger(), me.getButton())); if (!comp.isValid()) container.repaint(); } }
public void setIntermediate(Point p, ONLGraphic c) { if (ltool.isEnabled()) { if (linkGraphic != null) { Point pnt = new Point(p); // System.out.println("LinkTool::setIntermediate (" + pnt.getX() + ", " + pnt.getY() + // ")"); if (c != null) { // System.out.println(" translated by (" + c.getScreenX()+ ", " // + c.getScreenY() + ")"); pnt.translate( (c.getScreenX() + HardwareGraphic.OFFSET), (c.getScreenY() + HardwareGraphic.D_OFFSET)); // System.out.println(" (" + pnt.getX() + ", " + pnt.getY() + // ")"); } linkGraphic.setIntermediate(pnt); linkGraphic.revalidate(); linkGraphic.repaint(); } } }
public Point mouseToScreenCoords(Point p) { Point tp = new Point(p); Insets i = getInsets(); tp.translate(-i.left, -i.top); return tp; }
// ----------------------------------------------------------------------------