java產生一張圖片,java產生一張

來源:互聯網
上載者:User

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="圖片地址" />的方式嗎?
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.