Documente Academic
Documente Profesional
Documente Cultură
Download Ebooks
Download JDK
Search Java Tutorials
Hide TOC
Example: ShapeMover
This applet allows the user to drag a Shape around within the applet window. The Shape is redrawn at every mouse location to provide feedback as the
user drags it.
Note: If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.
ShapeMover.java contains the complete code for this applet.
The contains method is called to determine whether the cursor is within the bounds of the rectangle when the mouse is pressed. If it is, the location
of the rectangle is updated.
public void mousePressed(MouseEvent e){
last_x = rect.x - e.getX()
last_y = rect.y - e.getY()
if(rect.contains(e.getX(),
e.getY())) updateLocation(e)
...
public void updateLocation(MouseEvent e){
rect.setLocation(last_x + e.getX(),
last_y + e.getY())
...
repaint()
You might notice that redrawing the Shape at every mouse location is slow, because the filled rectangle is rerendered every time it is moved. Using
double buffering can eliminate this problem. If you use Swing, the drawing will be double buffered automatically you don't have to change the rendering
code at all. The code for a Swing version of this program is SwingShapeMover.java.
Example: HitTestSample
This application illustrates hit testing by drawing the default caret wherever the user clicks on the TextLayout, as shown in the following figure.
Note: If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.
HitTestSample.java contains the complete code for this applet.
The mouseClicked method uses TextLayout.hitTestChar to return a java.awt.font.TextHitInfo object that contains the mouse click
location (the insertion index) in the TextLayout object.
Information returned by the TextLayout getAscent, getDescent, and getAdvance methods is used to compute the location of the origin for the
TextLayout object so it is horizontally and vertically centered.
...
private Point2D computeLayoutOrigin() {
Dimension size = getPreferredSize()
Point2D.Float origin = new Point2D.Float()
origin.x = (float) (size.width textLayout.getAdvance()) / 2
origin.y =
(float) (size.height textLayout.getDescent() +
textLayout.getAscent())/2
return origin
}
...
public void paintComponent(Graphics g) {
super.paintComponent(g)
setBackground(Color.white)
Graphics2D graphics2D = (Graphics2D) g
Point2D origin = computeLayoutOrigin()
graphics2D.translate(origin.getX(),
origin.getY())
// Draw textLayout.
textLayout.draw(graphics2D, 0, 0)
// Retrieve caret Shapes for insertionIndex.
Shape[] carets =
textLayout.getCaretShapes(insertionIndex)
// Draw the carets. carets[0] is the strong
// caret and carets[1] is the weak caret.
graphics2D.setColor(STRONG_CARET_COLOR)
graphics2D.draw(carets[0])
if (carets[1] != null) {
graphics2D.setColor(WEAK_CARET_COLOR)
graphics2D.draw(carets[1])
}
}
...
private class HitTestMouseListener
extends MouseAdapter {
/**
* Compute the character position of the
* mouse click.
*/
public void mouseClicked(MouseEvent e) {
Point2D origin = computeLayoutOrigin()
// Compute the mouse click location
// relative to textLayout's origin.
float clickX =
(float) (e.getX() - origin.getX())
float clickY =
(float) (e.getY() - origin.getY())
// Get the character position of the
// mouse click.
TextHitInfo currentHit =
textLayout.hitTestChar(clickX, clickY)
insertionIndex =
currentHit.getInsertionIndex()
// Repaint the Component so the new
// caret(s) will be displayed.
hitPane.repaint()
}
Your use of this page and all the material on pages under "The Java Tutorials" banner is subject to these legal notices.
Copyright 1995, 2015 Oracle and/or its affiliates. All rights reserved.
Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.