简体   繁体   English

如何从主要访问重绘?

[英]How to access repaint from the main?

I am working on a Java program that basically renders an image from a file source and then paints that image onto a panel (which is on a frame). 我正在研究一个Java程序,该程序基本上从文件源渲染图像,然后将该图像绘制到面板(在框架上)上。

Now what I am able to do is invoke a line of code of the form 现在我能做的就是调用以下形式的代码行

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

where myimage is a class that contains the image. 其中myimage是包含图像的类。

As many of you know, this only prints the image 1 time, and should I resize the frame, the image disappears. 众所周知,这只会打印图像1次,如果我调整框架的大小,图像会消失。

Now the way to fix this would be to put the line into the repaint method, but i'm in the main method right now so how do I access the definition of the repaint method and change it from within the main method? 现在解决此问题的方法是将行放入repaint方法中,但是我现在在main方法中,所以我如何访问repaint方法的定义并在main方法中进行更改?

Thanks! 谢谢!

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

My code: 我的代码:

MAIN CLASS: 主要类别:

    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 class: 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()
    {

    }
}

I'm not sure I understand fully, but if what you want to do is display a panel with an image inside a window, you should subclass JPanel (or whatever other panel you'd like), and override the paintComponent method to paint the image. 我不确定我是否完全理解,但是如果您想做的是在窗口内显示带有图像的面板,则应该子类化JPanel(或您想要的任何其他面板),并重写paintComponent方法来绘制图片。 Something along the lines of: 类似于以下内容:

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);
    }
}

Now what i am able to do is invoke a line of code of the form printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null); 现在我能做的是调用形式为printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);

No don't do that. 不,不要那样做。 Your printPanel should already have it's paintComponent method, with the a drawImage in it. 您的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
    }
}

Then you can just have a setter for it if you want to change it 然后,如果要更改它,您可以为其设置一个设置器

public void setImage(Image image) {
    this.image = image;
    repaint();
}

Just call that method with the image you want to change 只需使用您要更改的图像调用该方法


EDIT 编辑

There's no way around it. 没有办法解决。 You need to @Override the paintComponent method of the JPanel . 您需要@Override JPanelpaintComponent方法。 When you resize the frame, repaint() will automatically be called, leaving the image there. 调整框架大小时,将自动调用repaint()将图像保留在那里。 The image should be drawn in the paintComponent method. 该图像应在paintComponent方法中绘制。 You can have the panel's constructor take an Image argument if you want to instantiate that way, with the image from the ImageObject 如果要实例化,可以让面板的构造函数使用Image参数,并使用ImageObject的图像

As many of you know, this only prints the image 1 time, and should I resize the frame, the image disappears. 众所周知,这只会打印图像1次,如果我调整框架的大小,图像会消失。

What is your requirement when the frame resizes? 调整框架大小时您有什么要求?

  1. Do you paint the image at its original size? 您是否以原始尺寸绘制图像? If so then just use a JLabel with an ImageIcon and add the label to the frame 如果是这样,则只需将JLabel与ImageIcon一起使用,并将标签添加到框架中
  2. Do you want the image to scale with the size of the frame? 您是否希望图像随着框架的大小缩放? Then you need to do custom painting. 然后,您需要进行自定义绘画。 You can create a custom component as demonstrated in other answers. 您可以创建一个自定义组件,如其他答案所示。 Or you can use a JLabel with a Stretch Icon which will stretch to fill the space available to the label. 或者,您可以使用带有“ 拉伸”图标的JLabel,它将拉伸以填充标签可用的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM