歡迎您)有點麻煩,請幫忙解決。下面的代碼:隨機輸出圖片
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);
}
}
如果我們運行的代碼,我們已經顯示picture.But我的任務是把許多相同的圖像,但在不同的地方。如何做到這一點)
如果你這樣做:
for(int i = 0; i < 500 ; i+=100){
g.drawImage(img1, 10+i, 10, (int) w, (int) h, this);
}
然後將圖像顯示在一排。我想更多的randoml
看看這裏http://docs.oracle.com/javase/7/docs/api/java/util/Random.html – user902383
謝謝大家!主要是圖片不「跳」 –