Java Tutorial/2D Graphics/GIF

Материал из Java эксперт
Перейти к: навигация, поиск

Hide the mouse cursor: use a transparent GIF as the cursor

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
public class Main {
  public static void main(String[] argv) throws Exception {
    int[] pixels = new int[16 * 16];
    Image image = Toolkit.getDefaultToolkit().createImage(
        new MemoryImageSource(16, 16, pixels, 0, 16));
    Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(
        image, new Point(0, 0), "invisibleCursor");
  }
}





Save To GIF

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class SaveToGIF {
  final static int WIDTH = 50;
  final static int HEIGHT = 50;
  final static int NUM_ITER = 1500;
  public static void main(String[] args) throws Exception {
    BufferedImage bi;
    bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    for (int i = 0; i < NUM_ITER; i++) {
      g.setColor(Color.RED);
      g.drawLine(1, 2, i, i + 1);
    }
    g.dispose();
    ImageIO.write(bi, "gif", new File("image.gif"));
  }
}