📄 more.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD> <TITLE>2D Text Tutorial Lesson 3: Interacting with Styled Text</TITLE> <META NAME="AUTHOR" CONTENT="Monica Pawlan of the JDC"> <META NAME="CREATED" CONTENT="09/15/98"> <META NAME="OWNER" CONTENT="Editorial/JDC"> <META NAME="revision" CONTENT="@(#)more.src 1.10 11/10/99 JDC"> <META NAME="TOPIC" CONTENT="tutorial, graphics, media, JDK 1.2, text, API"> <STYLE TYPE="text/css"> <!-- CODE {font-family: Courier, Monospace; font-size: 12pt} PRE {font-family: Courier, Monospace; font-size: 11pt} --> </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#ffffff"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%"> <TR ALIGN="CENTER" VALIGN="TOP"> <TD WIDTH="157" ALIGN="LEFT"> <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT=""> <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A> <BR> <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD> <TD> <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD ALIGN="RIGHT"> <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD WIDTH="100%"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD BGCOLOR="#CC9966" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> </TABLE> </TD> <TD ALIGN="LEFT"> <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD VALIGN="CENTER"> <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD> <TD VALIGN="CENTER"> <FONT FACE="Helvetica" SIZE="1"> <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD> <TD VALIGN="CENTER"> <INPUT TYPE="image" SRC="/images/search.button.gif" value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM> <P> <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR VALIGN="TOP"> <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD> <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152"> <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD> </TR> </TABLE> </TD> </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD> </TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR> <TD><!-- INSERT SUB-NAV INFO --> </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <DIV ALIGN="RIGHT"> <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT> <H2>2D Text Tutorial<BR> <FONT SIZE="3">Lesson 3: Interacting with Styled Text</FONT></H2> <H4><EM>By Monica Pawlan</EM></H4> <FONT SIZE="-1">[<A HREF="style.html"><<BACK</A>] [<A HREF="index.html#TOC">CONTENTS</A>] [<A HREF="int.html">NEXT>></A>]</FONT></DIV> <P>The<A HREF="http://java.sun.com/products/jdk/1.2/docs/api/index.html"><CODE>java.awt.font.TextLayout</CODE></A> class lets you respondto end user mouse clicks or keyboard inputs by displayingcursors and highlight regions in the styled text at specifiedlocations. This lesson explains how. <P>The samples in this lesson support foreign language text. In the source there is code for strong and weak carets, which appear in bidirectional foreign language text. See Lesson 4: <A HREF="int.html">Foreign Language Support</A>for more information.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#hit">Hit Testing</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#highlight">Selection Highlighting</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#caret">Moving the Caret</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="exercises.html#three">Exercises</A></FONT></UL><HR> <A NAME="hit"></A><H3>Hit Testing</H3>Hit testing is mapping a graphical location on the displayto a character position in the source text. The graphical location is often determined by end usermouse inputs or keyboard selections, and isrepresented by a caret. A <CODE>TextLayout</CODE> objectprovides default caret shapes including angled carets foritalic and oblique glyphs. If you want to use your owncaret shapes, you can retrieve the angle and position ofthe default carets from the <CODE>TextLayout</CODE> objectand use that information to draw your own shapes. <P>This sample application draws the default caret wherever theend user clicks on the <CODE>TextLayout</CODE>.The <CODE>mouseClicked</CODE> method uses<CODE>TextLayout.hitTestChar</CODE> to return a <CODE>java.awt.font.TextHitInfo</CODE> object that contains themouse click location (the insertion index)in the <CODE>TextLayout</CODE> object. <P>Information returned by the <CODE>TextLayout</CODE><A HREF="other.html#ascent"><CODE>getAscent</CODE>, <CODE>getDescent</CODE></A>, and <A HREF="other.html#advance"><CODE>getAdvance</CODE></A>methods is used to compute the location of the origin for the<CODE>TextLayout</CODE> object so it is horizontallyand vertically centered.<P>Here is the complete <A HREF="./Code/HitTestSample.java">HitTestSample.java</A> source code.<P><CENTER><IMG SRC="./Art/Caret.gif"></CENTER><P><PRE>public void paint(Graphics g) { Graphics2D graphics2D = (Graphics2D) g; Point2D origin = computeLayoutOrigin(); graphics2D.translate(origin.getX(), origin.getY()); textLayout.draw(graphics2D, 0, 0); Shape[] carets = textLayout.getCaretShapes(insertionIndex); graphics2D.setColor(STRONG_CARET_COLOR); graphics2D.draw(carets[0]); if (carets[1] != null) { graphics2D.setColor(WEAK_CARET_COLOR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -