![](/img/trans.png)
[英]How do I pass an object from another class into the paintComponent method?
[英]How to call my paintComponent() method in another class
我正在編寫一個 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);
}
}
我的類 DrawPicture
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 放置多個元素,但我還沒有想到那么遠。
在(重新)繪制 GUI 期間,會自動調用paintComponent (最好使用@Override
定義以檢測拼寫錯誤)。 所以它們是事件驅動的,做出反應。 它不打算直接稱為。 而是在 DrawPicture 變量或整個 JFrame ( Windows
) 上使用 JComponent 的repaint*
方法之一。
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);
}));
當actionPerformed
的代碼需要更長的時間時, invokeLater
使 GUI 表現得更靈敏(在按鈕按下期間它不會阻塞)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.