我已经分配了另一个在主要的JFrame中向JPanel绘制图像的类,但它不起作用。

huangapple go评论84阅读模式
英文:

I've assigned another class that draws images to a JPanel in the main JFrame, but it doesn't work

问题

I tried to create a JPanel and apply graphics to it, but only a blank screen appears.

我尝试创建一个JPanel并将图形应用于它,但只显示一个空白屏幕。

I created a new class that extends JPanel and assigned it to an arbitrary panel in the main JFrame.
(inGamePanel is JPanel in main JFrame)

我创建了一个扩展JPanel的新类,并将其分配给主JFrame中的任意面板。
(inGamePanel是主JFrame中的JPanel)

Below is the code in main JFrame

以下是主JFrame中的代码

inGamePanel = new GameStart(songList.get(currentSong), inGamePanel.getGraphics());
inGamePanel.setVisible(true);

Below is the another class that extends JPanel

以下是另一个扩展JPanel的类

package RhythmGame;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GameStart extends JPanel {
	
	private Song currentSong;
	private Graphics gameGraphics;
	private ImageIcon inGameImage;
	
	public GameStart(Song currentSong, Graphics gameGraphics) {
		this.currentSong = currentSong;
		this.gameGraphics = gameGraphics;
		paintComponent(gameGraphics);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		inGameImage = currentSong.getSongBackgroundImage();
		
		g.drawImage(inGameImage.getImage(), 0, 0, null);
	}
}

The main Jframe has a CardLayout applied to it. What's the matter?

主JFrame上应用了CardLayout。问题出在哪里?

I want to display a JPanel with an image normally applied to the screen.

我想正常显示一个带有图像的JPanel在屏幕上。

英文:

I tried to create a JPanel and apply graphics to it, but only a blank screen appears.

I created a new class that extends JPanel and assigned it to an arbitrary panel in the main JFrame.
(inGamePanel is JPanel in main JFrame)

Below is the code in main JFrame

inGamePanel = new GameStart(songList.get(currentSong), inGamePanel.getGraphics());
inGamePanel.setVisible(true);

Below is the another class that extends JPanel

package RhythmGame;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GameStart extends JPanel {
	
	private Song currentSong;
	private Graphics gameGraphics;
	private ImageIcon inGameImage;
	
	public GameStart(Song currentSong, Graphics gameGraphics) {
		this.currentSong = currentSong;
		this.gameGraphics = gameGraphics;
		paintComponent(gameGraphics);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		inGameImage = currentSong.getSongBackgroundImage();
		
		g.drawImage(inGameImage.getImage(), 0, 0, null);
	}
}

The main Jframe has a CardLayout applied to it. What's the matter?

I want to display a JPanel with an image normally applied to the screen.

答案1

得分: 0

问题在于您尝试在新的GameStart面板的构造函数中使用来自旧面板的Graphics对象(inGamePanel.getGraphics())。这是不正确的,因为Graphics对象与获取它的组件相关联,每个组件都将有自己的Graphics对象。

相反,允许Swing通过GameStart类中的paintComponent()方法提供Graphics对象。每当需要绘制组件(例如,当其被调整大小或变为可见时),Swing都会自动调用此方法。

其次,您需要将新创建的GameStart面板分配给JFrame中的inGamePanel变量。您编写的代码方式,您创建了一个新的GameStart对象,但没有将其附加到任何东西。

以下是您应该修改代码的方式:

在JFrame中:

// 创建一个新的GameStart面板
GameStart gamePanel = new GameStart(songList.get(currentSong));
// 将新面板分配给inGamePanel
inGamePanel = gamePanel;
inGamePanel.setVisible(true);

在GameStart类中:

package RhythmGame;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GameStart extends JPanel {

    private Song currentSong;
    private Image inGameImage;

    public GameStart(Song currentSong) {
        this.currentSong = currentSong;
        this.inGameImage = currentSong.getSongBackgroundImage().getImage();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(inGameImage, 0, 0, this);
    }
}

请注意,我已从构造函数中删除了Graphics对象,并在构造函数中加载了图像。传递给paintComponent的Graphics对象g用于绘制图像。

此外,在更改JFrame的内容后,请确保重新验证并重绘它。

frame.revalidate();
frame.repaint();

这确保了JFrame更新其布局管理器,在添加或删除组件后可能是必要的,然后重新绘制以显示更新的组件。

英文:

The problem is that you're trying to use the Graphics object from the old panel (inGamePanel.getGraphics()) in your constructor for the new GameStart panel. This is incorrect because the Graphics object is associated with the component from which it was obtained, and each component will have its own Graphics object.

Instead, allow Swing to provide the Graphics object via the paintComponent() method in your GameStart class. This method is automatically called by Swing every time the component needs to be painted (for example, when it is resized or becomes visible).

Secondly, you need to assign the newly created GameStart panel to the inGamePanel variable in the JFrame. The way you've written your code, you've created a new GameStart object but haven't attached it to anything.

Here is how you should modify your code:

In the JFrame:

    // create a new GameStart panel
GameStart gamePanel = new GameStart(songList.get(currentSong));
// assign the new panel to inGamePanel
inGamePanel = gamePanel;
inGamePanel.setVisible(true);

In the GameStart class:

package RhythmGame;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GameStart extends JPanel {
    
    private Song currentSong;
    private Image inGameImage;
    
    public GameStart(Song currentSong) {
        this.currentSong = currentSong;
        this.inGameImage = currentSong.getSongBackgroundImage().getImage();
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(inGameImage, 0, 0, this);
    }
}

Note that I've removed the Graphics object from the constructor, and instead load the image in the constructor. The Graphics object g that is passed to paintComponent is used to draw the image.

Also, make sure to revalidate and repaint your JFrame after changing its content.

frame.revalidate();
frame.repaint();

This ensures that the JFrame updates its layout manager, which could be necessary after adding or removing components, and then repaints itself to show the updated components.

huangapple
  • 本文由 发表于 2023年6月5日 16:29:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定