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

📄 imagedrawdemo.java

📁 非常好的java事例以及带源码事例的java2教程
💻 JAVA
字号:
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;

public class ImageDrawDemo extends JApplet
                           implements Runnable
{
  // The init() method to initialize everything...
  public void init()
  {
    sprite = createSprite(spriteSize);

    imagePanel = new ImagePanel();
    getContentPane().add(imagePanel);
  }

  // The start() method to start the animation...
  public void start()
  {
    rotator = new Thread(this);            // Create the animation thread
    rotator.start();                       // and start it
  }

  // The stop() method to stop the animation...
  public void stop()
  {
    rotator = null;                        // Discard the thread
  }

  public void run()
  {
    long time = System.currentTimeMillis(); // Starting time
    long interval = 50;                     // Time interval msec
    double secondsPerRotation = 15;         // Time for once round the circle - seconds
    totalAngle = 0;                         // Total angle moved through

    // Calculate the angle moved in interval milliseconds
    double angle = 2.*interval*Math.PI/(1000.0*secondsPerRotation);

    spriteAngle = 0;                        // Total rotation of sprite about its center

    // Calculate increment in interval milliseconds
    double spriteAngleIncrement = -4.0*angle;

    // Move image while the bouncer thread is running
    while(Thread.currentThread() == rotator)
    {
      imagePanel.repaint();                 // Repaint the image

      // Wait until the end of the interval
      try
      {
        time += interval;                   // Increment the time

        // Update the angles
        totalAngle += angle;
        spriteAngle += spriteAngleIncrement;

        Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
      }
      catch (InterruptedException e)
      {
        break;
      }
     }
  }

    void drawArm(Graphics2D g2D, int i)
   {
     AffineTransform at = g2D.getTransform();        // Save current transform
     g2D.setPaint(Color.darkGray);
     Stroke stroke = g2D.getStroke();                // Save current stroke
     g2D.setStroke(new BasicStroke(3));              // Set stroke as wide line
     g2D.draw(line);                                 // Draw the line
     g2D.setStroke(stroke);                          // Restore old stroke

     g2D.translate(line.getX2(), -spriteSize/20);    // Translate to circle position
     g2D.setPaint(colors[i%colors.length]);          // Set the fill color
     g2D.fill(circle);
     g2D.setTransform(at);                           // Restore original transform()
   }

  // The ImagePanel class defining the panel displaying the animation...
  class ImagePanel extends JPanel
  {
    public ImagePanel()
    {
      ImageIcon icon = new ImageIcon("Images\\wrox_logo.gif");
      image = icon.getImage();
    }

    public void paint(Graphics g)
    {
      Dimension size = getSize();                        // Get panel dimensions
      Graphics2D g2D = (Graphics2D)g;
       
      // Now fill the panel background
      g2D.setPaint(Color.gray);
      Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, size.width, size.height);
      g2D.fill(rect);

      g2D.translate(size.width/2, size.height/2);       // Move origin to panel center

      g2D.drawImage(image,                                     // Draw the logo
                    -image.getWidth(ImageDrawDemo.this)/2,     // Top left x
                    -image.getWidth(ImageDrawDemo.this)/2,     // Top left y
                    ImageDrawDemo.this);

      g2D.rotate(totalAngle);      // Rotate to the angle for the sprite position

      // Translate and rotate to sprite position - then translate back
      g2D.rotate(spriteAngle, 3*spriteSize/2, 0);

      g2D.translate(spriteSize, -spriteSize/2);         // Translate to sprite top left

      g2D.drawImage(sprite ,null, 0, 0);                // Draw the sprite
    }


    Image image;                                // The logo image
  }


  BufferedImage createSprite(int spriteSize)
  {
    // Create image with RGB and alpha channel
    BufferedImage sprite = new BufferedImage(spriteSize, spriteSize,
                                             BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2D = sprite.createGraphics();        // Context for buffered image

    // Set best alpha interpolation quality
    g2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                         RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

    // Clear image with transparent alpha by drawing a rectangle
    g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, spriteSize, spriteSize); 
    g2D.fill(rect);

    g2D.setComposite(AlphaComposite.SrcOver);
    line = new Line2D.Double(spriteSize/20.0,0,0.3*spriteSize,0);
    circle = new Ellipse2D.Double(0, 0, spriteSize/10.0, spriteSize/10.0);

    // Since sprite is symmetric, move origin to the center
    g2D.translate(spriteSize/2, spriteSize/2);

    int armCount = 8;                           // Number of arms in the sprite
    for(int i = 0; i < armCount; i++)
    {
      g2D.rotate(2*Math.PI/armCount);           // Rotate by pi/4
      drawArm(g2D, i);                          // Draw the arm
    }

     g2D.setPaint(Color.lightGray);                   // Set the fill color
     g2D.translate(-spriteSize/20,-spriteSize/20);    // Move origin to circle top left
     g2D.fill(circle);                                // Fill the circle
     g2D.dispose();                                   // Dispose of the context
     return sprite;                                   // Return the finished image
  }

  Thread rotator;                          // Thread rotating the sprite
  double totalAngle;                       // Current angular position of sprite
  double spriteAngle;                      // Rotation angle of sprite about its center
  ImagePanel imagePanel;                   // Panel to display animation

  BufferedImage sprite;                    // Stores reference to the sprite
  int spriteSize = 100;                    // Diameter of the sprite
  Ellipse2D.Double circle;                 // A circle - part of the sprite
  Line2D.Double line;                      // A line - part of the sprite

  // Colors used in sprite
  Color[] colors = {Color.red , Color.yellow, Color.green  , Color.blue,
                    Color.cyan, Color.pink  , Color.magenta, Color.orange};
}

⌨️ 快捷键说明

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