最节能就是把6个图片轮流画一次,不过你要把这六个图片弄成资源,然后就当作普通图来画,这是最简单的方法,如果你要全部自己弄,这6个图很难创建出来,你图里有渐变效果,这个渐变的值不知道是多少。我给一个画渐变园的类给你。如果要自己画可能有点用。
public class PaintTest {
public static void main(String[] args) {
JFrame frame = new PaintTestFrame();
frame.show();
}
}
class PaintTestFrame extends JFrame implements ActionListener {
public PaintTestFrame() {
setTitle("PaintTest");
setSize(400, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new PaintPanel();
contentPane.add(canvas, "Center");
JPanel buttonPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
colorButton = new JRadioButton("Color", true);
buttonPanel.add(colorButton);
group.add(colorButton);
colorButton.addActionListener(this);
gradientPaintButton = new JRadioButton("Gradient Paint", false);
buttonPanel.add(gradientPaintButton);
group.add(gradientPaintButton);
gradientPaintButton.addActionListener(this);
texturePaintButton = new JRadioButton("Texture Paint", false);
buttonPanel.add(texturePaintButton);
group.add(texturePaintButton);
texturePaintButton.addActionListener(this);
contentPane.add(buttonPanel, "North");
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == colorButton) {
canvas.setColor();
} else if (source == gradientPaintButton) {
canvas.setGradientPaint();
} else if (source == texturePaintButton) {
canvas.setTexturePaint();
}
}
private PaintPanel canvas;
private JRadioButton colorButton;
private JRadioButton gradientPaintButton;
private JRadioButton texturePaintButton;
}
class PaintPanel extends JPanel {
public PaintPanel() {
Image image = Toolkit.getDefaultToolkit().getImage("java2s.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
bufferedImage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
setColor();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(paint);
Ellipse2D circle = new Ellipse2D.Double(0, 0, getWidth(), getHeight());
g2.fill(circle);
}
public void setColor() {
paint = Color.red; // Color implements Paint
repaint();
}
public void setGradientPaint() {
paint = new GradientPaint(0, 0, Color.red, (float) getWidth(),
(float) getHeight(), Color.blue);
repaint();
}
public void setTexturePaint() {
Rectangle2D anchor = new Rectangle2D.Double(0, 0, 2 * bufferedImage.getWidth(), 2 * bufferedImage.getHeight());
paint = new TexturePaint(bufferedImage, anchor);
repaint();
}
private Paint paint;
private BufferedImage bufferedImage;
}
温馨提示:答案为网友推荐,仅供参考