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

📄 the java game programming tutorial part v.htm

📁 不错的JAVA书
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.intergate.bc.ca/personal/iago/javatut/jtp5.htm -->
<HTML><HEAD><TITLE>The Java Game Programming Tutorial: Part V</TITLE>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<META content="Garry Morse" name=Author>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content="An expanding tutorial on Java 2D/3D game programming" 
name=Description>
<META 
content=Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java,Java 
name=KeyWords></HEAD>
<BODY aLink=#ff0000 bgColor=#440044 link=#0000ee text=#db9504 vLink=#7724c1>
<H1><A href="http://www.intergate.bc.ca/personal/iago/javatut/index.htm"><IMG 
align=ABSCENTER border=0 height=85 
src="The Java Game Programming Tutorial Part V_ficheiros/coffee.gif" 
width=116></A><FONT color=#804040><FONT size=+2><A 
href="http://www.intergate.bc.ca/personal/iago/javatut/index.htm">The Java Game 
Programming Tutorial</A></FONT></FONT></H1>
<H1>
<HR width="100%">
</H1>
<H2>Part V:&nbsp;What about Image Filters?</H2>
<UL>
  <P>In <A href="http://www.intergate.bc.ca/personal/iago/javatut/jtp4.htm">Part 
  4</A>, an image was drawn into a double buffer and shown<BR>in the applet 
  window. One thing you may have noticed is the<BR>degraded image quality in the 
  applet compared to the frames<BR>of animation displayed on the tutorial page. 
  My incredibly<BR>rotating man lost his good looks and became rather 
  ruddy.<BR>Art imitating life? </P></UL>
<UL>
  <P>I wanted to demonstrate that Java dithers images down to a<BR>palette of 
  256 colors while browsers handle millions of<BR>colors easily. It is possible 
  to draw true color images<BR>with Java but only desirable for some users which 
  takes<BR>away from general graphics use. </P>
  <P>For the time being, to reduce the effects of dithering, you<BR>can use a 
  screen capture utility to catch the applet web page<BR>and edit your images to 
  fit Java's palette decisions better.<BR>As always with graphics, the end 
  result is what matters most<BR>and patchworks methods can help considerably. 
  </P>
  <P>I talked about images in <A 
  href="http://www.intergate.bc.ca/personal/iago/javatut/jtp3.htm">Part 3</A> 
  but this is getting to know<BR>them better. Our intimacy with them begins with 
  the fact<BR>that an image is just an array of dots called pixels. 
  Imagine<BR>each row of the image strung first to last in a single entity<BR>as 
  a longer row of values. </P>
  <P>Each value is an integer composed of four bytes forming the<BR>pixel to be 
  drawn by Java. These are extracted to be three<BR>intensity values; red, 
  green, and blue which combine to make<BR>the colour you see onscreen. The 
  fourth byte is called the<BR>alpha channel and is a value of transparency for 
  the pixel.<BR>A value of 0 would make the pixel appear transparent as 
  a<BR>value of 255 would indicate an opaque pixel. Values between<BR>make 
  pixels partially transparent. </P>
  <P>I have created a special image class to deal with the internal <BR>elements 
  of Java images. This code can be used without any<BR>modification but I will 
  explain all that is happening inside. </P>
  <P><A 
  href="http://www.intergate.bc.ca/personal/iago/javatut/fximgdef.htm">Click 
  here to learn about the internal Java code of FXImage.class</A> </P>
  <P>And now I'll go through the main applet code:</P><PRE>  <B>public class FXCycle extends Applet {</B></PRE>
  <P>Note here how my special effects class is declared as a<BR>variable 
  instance which will instruct the compiler to<BR>virtually bind it to 
  FXCycle.class which also means you can<BR>declare my class in your own applet 
  loops and utilize its<BR>black box functionality without learning the internal 
  workings. </P><PRE>    <B>FXImage fxi;</B></PRE>
  <P>If you do this, you will require your class (or FXCycle.class)<BR>and 
  definitely the FXImage.class in the same directory or path<BR>for this to 
  work. Below are the other variable declarations,<BR>including one defined 
  constant - FX_MAX. </P><PRE><B>    Image img;

    int effect = 0;

    static final int FX_MAX = 7;</B></PRE>
  <P>Here, the image is initialized (see previous tutorials) as<BR>usual. The 
  new instance of FXImage takes the image class<BR>as a parameter. It may seem 
  superfluous to convert the image<BR>here but it affects the color display. 
  Returning it back to<BR>its origin image class means an initially smoother 
  picture.<BR>Trust me. </P><PRE><B>    public void init() {
      img = getImage(getCodeBase(),getParameter("IMAGENAME") + ".gif");

      fxi = new FXImage(img);
      fxi.save();
      img = fxi.create();
    }</B></PRE>
  <P>This function handles your mouse clicks on the image. </P><PRE>    <B>public boolean mouseDown(Event evt,int x,int y) {</B></PRE>
  <P>The restore function is called here to normalize the<BR>image before doing 
  another special effect. It works<BR>better this way, due to the FX algorithms. 
  </P><PRE>      <B>fxi.restore();</B></PRE>
  <P>This part selects a special effect to perform based<BR>on the counter 
  effect. This appears to be inefficient<BR>code but it didn't work as a case 
  statement. Trust me. </P><PRE><B>      if(effect==0) fxi.negative();
      else if(effect==1) fxi.lighting(50,50,50);
      else if(effect==2) fxi.lighting(170,170,170);
      else if(effect==3) fxi.fuzzy(90);
      else if(effect==4) fxi.border(50,50,30);
      else if(effect==5) fxi.grey();
      else if(effect==6) fxi.stone(45,2,100,100,100);
      else if(effect==7) {}

</B> <B>     if(++effect &gt; FX_MAX) effect = 0;</B></PRE>
  <P>Observe here how a new image must be created each time. </P><PRE>      <B>img = fxi.create();</B></PRE>
  <P>Now call repaint to trigger the paint method's image drawing. </P><PRE>      <B>repaint();

      return true;
    }</B></PRE>
  <P>The paint method below will draw the current image onscreen. </P><PRE><B>    public void paint(Graphics g) {
      g.drawImage(img,0,0,this.size().width,this.size().height,this);
    }

  }</B></PRE>
  <P>And that's it. This might seem slow because of the necessary<BR>allocation 
  used by FXImage.Create but another intention of mine<BR>is that you use the <A 
  href="http://www.intergate.bc.ca/personal/iago/javatut/fximgdef.htm">FXImage 
  class</A> to generate all kinds of warped<BR>images when your applet loads and 
  save each in an image loop as<BR>I taught you in the slideshow program (<A 
  href="http://www.intergate.bc.ca/personal/iago/javatut/jtp3.htm">Part 3</A>). 
  </P>
  <P>Here is an example of modifying your code to build FX animation arrays: 
</P><PRE>  <B>Image img;
  Image imgs[];
  FXImage fxi;

  public void init() {
    imgs = new Image[CONSTANT_COUNT];

    img = getImage(getCodeBase(),getParameter("IMAGENAME") + ".gif");

    fxi = new FXImage(img);

    for(int count=0; count&lt;CONSTANT_COUNT; count++) {

      fxi.save();
      fxi.lighting(50+(count*3),50+(count*3),50+(count*3));

      imgs[count] = fxi.create();

      fxi.restore();
    }
  }</B></PRE>
  <P>The special effects will be explained in the next tutorial part<BR>along 
  with their respective prototypes. In the meantime, play<BR>around with the <A 
  href="http://www.intergate.bc.ca/personal/iago/javatut/fximgdef.htm">FXImage 
  class</A>. It should be useful to generate<BR>animations, banners, fade to 
  black, etc... </P>
  <P>For clarity and not brevity, here is the entire code listing:</P><PRE><B>// FXCycle.java
// by Garry Morse

import java.awt.*;
import java.applet.*;

public class FXCycle extends Applet {

  // special effects image class
  FXImage fxi;
  Image img;

  int effect = 0;
  
  static final int FX_MAX = 7;

  // initializes applet
  public void init() {
    // load image from file
    img = getImage(getCodeBase(),getParameter("IMAGENAME") + ".gif");

    // create a new image class
    fxi = new FXImage(img);
    // save its initial contents
    fxi.save();
    
    img = fxi.create();
  }

  // handler for mouse click events on image
  public boolean mouseDown(Event evt,int x,int y) {

    // restore image to initial state before next alteration
    fxi.restore();

    // perform special effect based on cycle counter
    if(effect==0) fxi.negative();
    else if(effect==1) fxi.lighting(50,50,50);
    else if(effect==2) fxi.lighting(170,170,170);
    else if(effect==3) fxi.fuzzy(90);
    else if(effect==4) fxi.border(50,50,30);
    else if(effect==5) fxi.grey();
    else if(effect==6) fxi.stone(45,2,100,100,100);
    else if(effect==7) {}

    // reloop if finished cycle
    if(++effect &gt; FX_MAX) effect = 0;

    // re-create image class from special effect class
    img = fxi.create();

    // draw image with new special effect alteration
    repaint();

    return true;
  }

  // paints image in applet window
  public void paint(Graphics g) {
    g.drawImage(img,0,0,this.size().width,this.size().height,this);
  }

}</B></PRE>
  <P>Below are the HTML parameters:</P>
  <P><B>&lt;APPLET CODE="FXCycle.class" WIDTH=100 HEIGHT=100&gt;<BR>&lt;PARAM 
  NAME="IMAGENAME" VALUE="../garry"&gt;<BR>&lt;/APPLET&gt; </B></P></UL>
<P><FONT size=+1><A 
href="http://www.intergate.bc.ca/personal/iago/javatut/fxcycle.htm">Click here 
to see the Image Filter applet in action!</A></FONT></P>
<P><FONT color=#800080><FONT size=-2>The Java Game Programming Tutorial and all 
tutorials within are created by Garry Morse, Copyright 
1997</FONT></FONT></P></BODY></HTML>

⌨️ 快捷键说明

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