1
通過簡單的循環或數組簡單地將像素打印到Java屏幕上的最佳方式是什麼?Java - 如何在屏幕上繪製像素
通過簡單的循環或數組簡單地將像素打印到Java屏幕上的最佳方式是什麼?Java - 如何在屏幕上繪製像素
您可以使用BufferedImage並將其顯示在JLabel上。喜歡的東西:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.List;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
int size = 300;
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
ImageIcon icon = new ImageIcon(bi);
add(new JLabel(icon));
for (int y = 0; y < size; y += 5)
{
for (int x = 0; x < size; x++)
{
Color color = (y % 2 == 0) ? Color.RED : Color.GREEN;
int colorValue = color.getRGB();
bi.setRGB(x, y, colorValue);
bi.setRGB(x, y + 1, colorValue);
bi.setRGB(x, y + 2, colorValue);
bi.setRGB(x, y + 3, colorValue);
bi.setRGB(x, y + 4, colorValue);
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
或者你可以創建一個自定義組件,使用Graphics類的方法實現paintComponent(...)
方法:
Graphics.fillRect(...);
Graphics.fillOval(...);
etc..
閱讀從Custom Painting Swing的教程部分獲取更多信息和示例讓你開始。別忘了閱讀其他圖形方法的Graphics
API。
這似乎是基於意見 – 2015-03-30 18:09:58
你到目前爲止考慮和嘗試了什麼? – jny 2015-03-30 18:10:41
我目前不知道如何以任何方式繪製像素,但我已經看了一下Canvas和BufferedImage類 – 2015-03-30 18:14:09