我一直在尝试绘制一只猎豹的图像,只是为了弄乱油漆方法等等,但它似乎没有工作,任何想法为什么?
我已经尝试了很多我在这里找到的东西,但似乎没有任何东西可以工作,我得到的是我的按钮,我知道它的实际涂装方法不起作用,因为它甚至没有疼痛Hello字符串。
这是主要课程:
package dev.main;
import javax.swing.JFrame;
import javax.swing.JPanel;
import dev.angora.gui.GameGUI;
@SuppressWarnings("serial")
public class Main extends JFrame {
public static JFrame p = new JFrame("Angora Realms");
public static void main (String[] args) {
new Main();
}
public Main() {
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.pack();
p.setSize(640, 800);
p.setVisible(true);
p.setLayout(null);
GameGUI g = new GameGUI();
g.createGui(p);
}
}
这是我尝试绘制的课程:
package dev.gui;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameGUI extends JFrame implements ActionListener {
public Button drawCard = new Button("Draw Card");
public Image cheetah = null;
public void createGui(JFrame p) {
drawCard.addActionListener(this);
drawCard.setBounds(20,30,80,30);
p.add(drawCard);
}
@Override
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
if (cause == drawCard) {
System.out.println("Ay");
}
}
public void paintComponent(Graphics g) {
super.paint(g);
g.drawString("Hello", 200, 50);
if (cheetah == null) {
cheetah = getImage("plains/Cheetah.png");
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(cheetah, 100, 100, 100, 300, this);
}
}
public Image getImage(String path) {
Image tempImage = null;
try {
URL imageURL = GameGUI.class.getResource(path);
tempImage = Toolkit.getDefaultToolkit().getImage(imageURL);
}
catch (Exception e) {
System.out.println("An error occured -" + e.getMessage());
}
return tempImage;
}
} '
答案 0 :(得分:1)
JFrame
之类的内容,因此很难跟踪事情的实际发展方向。没有必要对您提供的两个类中的任何一个使用extends JFrame
,事实上,这将突出显示您的错误的原因...... JFrame
没有paintComponent
方法,因此永远不会调用它,而是将GameGUI
更改为从JPanel
扩展并添加@Override
paintComponent
方法声明的开头(在它之前),然后将super.paint(g);
更改为super.paintComponent(g);
例如......
public class GameGUI extends JPanel implements ActionListener {
public Button drawCard = new Button("Draw Card");
public Image cheetah = null;
public void createGui(JFrame p) {
drawCard.addActionListener(this);
drawCard.setBounds(20,30,80,30);
p.add(drawCard);
}
@Override
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
if (cause == drawCard) {
System.out.println("Ay");
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
没有冒犯,但我不喜欢传递"容器的引用"到子组件,以便它可以这种方式添加自己。最好创建一个GameGUI
的实例,并将其添加到您想要的容器中。 GameGUI
并不需要知道或关心......恕我直言
可运行示例
pack
和setSize
,他们互相竞争。帧的内容应决定帧的大小。这是通过使用适当的布局管理器并覆盖getPreferredSize
自定义组件来完成的。setVisible
此外,您将重量级(java.awt.Button
)和轻量级(Swing / javax.swing.JFrame
)组件混合在一起......我的建议是,不要,这会导致还有一些你真正不需要处理的问题。
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
JFrame p = new JFrame("Angora Realms");
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameGUI g = new GameGUI();
p.add(g);
p.setSize(640, 800);
p.setVisible(true);
}
public class GameGUI extends JPanel implements ActionListener {
public Button drawCard = new Button("Draw Card");
public Image cheetah = null;
public GameGUI() {
drawCard.addActionListener(this);
add(drawCard);
}
@Override
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
if (cause == drawCard) {
System.out.println("Ay");
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello", 200, 50);
if (cheetah == null) {
cheetah = getImage("plains/Cheetah.png");
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(cheetah, 100, 100, 100, 300, this);
}
}
public Image getImage(String path) {
Image tempImage = null;
try {
URL imageURL = GameGUI.class.getResource(path);
tempImage = Toolkit.getDefaultToolkit().getImage(imageURL);
} catch (Exception e) {
System.out.println("An error occured -" + e.getMessage());
}
return tempImage;
}
}
}