Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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_Graphics2d_Repaint - Fatal编程技术网

Java 如何从主菜单访问重新绘制?

Java 如何从主菜单访问重新绘制?,java,swing,graphics,graphics2d,repaint,Java,Swing,Graphics,Graphics2d,Repaint,我正在开发一个Java程序,它基本上是从一个文件源渲染一个图像,然后将该图像绘制到一个面板上(在一个框架上) 现在我能做的是调用表单的一行代码 printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null); 其中myimage是包含图像的类 很多人都知道,这只会打印一次图像,如果我调整帧的大小,图像就会消失 现在解决这个问题的方法是将行放入repaint方法中,但是我现在在main方法中,那么如何访问repaint方法的定

我正在开发一个Java程序,它基本上是从一个文件源渲染一个图像,然后将该图像绘制到一个面板上(在一个框架上)

现在我能做的是调用表单的一行代码

printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);
其中
myimage
是包含图像的类

很多人都知道,这只会打印一次图像,如果我调整帧的大小,图像就会消失

现在解决这个问题的方法是将行放入
repaint
方法中,但是我现在在
main
方法中,那么如何访问
repaint
方法的定义并从
main
方法中更改它呢

谢谢

=====================================================================================================

我的代码:

主要类别:

    package imagetester;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.Graphics2D.*;
import java.io.*;

public class Imagetester 
{



    public static void main(String[] args) 
    {
        JFrame printframe = new JFrame("The drawing frame");
        JPanel printpanel = new JPanel();
        printframe.setSize(700,700);
        printpanel.setSize(700,700);
        printframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        printframe.add(printpanel);
        printpanel.setVisible(true);
        printframe.setVisible(true);

        Imageobject myimage = new Imageobject();
        try
        {
            myimage.setImage("word.jpg");
        }
        catch(IOException e)
        {
            System.out.println("the image failed!");
        }


        printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);
        printpanel.repaint();


        System.out.println("hi");

    }

}
myimage类:

    package imagetester;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Imageobject 
{
    BufferedImage globalimage;

    public void setImage(String filename) throws IOException
    {
        globalimage = ImageIO.read(new File(filename));

    }

    public void Imagebject()
    {

    }
}

我不确定我是否完全理解,但是如果您想在窗口中显示一个带有图像的面板,那么您应该子类化JPanel(或任何您想要的其他面板),并重写paintComponent方法来绘制图像。大致如下:

public class ImagePanel extends JPanel {
    private Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    @Override
    protected void paintComponent(graphics g) {
        g.drawImage(image, 0, 0, this);
    }
}

我不确定我是否完全理解,但是如果您想在窗口中显示一个带有图像的面板,那么您应该子类化JPanel(或任何您想要的其他面板),并重写paintComponent方法来绘制图像。大致如下:

public class ImagePanel extends JPanel {
    private Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    @Override
    protected void paintComponent(graphics g) {
        g.drawImage(image, 0, 0, this);
    }
}

我不确定我是否完全理解,但是如果您想在窗口中显示一个带有图像的面板,那么您应该子类化JPanel(或任何您想要的其他面板),并重写paintComponent方法来绘制图像。大致如下:

public class ImagePanel extends JPanel {
    private Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    @Override
    protected void paintComponent(graphics g) {
        g.drawImage(image, 0, 0, this);
    }
}

我不确定我是否完全理解,但是如果您想在窗口中显示一个带有图像的面板,那么您应该子类化JPanel(或任何您想要的其他面板),并重写paintComponent方法来绘制图像。大致如下:

public class ImagePanel extends JPanel {
    private Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    @Override
    protected void paintComponent(graphics g) {
        g.drawImage(image, 0, 0, this);
    }
}
现在我能做的是调用表单的一行代码
printpanel.getGraphics().drawImage(myimage.globalimage,0,0,null)

不,不要那样做。您的
printPanel
应该已经有了
paintComponent
方法,其中有一个
drawImage

Image image;
...
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        g.drawImage(image, ..., this); // this is the ImageObserver. Should not be null
    }
}
如果你想改变它,你可以为它设置一个setter

public void setImage(Image image) {
    this.image = image;
    repaint();
}
只需使用要更改的图像调用该方法


编辑 这是没有办法的。您需要
@覆盖
JPanel
paintComponent
方法。调整框架大小时,将自动调用
repaint()
,将图像保留在那里。应使用
paintComponent
方法绘制图像。如果希望用
ImageObject

现在我能做的是调用表单的一行代码
printpanel.getGraphics().drawImage(myimage.globalimage,0,0,null)

不,不要那样做。您的
printPanel
应该已经有了
paintComponent
方法,其中有一个
drawImage

Image image;
...
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        g.drawImage(image, ..., this); // this is the ImageObserver. Should not be null
    }
}
如果你想改变它,你可以为它设置一个setter

public void setImage(Image image) {
    this.image = image;
    repaint();
}
只需使用要更改的图像调用该方法


编辑 这是没有办法的。您需要
@覆盖
JPanel
paintComponent
方法。调整框架大小时,将自动调用
repaint()
,将图像保留在那里。应使用
paintComponent
方法绘制图像。如果希望用
ImageObject

现在我能做的是调用表单的一行代码
printpanel.getGraphics().drawImage(myimage.globalimage,0,0,null)

不,不要那样做。您的
printPanel
应该已经有了
paintComponent
方法,其中有一个
drawImage

Image image;
...
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        g.drawImage(image, ..., this); // this is the ImageObserver. Should not be null
    }
}
如果你想改变它,你可以为它设置一个setter

public void setImage(Image image) {
    this.image = image;
    repaint();
}
只需使用要更改的图像调用该方法


编辑 这是没有办法的。您需要
@覆盖
JPanel
paintComponent
方法。调整框架大小时,将自动调用
repaint()
,将图像保留在那里。应使用
paintComponent
方法绘制图像。如果希望用
ImageObject

现在我能做的是调用表单的一行代码
printpanel.getGraphics().drawImage(myimage.globalimage,0,0,null)

不,不要那样做。您的
printPanel
应该已经有了
paintComponent
方法,其中有一个
drawImage

Image image;
...
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        g.drawImage(image, ..., this); // this is the ImageObserver. Should not be null
    }
}
如果你想改变它,你可以为它设置一个setter

public void setImage(Image image) {
    this.image = image;
    repaint();
}
只需使用要更改的图像调用该方法


编辑 这是没有办法的。您需要
@覆盖
JPanel
paintComponent
方法。调整框架大小时,将自动调用
repaint()
,将图像保留在那里。应使用
paintComponent
方法绘制图像。如果希望用
ImageObject

很多人都知道,这只会打印一次图像,如果我调整帧的大小,图像就会消失

调整框架大小时,您的要求是什么

  • 您是否按原始大小绘制图像?如果是这样,那么只需使用带有图像图标的JLabel并将标签添加到框架中即可
  • 你想要照片吗