简体   繁体   English

Swing问题绘制2张图像

[英]Swing issues drawing 2 images

I'm trying to design a GUI using SWING.My problem is that I'm not sure how the paintComponent method works. 我正在尝试使用SWING设计GUI。我的问题是我不确定paintComponent方法是如何工作的。 I'm trying to display 2 images but only the one from PanClass is displayed.Here is the relevant code(2 classes) . 我正在尝试显示2个图像,但只显示了PanClass中的图像。这是相关代码(2个类)。

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;

public class LP3 extends JPanel
{
    public static BufferedImage image;
    public static BufferedImage image2;
    private JFrame frame=new JFrame();
    private PanClass Panel=new PanClass();

    public LP3()
    {
        try
        {               
            image2=ImageIO.read(new File("New Game.png"));     
        }
        catch (IOException e)
        {
            //Nothing
        }
        frame.setSize(1000,100);
        frame.setResizable(true);
        frame.add(Panel);
        Panel.setOpaque(true);
        frame.pack();
        frame.setVisible(true);     
    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(image2,0,0,null);
    }
}

Class No2: 2级:

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;

public class PanClass extends JPanel
{
    private static BufferedImage theimage;
    private static BufferedImage image2;
    private JPanel a=new JPanel();

    public PanClass()
    {
        a.setLayout(null);
        a.setOpaque(true);
        try
        {                 
            theimage = ImageIO.read(new File(design4.jpg"));
        }
        catch (IOException e)
        {
            //Not handled.
        }
    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(theimage,0,0,null);
    }
}

The code as it is now displays only the image from PanClass. 现在的代码只显示来自PanClass的图像。 If I get to add the drawing of both images to be done in the PanClass then both will be correctly drawn. 如果我要在PanClass中添加两个图像的绘图,那么两者都将被正确绘制。 I am interested in knowing why this happens as I'm more interested in learning how it works rather than getting the job done. 我很想知道为什么会这样,因为我更感兴趣的是学习它的工作原理而不是完成工作。 Also If I create a JFrame and a JLayered Pane in a class, then create 2 more classes drawing an image with paintComponent() (using similar code to the above) and then add an instance of each class on the Layered Pane on a different Layer of the first class, why nothing is displayed? 此外,如果我在类中创建JFrame和JLayered窗格,则创建另外两个类,使用paintComponent()绘制图像(使用与上面类似的代码),然后在不同图层上的分层窗格上添加每个类的实例第一堂课,为什么没有显示?

(My main method is supposed to be on LP3 but I'm just using an IDE that allows you to call methods directly on instances without having a main method-used for learning) (我的主要方法应该是在LP3上,但我只是使用一个IDE,它允许你直接在实例上调用方法而不需要用于学习的主方法)

You didn't add LP3 to the JFrame but only PanClass was added. 您没有将LP3添加到JFrame但只添加了PanClass So the LP3 's paintComponent() is not called. 所以不调用LP3paintComponent()

You can add both component (may be define proper LayoutManager eg GridLayout ) to call both paintComponent() 您可以添加两个组件(可以定义正确的LayoutManager例如GridLayout )来同时调用paintComponent()


Points you doing wrong in your program : 指出你在程序中做错了什么:

  • Naming Conventions used by you in your code, does not implies with Java. 您在代码中使用的命名约定,并不意味着使用Java。 Please do refer to Java Naming Conventions for more info. 有关详细信息,请参阅Java命名约定 Two easy things use Pascal Notions for Class Names and Camel notation for Variable Names. 两个简单的事情使用Pascal Notions用于类名称和Camel表示法用于变量名称。
  • Whenever one needs to override the method of the super class, it is BEST to keep the ACCESS SPECIFIER , the same, as much as possible. 每当需要覆盖超类的方法时,尽可能保持ACCESS SPECIFIER最好的 In your case, the Access Specifier for the paintComponent(...) method is protected and not public . 在您的情况下, paintComponent(...)方法的Access Specifier protected而不是public
  • Especially while overriding paintComponent(...) , it is BEST to call super.paintComponent(...) , in the overridden method. 特别是在重写paintComponent(...)最好在重写方法中调用super.paintComponent(...)
  • Inside your PanClass which extends JPanel you are creating a new JPanel a , which is not what you want, if you look closely. 里面你PanClass延伸JPanel要创建一个新JPanel 一个 ,这是不是你想要的,如果你仔细观察。 You want PanClass itself. 你想要PanClass本身。
  • Make it a customary habit to override, the getPreferredSize() , of the JPanel/JComponent , when ever you are extending it to any class. 当你将它扩展到任何类时,让它成为覆盖JPanel/JComponentgetPreferredSize()的习惯习惯。
  • Always define the behaviour how the JFrame needs to go off, when the X Button is pressed. 始终定义按下X按钮时JFrame需要关闭的行为。 For more Info JFrame.setDefaultCloseOperation(...) 有关更多信息JFrame.setDefaultCloseOperation(...)
  • Whenever overriding a method of super class, its best to prefix it with an Annotations. 每当覆盖超类的方法时,最好在它前面添加一个注释。 More information on Java Annotation Tutorials 有关Java Annotation Tutorials的更多信息
  • I hope after everything, a small information related to Concurrency in Swing , is worth mentioning :-) 我希望在一切之后,有关Swing中的Concurrency的小信息值得一提:-)
  • Last but not the least, the answer is very much given by @StanislavL 最后但并非最不重要的,答案是由@StanislavL给出的

Please consider this code for more reference : 请考虑此代码以获取更多参考:

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;
import java.net.URL;

public class LP3 extends JPanel
{
    public static BufferedImage image;
    public static BufferedImage image2;
    private JFrame frame = new JFrame();
    private PanClass panel = new PanClass();

    public LP3()
    {
        setOpaque(true);
        try
        {               
            image2=ImageIO.read(new URL(
                "https://encrypted-tbn1.gstatic.com/images?q=tbn:" + 
                "ANd9GcQCluuYpyVQYZuADHAYIfpkRO7SaWMn0OCM_nGH6Tr2SCFtGtE_"));     
        }
        catch (IOException e)
        {
            //Nothing
            e.printStackTrace();
        }           
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(300, 300));
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image2,0,0,this);
    }

    private void displayGUI()
    {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(true);
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 1, 5, 5));
        contentPane.add(panel);
        contentPane.add(this);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true); 
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new LP3().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class PanClass extends JPanel
{
    private static BufferedImage theimage;
    private static BufferedImage image2;

    public PanClass()
    {
        setOpaque(true);
        try
        {                 
            theimage = ImageIO.read(
                new URL(
                    "https://encrypted-tbn0.gstatic.com/" + 
                    "images?q=tbn:ANd9GcR5PNwAcLVjphhST_" + 
                    "S-K_dU0CEAuXM0g4oc1-v1r-z5VJFuemOD"));
        }
        catch (IOException e)
        {
            //Not handled.
            e.printStackTrace();
        }
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(300, 300));
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(theimage, 0, 0, this);
    }
}

EDIT : ! 编辑:!

  • I forgot one very important point the previous time regarding your use of g.draw(...) , you using null for the ImageObserver part, which in this case is your JPanel and not null 我忘记了前一次关于你使用g.draw(...)一个非常重要的观点,你对ImageObserver部分使用null ,在这种情况下是你的JPanel而不是null

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

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