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

📄 texturetest.java

📁 本源码演示Texture的使用方法
💻 JAVA
字号:
     import java.applet.*;
     import java.awt.*;
     import java.awt.image.*;
     import java.awt.event.*;
     import java.awt.geom.*; 
          
     public class TextureTest extends Applet implements ActionListener
     {  
          // field for accepting a filename
          private TextField input;

          public void init()
          {
               // create a layout and add the textfield and "Ok" button
               setLayout(new BorderLayout());
               Panel p = new Panel();
               input = new TextField("", 20);
               p.add(input);
               Button ok = new Button("Ok");
               ok.addActionListener(this);
               p.add(ok);
               add(p, BorderLayout.SOUTH);
          }

          public void paint(Graphics g)
          {
               // cast the sent Graphics context to get a usable Graphics2D object
               Graphics2D g2d = (Graphics2D)g;

               // just draw an outline of the shape and return if the text 
               // field just contains white-space
               if("".equals(input.getText().trim()))
               {
                    g2d.translate(112, 15);
                    g2d.rotate(Math.PI/4);
                    g2d.draw(new Rectangle2D.Double(0, 0, 104, 104));
                    return;
               }

               // load an image
               // we'll look at the MediaTracker class when we discuss animation

               MediaTracker mt = new MediaTracker(this);
               Image image = getImage(getCodeBase(), input.getText());
               mt.addImage(image, 0);
               try
               {     mt.waitForAll();
               }
               catch(InterruptedException e) { /* do nothing */ }

               // the filename was probably invalid if the width or height of 
               // the image created is <= 0
               if(image.getWidth(this) <= 0 || image.getHeight(this) <= 0) 
               {
                    // print error message and return
                    input.setText(input.getText() + " : invalid filename.");
                    return;
               }

               // create a new BufferedImage with the image's width and height
               BufferedImage bi = new BufferedImage(
                        image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_RGB);

               // get the Graphics2D context of the BufferedImage and render the original
               // image onto it
               ((Graphics2D)bi.getGraphics()).drawImage(image, new AffineTransform(), this);

               // create the anchoring rectangle for the paint's image equal in size to 
               // the image's size
               Rectangle2D bounds = new Rectangle2D.Float(0, 0, bi.getWidth(), bi.getHeight());

               // set the paint
               g2d.setPaint(new TexturePaint(bi, bounds));

               // transform and render!
               g2d.translate(112, 15);
               g2d.rotate(Math.PI/4);
               g2d.fill(new Rectangle2D.Double(0, 0, 104, 104));
          }

          public void actionPerformed(ActionEvent e)
          {
               // the "Ok" was pressed; update the changes
               repaint();
          } 
     }    // TextureTest

⌨️ 快捷键说明

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