java - 在java中绘制像素

标签 java pixel bufferedimage

我一次又一次地尝试掌握基本的 Java 编程,但我编写的所有程序中出现的大量错误已经让我放弃了。这次我尝试设置一个像素,然后通过添加一行又一行或循环来设置更多像素。

import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Points extends JPanel {
    BufferedImage image = new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB);
    rgb = 0xFF00FF00; // green
    image.setRGB(1, 1, rgb);

  public static void main(String[] args) {
    Points points = new Points();
    JFrame frame = new JFrame("Points");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(points);
    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

Points.java:7: error: <identifier> expected
    rgb = 0xFF00FF00; // green
       ^
Points.java:8: error: <identifier> expected
    image.setRGB(1, 1, rgb);
                ^
Points.java:8: error: illegal start of type
    image.setRGB(1, 1, rgb);
                 ^
Points.java:8: error: illegal start of type
    image.setRGB(1, 1, rgb);
                    ^
Points.java:8: error: <identifier> expected
    image.setRGB(1, 1, rgb);
                          ^

5 个错误

代码部分是我在下面遇到的错误。

最佳答案

rgb 未定义,因此编译器不知道应该用它做什么。

您还尝试在执行上下文之外执行一段代码。

public class Points extends JPanel {
    BufferedImage image = new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB);
    // Invalid decleration, rgb is undefined
    rgb = 0xFF00FF00; // green
    // execution of code outside of a execution context
    image.setRGB(1, 1, rgb);

声明rgb

int rgb = 0xFF00FF00; // green

image.setRGB(1, 1, rgb); 移动到适当的执行上下文,例如方法或构造函数...

public Points () {
    image.setRGB(1, 1, rgb);

另请记住,像素数据的索引为 0,这意味着第一个像素出现在 0x0

关于java - 在java中绘制像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539167/

相关文章:

java - 如何从 XSD 生成 JAXB 类?

java - Java中如何读取多种语言的文件?

python - OpenCV:像素数与行数*列数

Java : BufferedImage to Bitmap format

java - 保存使用graphics2d、java绘制的缓冲图像

java - 如何在级联中仅从一行中获取某些列

java - tagoup 破坏了良好的 xml

c - 调整 BMP 图像的大小(使其变小)

java - 使绿色消失或透明

Java - 从图像中获取像素数组