java產生一張圖片,java產生一張
public class CreateImage {public static void main(String[] args) throws Exception{int width = 100; int height = 100; String s = "你好"; File file = new File("/Users/tengxin/Pictures/image.jpg"); Font font = new Font("Serif", Font.BOLD, 10); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)bi.getGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, width, height); g2.setPaint(Color.RED); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(s, context); double x = (width - bounds.getWidth()) / 2; double y = (height - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.drawString(s, (int)x, (int)baseY); ImageIO.write(bi, "jpg", file); }}
java 隨機產生圖片
你的代碼不需要改太多,就是JLabel數組labels只是一個JLabel數組,並沒有建立真正的JLabel對象,所以加上一句labels[i] = new JLabel();就行了。
原始碼:
import javax.swing.*;
import java.awt.*;
public class Exercise_4 {
public static void main(String[] args) {
JFrame frame = new JFrame("Exercise 4");
frame.setLayout(new GridLayout(3, 3));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel[] labels = new JLabel[9];
for (int i = 0; i < 9; i++) {
labels[i] = new JLabel();
frame.add(labels[i]);
int num = ((int) (Math.random() * 3));
if (num == 0) {
labels[i].setIcon(new ImageIcon("X.jpg"));
}
else if (num == 1) {
labels[i].setIcon(new ImageIcon("0.jpg"));
}
}
}
}
JAVA 在一個Action中產生多張圖片並顯示在JSP上
請問你的jsp頁面準備怎麼顯示圖片?是用<img src="圖片地址" />的方式嗎?