⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0149.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Font.BOLD</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Font.ITALIC</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
&#8211; there&#8217;s no support for underline, strikethrough, etc.), and point
size. It also simplifies the calculation of the width and height of a string.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you press the &#8220;Print text&#8221; button, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TBL
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">listener
is activated. You can see that it goes through two iterations of creating a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ChangeFont</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and calling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>drawString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to print out the string in a calculated position, centered, one-third, and
two-thirds down the page, respectively. Notice whether these calculations
produce the expected results. (They didn&#8217;t with the version I used.)
</FONT><P></DIV>
<A NAME="Heading451"></A><H4 ALIGN=LEFT>
Printing
graphics
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you press the &#8220;Print graphics&#8221; button the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>GBL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
listener is activated. The creation of a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintData</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object initializes 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>g</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and then you simply call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>print(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the component you want to print. To force printing you must call <A NAME="Index2130"></A><A NAME="Index2131"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>dispose(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the <A NAME="Index2132"></A><A NAME="Index2133"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and <A NAME="Index2134"></A><A NAME="Index2135"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>end(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintData</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object (which turns around and calls 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>end(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintJob</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
work is going on inside the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. You can see that the base-class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is simple &#8211; it extends 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and contains an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>rings</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to indicate how many concentric rings to draw on this particular 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The three derived classes show different approaches to accomplishing the same
goal: drawing on both the screen and on the printed page.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot1
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">takes
the simplest approach to coding: ignore the fact that there are differences in
painting and printing, and just override <A NAME="Index2136"></A><A NAME="Index2137"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The reason this works is that the default
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
<A NAME="Index2138"></A><A NAME="Index2139"></A>print(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method simply turns around and calls 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, you&#8217;ll notice that the size of the output depends on the size of
the on-screen canvas, which makes sense since the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>width</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>height</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are determined by calling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Canvas.getSize(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The other situation in which this is acceptable is if your image is always a
fixed size.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
the size of the drawing surface is important, then you must discover the
dimensions. Unfortunately, this turns out to be awkward, as you can see in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
For some possibly good reason that I don&#8217;t know, you cannot simply ask the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object the dimensions of its drawing surface. This would have made the whole
process quite elegant. Instead, to see if you&#8217;re printing rather than
painting, you must detect the <A NAME="Index2140"></A><A NAME="Index2141"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintGraphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
using the RTTI 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword (described in Chapter 11), then downcast and call the sole 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintGraphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method: <A NAME="Index2142"></A><A NAME="Index2143"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getPrintJob(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Now you have a handle to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintJob</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and you can find out the width and height of the paper. This is a hacky
approach, but perhaps there is some rational reason for it. (On the other hand,
you&#8217;ve seen some of the other library designs by now so you might get the
impression that the designers were, in fact, just hacking around...)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>paint(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot2
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">goes
through both possibilities of printing or painting. But since the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>print(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method should be called when printing, why not use that? This approach is used
in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and it eliminates the need to use 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>instanceof</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
since inside 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>print(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you can assume that you can cast to a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintGraphics</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. This is a little better. The situation is improved by placing the
common drawing code (once the dimensions have been detected) inside a separate
method 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>doGraphics(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
<A NAME="Heading452"></A><H4 ALIGN=LEFT>
Running
Frames within applets
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What
if you&#8217;d like to print from within an applet? Well, to print anything you
must get a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintJob</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object through a <A NAME="Index2144"></A><A NAME="Index2145"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Toolkit</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object&#8217;s <A NAME="Index2146"></A><A NAME="Index2147"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getPrintJob(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, which takes only a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and not an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Applet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Thus it would seem that it&#8217;s possible to print from within an
application, but not an applet. However, it turns out that you can <A NAME="Index2148"></A><A NAME="Index2149"></A>create
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
from within an applet (which is the reverse of what I&#8217;ve been doing for
the applet/application examples so far, which has been making an applet and
putting inside a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
This is a useful technique since it allows you to use many <A NAME="Index2150"></A>applications
within applets (as long as they don&#8217;t violate applet security). When the
application window comes up within an applet, however, you&#8217;ll notice that
the Web browser sticks a little caveat on it, something to the effect of
&#8220;Warning: Applet Window.&#8221;
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that it&#8217;s quite straightforward to put a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside an applet. The only thing that you must add is code to <A NAME="Index2151"></A><A NAME="Index2152"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>dispose(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Frame</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
when the user closes it (instead of calling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.exit(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">):</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: PrintDemoApplet.java</font>
<font color="#009900">// Creating a Frame from within an Applet</font>
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.awt.event.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> PrintDemoApplet <font color="#0000ff">extends</font> Applet {
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
    Button b = <font color="#0000ff">new</font> Button("Run PrintDemo");
    b.addActionListener(<font color="#0000ff">new</font> PDL());
    add(b);
  }
  <font color="#0000ff">class</font> PDL <font color="#0000ff">implements</font> ActionListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      <font color="#0000ff">final</font> PrintDemo pd = <font color="#0000ff">new</font> PrintDemo();
      pd.addWindowListener(<font color="#0000ff">new</font> WindowAdapter() {
        <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e){
          pd.dispose();
        }
      });
      pd.setSize(500, 500);
      pd.show();
    }
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There&#8217;s
some confusion involved with Java 1.1<A NAME="Index2153"></A>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -