gpt4 book ai didi

java - 如何插入图像 .gif 文件而不是 filloval?

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:27 24 4
gpt4 key购买 nike

我是编程新手,感谢您的帮助。我创建了这段代码,我想用 .gif 文件/对象替换 fillOval。有哪些修改
我应该表演吗?

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel {

int x;
int y;

int inix=500;
int iniy=500;

int i=0;
private void moveBall() {
/*x = x + 1;
y = y + 1;*/
double degrees=(double) i;
double radians=Math.toRadians(degrees);
double Sinu=Math.sin(radians);
double Sinu200=Math.sin(radians)*200;
int SinuInt=(int) Sinu200;
//y=500+SinuInt;
y=iniy+SinuInt;
double Cos=Math.cos(radians);
double Cos200=Math.cos(radians)*200;
int CosInt=(int) Cos200;
//x=500+CosInt;
x=inix+CosInt;

i++;
if (i==360) i=0;
//System.out.println(Sinu+" "+Sinu200+" "+SinuInt +" "+x);

}

private int sin(double radians) {
// TODO Auto-generated method stub
return 0;
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(x, y, 50, 50);


}

public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
Game game = new Game();
frame.add(game);
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

while (true) {
game.moveBall();
game.repaint();
Thread.sleep(3);
}
}


} >

最佳答案

首先,您应该使用paintComponent()而不是paint()

要回答您的问题,请使用g.drawImage()。像这样的东西。

protected void paintComponent(Graphics g){
super.paintComponent(g);

try {
BufferedImage img = ImageIO.read(new File("image.gif"));
g.drawImage(img, xLocation, yLocation, width, height, this);
} catch (Exception ex){
ex.printStackTrace();
}
}

参见Graphics#drawImage()

关于java - 如何插入图像 .gif 文件而不是 filloval?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com