我想使用 graphics2D 但我无法让它显示我的图形。有没有更好的方法可以让我使用 repaint()?最终,我想将图像设置为背景并能够在其上绘图,然后将框架的内容保存为图像。
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.swing.JApplet;
import java.awt.*;
// assume that the drawing area is 150 by 150
public class test extends JApplet
{
final int radius = 25;
int width = 200, height = 200;
BufferedImage img = new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
public void paint ( )
{
Graphics2D g = img.createGraphics();
g.setColor( Color.orange );
g.fillRect( 0, 0, 150, 150 );
g.setColor( Color.black );
g.drawOval( (150/2 - radius), (150/2 - radius), radius*2, radius*2 );
}
}