首页 > 解决方案 > 如何在另一个类中调用我的paintComponent() 方法

问题描述

我正在编写一个 Blackjack 程序,该程序有时会调用 paintComponent() 方法从文件中绘制图像。图片应该出现在 JPanel 中,但如果我尝试调用该方法JPanel-> style2b1.paintComponent(getGraphics())

它不起作用(NullPointerException),但这本身并不让我感到惊讶。我尝试过并且没有给我任何错误的唯一方法就是简单地写:paintComponent(getGraphics());

但是图像也没有显示。

就像我之前说的,我尝试了一堆不同的调用,用不同的东西作为参数,但它从来没有给出积极的结果。

我的班级视窗

package com;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class Windows extends JFrame{


JFrame test = new JFrame();
JLabel text = new JLabel("Welcome to Blackjack 21");
JLabel textAction = new JLabel("");
JLabel textBank = new JLabel("BANK");
JLabel textPlayer = new JLabel("PLAYER");
JPanel style = new JPanel();
JPanel style2 = new JPanel(new BorderLayout());
JPanel style2a = new JPanel(new BorderLayout());
JPanel style2a1 = new JPanel();
JPanel style2a2 = new JPanel();
JPanel style2b = new JPanel(new BorderLayout());
JPanel style2b1 = new JPanel();
JPanel style2b2 = new JPanel();
JPanel style3 = new JPanel();
JButton b1 = new JButton("Stand");
JButton b2 = new JButton("Hit");
JButton b3 = new JButton("Double");
JButton b4 = new JButton("Split");
JButton b5 = new JButton("Insurance");
Font font = new Font("Helvetica", 10, 20);


public Windows(){
   style.setBackground(Color.BLACK);
   style2.setBackground(Color.DARK_GRAY);
   style2a.setBackground(Color.magenta);
   style2a1.setBackground(Color.BLACK);
   style2a2.setBackground(Color.DARK_GRAY);
   style2b.setBackground(Color.magenta);
   style2b1.setBackground(Color.DARK_GRAY);
   style2b2.setBackground(Color.BLACK);
   style3.setBackground(Color.BLACK);
   style3.add(b1);
   style3.add(b2);
   style3.add(b3);
   style3.add(b4);
   style3.add(b5);
   text.setForeground(Color.WHITE);
   text.setFont(font);
   style.add(text);
   textAction.setForeground(Color.WHITE);
   textAction.setFont(font);
   style2.add(textAction);
   textBank.setFont(font);
   textBank.setForeground(Color.RED);
   style2a1.add(textBank);
   textPlayer.setForeground(Color.GREEN);
   textPlayer.setFont(font);
   style2b2.add(textPlayer);
   style2b1.setSize(this.getWidth(), 300);
   DrawPicture otaku = new DrawPicture();

   b1.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to stand. You won't receive any more cards.");
       }
   });
   b2.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to hit. You will receive another card");
//This is where I want to call my method
           paintComponents(getGraphics());
       }
   });
   b3.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to double. You will receive one last card and you have to double your bet");
       }
   });
   b4.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to split. Each deck of card will be considered as individual, however you have to double your bet ");
       }
   });
   b5.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to raise an insurance. Please double your bet.");
       }
   });


   test.add(style, BorderLayout.PAGE_START);
   test.add(style2, BorderLayout.CENTER);
   style2.add(style2a, BorderLayout.NORTH);
   style2a.add(style2a1, BorderLayout.NORTH);
   style2a.add(style2a2, BorderLayout.SOUTH);
   style2.add(style2b, BorderLayout.SOUTH);
   style2b.add(style2b1, BorderLayout.NORTH);
   style2b.add(style2b2, BorderLayout.SOUTH);
   test.add(style3, BorderLayout.PAGE_END);

   test.setSize(1000, 1000);
   test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   test.setVisible(true);
   test.setLocation(500, 0);
   test.setResizable(false);
}

}

我的班级画图

 package com;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;


public class DrawPicture extends JPanel {

File a = new File("C:\\Users\\km\\Pictures\\Cards\\red_back.png");

public void paintComponents(Graphics g){
    try {
        Image testItOut = ImageIO.read(a);
        g.drawImage(testItOut, 50, 300, this);
    }catch(IOException e){
        e.printStackTrace();
    }
}
}

我的目标是能够在 JPanel style2a2 和 JPanel style2b1 中显示许多图片(然后调整它们的大小)。我不知道我是否可以通过 JPanel 放置多个元素,但我还没有想到那么远。

我的 JFrame 看起来如何

标签: javaswingpaintcomponentdrawimage

解决方案


在 GUI 的(重新)绘制期间会自动调用一个paintComponent(更好地定义为检测错别字)。@Override所以他们是事件驱动的,反应的。它不打算被称为直。repaint*而是在 DrawPicture 变量上或整个 JFrame ( Windows)上使用 JComponent 的方法之一。

public class DrawPicture extends JPanel {

    private Image testItOut;

    // Access for setting an other image.
    public void setImage(Image testItOut) {
        this.testItOut = testItOut;
    }

    public Image getImage() {
         return testItOut;
    }

    public DrawPicture() {
        try {
            File a = new File("C:\\Users\\km\\Pictures\\Cards\\red_back.png");
            testItOut = ImageIO.read(a);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        if (testItOut != null) {
            g.drawImage(testItOut, 50, 300, this);
        }
    }
}

例如:

b5.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
         otaku.repaint(50L); // Repaint after 50 ms.
     }
});

这可以用 lambdas 缩短为:

b5.addActionListener(e -> {
   otaku.repaint(50L); // Repaint after 50 ms.
});

甚至

b5.addActionListener(e -> otaku.repaint(50L));

更现实一点:

b5.addActionListener(e ->
   SwingUtilities.invokeLater(() -> {
       File imgFile = new File("C:\\Users\\km\\Pictures\\Cards\\green_back.png");
       Image img = ImageIO.read(imgFile);
       otaku.setImage(img);
       otaku.repaint(50L);
}));

invokeLateractionPerformed当代码需要更长的时间(在按钮按下期间它不会阻塞)时,使 GUI 表现得更加响应。


推荐阅读