[英]drawing pixels in java
我一次又一次地尝试掌握基本的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
setRGB方法将第三个参数作为int rgb
说您想将像素着色为绿色
您必须先创建Color类型的对象
Color myGreenColor=new Color(255,0,0);
然后您可以将其设置为像
image.setRGB(i,j,myGreenColor.getRGB());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.