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

📄 tij0149.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
      g.dispose();
      pd.end();
    }
  }
  <font color="#0000ff">class</font> GBL <font color="#0000ff">implements</font> ActionListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> actionPerformed(ActionEvent e) {
      PrintData pd = 
        <font color="#0000ff">new</font> PrintData("Print Graphics Test");
      <font color="#0000ff">if</font>(pd == <font color="#0000ff">null</font>) <font color="#0000ff">return</font>;
      plot.print(g);
      g.dispose();
      pd.end();
    }
  }
  <font color="#0000ff">class</font> RingL <font color="#0000ff">implements</font> TextListener {
    <font color="#0000ff">public</font> <font color="#0000ff">void</font> textValueChanged(TextEvent e) {
      <font color="#0000ff">int</font> i = 1;
      <font color="#0000ff">try</font> {
        i = Integer.parseInt(ringNum.getText());
      } <font color="#0000ff">catch</font>(NumberFormatException ex) {
        i = 1;
      }
      plot.rings = i;
      plot.repaint();
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Frame pdemo = <font color="#0000ff">new</font> PrintDemo();
    pdemo.setTitle("Print Demo");
    pdemo.addWindowListener(
      <font color="#0000ff">new</font> WindowAdapter() {
        <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    pdemo.setSize(500, 500);
    pdemo.setVisible(<font color="#0000ff">true</font>);
  }
}

<font color="#0000ff">class</font> Plot <font color="#0000ff">extends</font> Canvas {
  <font color="#0000ff">public</font> <font color="#0000ff">int</font> rings = 3;
}

<font color="#0000ff">class</font> Plot1 <font color="#0000ff">extends</font> Plot {
  <font color="#009900">// Default print() calls paint():</font>
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
    <font color="#0000ff">int</font> w = getSize().width;
    <font color="#0000ff">int</font> h = getSize().height;
    <font color="#0000ff">int</font> xc = w / 2;
    <font color="#0000ff">int</font> yc = w / 2;
    <font color="#0000ff">int</font> x = 0, y = 0;
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; rings; i++) {
      <font color="#0000ff">if</font>(x &lt; xc &amp;&amp; y &lt; yc) {
        g.drawOval(x, y, w, h);
        x += 10; y += 10;
        w -= 20; h -= 20;
      }
    }
  }
} 

<font color="#0000ff">class</font> Plot2 <font color="#0000ff">extends</font> Plot {
  <font color="#009900">// To fit the picture to the page, you must</font>
  <font color="#009900">// know whether you're printing or painting:</font>
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
    <font color="#0000ff">int</font> w, h;
    <font color="#0000ff">if</font>(g <font color="#0000ff">instanceof</font> PrintGraphics) {
      PrintJob pj = 
        ((PrintGraphics)g).getPrintJob();
      w = pj.getPageDimension().width;
      h = pj.getPageDimension().height;
    } 
    <font color="#0000ff">else</font> {
      w = getSize().width;
      h = getSize().height;
    }
    <font color="#0000ff">int</font> xc = w / 2;
    <font color="#0000ff">int</font> yc = w / 2;
    <font color="#0000ff">int</font> x = 0, y = 0;
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; rings; i++) {
      <font color="#0000ff">if</font>(x &lt; xc &amp;&amp; y &lt; yc) {
        g.drawOval(x, y, w, h);
        x += 10; y += 10;
        w -= 20; h -= 20;
      }
    }
  }
} 

<font color="#0000ff">class</font> Plot3 <font color="#0000ff">extends</font> Plot {
  <font color="#009900">// Somewhat better. Separate </font>
  <font color="#009900">// printing from painting:</font>
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> print(Graphics g) {
    <font color="#009900">// Assume it's a PrintGraphics object:</font>
    PrintJob pj = 
      ((PrintGraphics)g).getPrintJob();
    <font color="#0000ff">int</font> w = pj.getPageDimension().width;
    <font color="#0000ff">int</font> h = pj.getPageDimension().height;
    doGraphics(g, w, h);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g) {
    <font color="#0000ff">int</font> w = getSize().width;
    <font color="#0000ff">int</font> h = getSize().height;
    doGraphics(g, w, h);
  }
  <font color="#0000ff">private</font> <font color="#0000ff">void</font> doGraphics(
      Graphics g, <font color="#0000ff">int</font> w, <font color="#0000ff">int</font> h) {
    <font color="#0000ff">int</font> xc = w / 2;
    <font color="#0000ff">int</font> yc = w / 2;
    <font color="#0000ff">int</font> x = 0, y = 0;
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; rings; i++) {
      <font color="#0000ff">if</font>(x &lt; xc &amp;&amp; y &lt; yc) {
        g.drawOval(x, y, w, h);
        x += 10; y += 10;
        w -= 20; h -= 20;
      }
    }
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
program allows you to select fonts from a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Choice</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
list (and you&#8217;ll see that the number of fonts available in Java 1.1<A NAME="Index2122"></A>
is still extremely limited, and has nothing to do with any extra fonts you
install on your machine). It uses these to print out text in bold, italic, and
in different sizes. In addition, a new type of component called a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created to demonstrate graphics. A 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
has rings that it will display on the screen and print onto paper, and the
three derived classes 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot1</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot2,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Plot3</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
perform these tasks in different ways so that you can see your alternatives
when printing graphics. Also, you can change the number of rings in a plot
&#8211; this is interesting because it shows the printing fragility in Java 1.1<A NAME="Index2123"></A>.
On my system, the printer gave error messages and didn&#8217;t print correctly
when the ring count got &#8220;too high&#8221; (whatever that means), but
worked fine when the count was &#8220;low enough.&#8221; You will notice, too,
that the page dimensions produced when printing do not seem to correspond to
the actual dimensions of the page. This might be fixed in a future release of
Java, and you can use this program to test it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
program encapsulates functionality inside inner classes whenever possible, to
facilitate reuse. For example, whenever you want to begin a print job (whether
for graphics or text), you must create a <A NAME="Index2124"></A><A NAME="Index2125"></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, which has its own 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">object
along with the width and height of the page. The creation of a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintJob</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and extraction of page dimensions is encapsulated in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintData</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class.
</FONT><P></DIV>
<A NAME="Heading450"></A><H4 ALIGN=LEFT>
Printing
text
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Conceptually,
<A NAME="Index2126"></A><A NAME="Index2127"></A>printing
text is straightforward: you choose a typeface and size, decide where the
string should go on the page, and draw it with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Graphics.drawString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This means, however, that you must perform the calculations of exactly where
each line will go on the page to make sure it doesn&#8217;t run off the end of
the page or collide with other lines. If you want to make a word processor,
your work is cut out for you.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ChangeFont</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
encapsulates a little of the process of changing from one font to another by
automatically creating a new <A NAME="Index2128"></A><A NAME="Index2129"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Font</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object with your desired typeface, style (

⌨️ 快捷键说明

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