EkLuthra:
私は基本的にいくつかのゲームのタイトル画面ことになっているプログラムを持っています。背景はGIF(それが問題に貢献していた場合、私はよく分からない)である、と私はいくつか持っている必要がありますJButtons
私は実際にゲームを実行することを可能とすることを。問題があるということJButton
だけを示しアップは、時にはそれ(その時、一瞬のために)、それ以外の場合は、目に見えない私がいたときにホバー上。それは、うまく動作しますゲームに行くとすべてが、それだけで目に見えないのです。
私はこの問題は、私はGIFと同様に、使用しているという事実であるかどうかを確認するために試したpaintComponent()
私はJPEGを使用する場合、それだけでは表示されませんでした、が、方法を。
ここで、コードは次のとおりです。
public class TestingGrounds extends JFrame{
//declarations
JButton snakeButton;
JPanel snakeButtonPanel;
JFrame window;
Container con;
TitleScreenHandler tsHandler = new TitleScreenHandler();
//constructor
public TestingGrounds(){
//main JFrame
window = new JFrame("Title Screen");
window.add(new ImagePanel());
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(1831, 1030);
window.setVisible(true);
window.setLocationRelativeTo(null);
window.setLayout(null);
con = window.getContentPane();
//panel for the button to go to the snake game
snakeButtonPanel = new JPanel();
snakeButtonPanel.setBounds(100,100,600,150);
//button to go to snake game
snakeButton = new JButton("Snake");
snakeButton.setBackground(Color.BLACK);
snakeButton.setForeground(Color.WHITE);
snakeButton.setFont(new Font("Times New Roman", Font.ITALIC, 30));
snakeButton.addActionListener(tsHandler);
snakeButton.setFocusPainted(false);
//adding button to panel
snakeButtonPanel.add(snakeButton);
//adding panel to container
con.add(snakeButtonPanel);
//setting the panel as visible
snakeButtonPanel.setVisible(true);
}
//main method for running constructor
public static void main(String[] args) {
new TestingGrounds();
}
//what to do if the button is pressed
public class TitleScreenHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
//goes to main game screen if start button is pressed
new SnakeGame();
}
}
}
//class for using the gif
class ImagePanel extends JPanel {
Image image;
public ImagePanel() {
image = Toolkit.getDefaultToolkit().createImage("C:/Users/eklut/Desktop/Coding/ICS4U1/src/graphicsstuff/snek/source.gif");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
申し訳ありませんが、私はそれは、コードのかなりの量だけど、私は私が正確にわから問題が由来どこじゃないとして、それをすべて表示する必要があるような気がします。
私は、GIFの上に表示されるまでボタンを期待し、それは他の方法の周りが起こっているようにそれがほとんどらしいです
アンドリュー・トンプソン:
このMCVEは、それがコメントでコードと表情を経由する方が良いですので、多くの変更が含まれています。
注コードが使用するアニメーション BGとしてGIFを。私はそれが明示的に使用されたと言うでしょう、それはGIFあったことを実証しているが、真実は、例えば画像のページのみアニメーションGIFが含まれていることです。形式はGIFよりも半透明性と多くの色をサポートしていますPNG与えられ、他の何かのために非常に良いではありません。
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import javax.swing.*;
import java.net.URL;
//public class TestingGrounds extends JFrame {
public class TestingGrounds {
//declarations
JButton snakeButton;
JPanel snakeButtonPanel;
JFrame window;
TitleScreenHandler tsHandler = new TitleScreenHandler();
//constructor
public TestingGrounds() {
//main JFrame
window = new JFrame("Title Screen");
try {
JPanel imagePanel = new ImagePanel();
imagePanel.setLayout(new BorderLayout());
window.add(imagePanel);
//window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Don't guess sizes needed. They will be wrong on other
// PLAFs or platforms.
//window.setSize(850, 600);
// should be last
//window.setVisible(true);
window.setLocationRelativeTo(null);
//window.setLayout(null);
//con = window.getContentPane();
//panel for the button to go to the snake game
snakeButtonPanel = new JPanel(new GridBagLayout());
snakeButtonPanel.setOpaque(false);
//snakeButtonPanel.setBounds(100, 100, 600, 150);
//button to go to snake game
snakeButton = new JButton("Snake");
snakeButton.setMargin(new Insets(10,40,10,40));
snakeButton.setBackground(Color.BLACK);
snakeButton.setForeground(Color.WHITE);
snakeButton.setFont(new Font("Times New Roman", Font.ITALIC, 30));
snakeButton.addActionListener(tsHandler);
snakeButton.setFocusPainted(false);
//adding button to panel
snakeButtonPanel.add(snakeButton);
//adding panel to container
//con.add(snakeButtonPanel);
// Adding the container to imagePanel
imagePanel.add(snakeButtonPanel);
//setting the panel as visible
snakeButtonPanel.setVisible(true);
window.pack();
window.setVisible(true);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
//main method for running constructor
public static void main(String[] args) {
new TestingGrounds();
}
//what to do if the button is pressed
public class TitleScreenHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
//goes to main game screen if start button is pressed
//new SnakeGame();
JOptionPane.showMessageDialog(snakeButton, "Snake Game");
}
}
}
//class for using the gif
class ImagePanel extends JPanel {
Image image;
public ImagePanel() throws MalformedURLException {
image = Toolkit.getDefaultToolkit().createImage(
new URL("https://i.stack.imgur.com/OtTIY.gif"));
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 1);
try {
mt.waitForAll();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
@Override
public Dimension getPreferredSize() {
// this allows us to pack() the window around the image
return (new Dimension(image.getWidth(this), image.getHeight(this)));
}
}