Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的图形类_Java_Swing_Graphics_Jlabel_Graphics2d - Fatal编程技术网

Java中的图形类

Java中的图形类,java,swing,graphics,jlabel,graphics2d,Java,Swing,Graphics,Jlabel,Graphics2d,我正在尝试用Java绘制一个函数。我知道有很多库可以实现这一点,但我想学习如何使用Java图形。我正在尝试创建一个BuffereImage并将其分配给标签。现在我只想让其他像素变成黑色,这样我就能看到它在工作。我会定期将值重新分配给BuffereImage。然而,我需要一个抽象的图形类。我需要实现自己的图形类扩展吗? 有没有更好或更喜欢的方法? 这可能也适用于图像观察者 这是我的密码: static BufferedImage I = new BufferedImage(X, Y, Buffer

我正在尝试用Java绘制一个函数。我知道有很多库可以实现这一点,但我想学习如何使用Java图形。我正在尝试创建一个BuffereImage并将其分配给标签。现在我只想让其他像素变成黑色,这样我就能看到它在工作。我会定期将值重新分配给BuffereImage。然而,我需要一个抽象的图形类。我需要实现自己的图形类扩展吗? 有没有更好或更喜欢的方法? 这可能也适用于图像观察者

这是我的密码:

static BufferedImage I = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB);
public static void main(String args[]){
        JLabel label = new JLabel(new ImageIcon(I));
        panel.add(label);
painter(I);
//edited to remove various declarations
}
public static void painter(BufferedImage b){
        for(int x = 0; x<b.getWidth(); x+=2){
            for(int y = 0; y<b.getHeight(); y+=2){
                b.setRGB(x,y, 000000);


            }
            paint(g, iobs);
        }

public void paint(Graphics g, ImageObserver iobs)
    {
        //ImageObserver iobs 
        g.drawImage(I, 0, 0, iobs);// iobs);    
    }
static BufferedImage I=新的BufferedImage(X,Y,BufferedImage.TYPE_INT_RGB);
公共静态void main(字符串参数[]){
JLabel标签=新JLabel(新图像图标(I));
面板。添加(标签);
画家(一);
//编辑以删除各种声明
}
公共静态空白绘制程序(BuffereImage b){

对于(int x=0;x您可能还想研究这些使用
setRGB()
的示例。第一个示例在一个框架中显示了多个视图,而第二个示例则提供了一些有关
BufferedImage
颜色模型如何选择颜色的见解。

您可能还想研究这些使用
setRGB()的示例
。第一个显示了一个框架中的几个视图,而第二个提供了一些关于
BuffereImage
ColorModel
如何选择颜色的见解。

只是好奇-你为什么要使用ImageObserver?在我从事Swing/Java图形研究的五年中,我从来没有使用过这个类。在AWT标签中绘画有paint()吗,但是对于Swing JComponent(JLabel)有方法paintComponent()吗?很奇怪,为什么要使用ImageObserver?在我接触Swing/Java图形的五年中,我从未使用过该类。在AWT标签中绘制有paint(),但对于Swing JComponent(JLabel)有方法paintComponent()吗
BufferedImage a = ...;
// In fact, this is a Graphics2D but it's safe to use it
// as a Graphics since that's the super class
Graphics g = a.createGraphics();

// now you can draw into the buffered image - here's a rect in upper left corner.
g.drawRect(0, 0, a.getWidth() / 2, a.getHeight() / 2);