欢迎)有一点麻烦,请帮忙解决。这是代码:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif");
int width = img1.getWidth(this);
int height = img1.getHeight(this);
int scale = 2;
int w = scale * width;
int h = scale * height;
// explicitly specify width (w) and height (h)
g.drawImage(img1, 10, 10, (int) w, (int) h, this);
}
}
public class Graphics2DDrawScaleImage {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
如果我们运行代码,我们已经显示了一张图片。但我的任务是带来许多相同但在不同位置的图片。怎么做?)
如果你这样做:
for(int i = 0; i < 500 ; i+=100){
g.drawImage(img1, 10+i, 10, (int) w, (int) h, this);
}
然后图像连续显示。我想要更多随机