在下面的代码中,我将 2dim 数组转换为缓冲图像(可以工作,图像是二进制的(背面和白色))。然后我显示这个图像。
我现在的问题是如何更新此图像(因为我想在每次循环运行时绘制一些此处未显示的内容)。
这也引出了我的第二个问题:如何在该图像上绘制点。 (这也意味着如果我想在 150,100 上绘制一个点;它应该在图像的像素 150,100 上)。
public void showImage(int xPoint, int yPoint) throws IOException {
// Two dim array conversion to a bufferedImage
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
tempValue = (pixelArray[y][x]==1) ? 255 : 0;
int value = tempValue << 16 | tempValue << 8 | tempValue;
bimg.setRGB(x, y, value);
}
}
JFrame canvas = new JFrame();
canvas.setSize(bimg.getWidth(), bimg.getHeight());
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setTitle("Contour");
Container pane = canvas.getContentPane();
ColorPanel panel = new ColorPanel(bimg,xPoint,yPoint);
pane.add(panel);
canvas.setVisible(true);
}
和
class ColorPanel extends JPanel {
BufferedImage bimg;
int x;
int y;
public ColorPanel(BufferedImage image,int _x, int _y) {
bimg = image;
x = _x;
y = _y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bimg, null, 0, 0);
}
}
我尝试的是:
g2d.setColor(Color.RED);
g2d.drawLine(x, y, x, y);
虽然每次运行都会打开一个新窗口,但我认为重点不在右侧
最佳答案
我为你做了一个小例子。
基本上是一个JFrame
带有自定义 JPanel
叫ColorPanel
(这很像你的,有一些额外的方法,即 drawDot(..)
和 setBufferedImage(..)
)
JFrame
将初始化并添加JPanel
与 BufferedImage
(在这种情况下完全黑色)。此后,将在 Image
上绘制白色点/像素。每 2 秒使用 BufferedImage#setRGB(...)
随机坐标(在图像范围内) .
我将计时器设置得更快(200milis),这就是图片的样子:
注意它是准确的,让它为一个明显的坐标着色,如 drawPoint(0,0)
你会看到(我没有演示这一点,因为屏幕截图不可能或没有任何用处)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class PixelDotOnImage {
public PixelDotOnImage() throws Exception {
JFrame frame = new JFrame();
frame.setTitle("Random Pixel Dots On Image with Timer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//create frame and components on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new PixelDotOnImage();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
private void initComponents(JFrame frame) throws Exception {
final BufferedImage bi = ImageIO.read(new URL("http://2.bp.blogspot.com/_KI3IRH6RxSs/S-uuLsgGJ3I/AAAAAAAAA5E/AA5mWBMLIvo/s1600/mat-black-lg.jpg"));
final ColorPanel cPanel = new ColorPanel(bi);
frame.add(cPanel);
//create timer to color random pixels
Timer timer = new Timer(2000, new AbstractAction() {
int xMax = bi.getWidth(), yMax = bi.getHeight();
Random rand = new Random();
@Override
public void actionPerformed(ActionEvent ae) {
int x = rand.nextInt(xMax);
int y = rand.nextInt(yMax);
if (cPanel.drawDot(x, y)) {
System.out.println("Drew white dot at: (" + x + "," + y + ")");
} else {
System.out.println("Cant draw white dot at: (" + x + "," + y + ")");
}
}
});
timer.start();
}
}
class ColorPanel extends JPanel {
private BufferedImage bimg;
private Dimension dims;
public ColorPanel(BufferedImage image) {
bimg = image;
dims = new Dimension(bimg.getWidth(), bimg.getHeight());
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bimg, 0, 0, null);
}
//this method will allow the changing of image
public void setBufferedImage(BufferedImage newImg) {
bimg = newImg;
}
//ths method will colour a pixel white
public boolean drawDot(int x, int y) {
if (x > dims.getWidth() || y > dims.getHeight()) {
return false;
}
bimg.setRGB(x, y, 0xFFFFFFFF);//white
repaint();
return true;
}
@Override
public Dimension getPreferredSize() {
return dims;
}
}
希望这有帮助。
关于java - 用 Java 在图像上精确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12918780/