我试图将背景图像添加到我的框架中,但是我没有做任何工作。
我设计了一种老虎机,该老虎机由添加到容器中的几个面板组成。现在,我正在尝试为框架添加漂亮的背景。
我尝试使用绘画方法。但是,由于我已经在使用绘制方法绘制卷轴图像,因此它无法在背景上运行。
我还尝试添加JLabel,但是当我这样做时,它会覆盖所有内容或被覆盖,这取决于我如何称呼它。以下是我的代码;任何帮助都感激不尽:
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
public class SlotMachine extends JFrame {
private Container c = getContentPane();
private ImageIcon handleIcon = new ImageIcon("starWars/slot-handle.png");
private ImageIcon quitIcon = new ImageIcon("starWars/quit2.jpg");
private ImageIcon logoIcon = new ImageIcon("starWars/logo3.jpg");
private ImageIcon BG = new ImageIcon("space.jpg");
private JButton spin = new JButton("Spin", handleIcon);
private JButton quit = new JButton("Quit", quitIcon);
private JLabel logo = new JLabel(logoIcon);
private JLabel bankTotal = new JLabel("Empire Total");
private JLabel bankLabel = new JLabel("$1000.00");
private JLabel playerLabel = new JLabel("$1000.00");
private JLabel playerTotal = new JLabel("Rebellion Total");
private Font newFont = new Font("DialogInput",Font.ITALIC, 25);
private JPanel logoPanel = new JPanel(new BorderLayout());
private JPanel moneyPanel = new JPanel(new GridLayout(1, 3, 5, 5));
private JPanel imagePanel;
private JPanel mainPanel = new JPanel(new BorderLayout());
private JPanel bankPanel = new JPanel(new GridLayout(2, 1, 5, 5));
private JPanel playerPanel = new JPanel(new GridLayout(2, 1, 5, 5));
private JPanel panel = new JPanel(new FlowLayout());
private SlotMachine.ReelPanel reel1 = new SlotMachine.ReelPanel();
private SlotMachine.ReelPanel reel2 = new SlotMachine.ReelPanel();
private SlotMachine.ReelPanel reel3 = new SlotMachine.ReelPanel();
private AudioPlayer audioPlayer = AudioPlayer.player;
private AudioDataStream continuousMusic;
private AudioDataStream winMusic;
private AudioDataStream force;
private AudioDataStream force2;
//private AudioDataStream intro;
private ContinuousAudioDataStream audioLoop;
private static final int DELAY = 1000;
private static final double FUNDS = 1000.00;
private static final float PRICE = 1;
private int timerCounter = 0;
private double bank = FUNDS;
private double playerMoney = 1000.00;
private Timer timer = new Timer(DELAY, new SlotMachine.TimeHandler());
public SlotMachine() {
try {
FileInputStream inputStream = new FileInputStream(new File("cantina4.wav"));
AudioStream audioStream = new AudioStream(inputStream);
AudioData audioData = audioStream.getData();
continuousMusic = new AudioDataStream(audioData);
audioLoop = new ContinuousAudioDataStream(audioData);
inputStream = new FileInputStream(new File("Cheer.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
winMusic = new AudioDataStream(audioData);
inputStream = new FileInputStream(new File("forceNN.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
force = new AudioDataStream(audioData);
inputStream = new FileInputStream(new File("force2NN.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
force2 = new AudioDataStream(audioData);
} catch (Exception e) {
e.printStackTrace();
}
audioPlayer.start(force);
// Set the font
spin.setFont(newFont);
quit.setFont(newFont);
bankLabel.setFont(newFont);
bankTotal.setFont(newFont);
playerLabel.setFont(newFont);
playerTotal.setFont(newFont);
// implements start button
spin.setVerticalTextPosition(SwingConstants.BOTTOM);
spin.setHorizontalTextPosition(SwingConstants.CENTER);
spin.setBackground(Color.GREEN);
spin.setForeground(Color.WHITE);
spin.setPreferredSize(new Dimension(100, 370));
spin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
audioPlayer.stop(force);
audioPlayer.stop(force2);
timer.start();
reel1.startAnimation();
reel2.startAnimation();
reel3.startAnimation();
spin.setEnabled(false);
audioPlayer.start(audioLoop);
}
});
// implements quit button
quit.setBackground(Color.RED);
quit.setForeground(Color.WHITE);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
spin.setEnabled(true);
reel1.stopAnimation();
reel2.stopAnimation();
reel3.stopAnimation();
timer.stop();
audioPlayer.stop(continuousMusic);
audioPlayer.stop(audioLoop);
audioPlayer.stop(winMusic);
timerCounter = 0;
audioPlayer.stop(force);
audioPlayer.start(force2);
imagePanel.repaint(); // without this call for repaint, if you press quit but then choose to cancel
// the curent image and the next image would sometimes overlap this repaint may change the images but they do not overlap.
if (JOptionPane.showConfirmDialog(SlotMachine.this,
"Are you sure you want to quit?", "Quit Option",
JOptionPane.OK_CANCEL_OPTION) == 0) {
audioPlayer.start(force2);
System.exit(0);
}
}
});
// create image panel
imagePanel = new JPanel(new GridLayout(1, 3, 15, 15));
imagePanel.setBackground(Color.WHITE);
imagePanel.add(reel1);
imagePanel.add(reel2);
imagePanel.add(reel3);
// create a panel to hold bank values
bankTotal.setForeground(Color.WHITE);
bankLabel.setForeground(Color.WHITE);
bankPanel.setBackground(Color.GRAY);
bankPanel.add(bankTotal);
bankPanel.add(bankLabel);
// panel to hold player values
playerTotal.setForeground(Color.WHITE);
playerLabel.setForeground(Color.WHITE);
playerPanel.setBackground(Color.GRAY);
playerPanel.add(playerTotal);
playerPanel.add(playerLabel);
// create a panel to add bank and player panels and quit button
//moneyPanel.setBackground(Color.BLACK);
moneyPanel.add(bankPanel);
moneyPanel.add(playerPanel);
moneyPanel.add(quit);
moneyPanel.setOpaque(false);
// this panel adds the reel panel and spin button
panel.setPreferredSize(new Dimension(650, 350));
//panel.setBackground(Color.BLACK);
panel.add(imagePanel);
panel.add(spin);
panel.setOpaque(false);
// create the logo panel
logoPanel.add(logo);
//logoPanel.setBackground(Color.BLACK);
logoPanel.setOpaque(false);
mainPanel.add(logoPanel, BorderLayout.NORTH);
mainPanel.add(panel, BorderLayout.CENTER);
mainPanel.add(moneyPanel, BorderLayout.SOUTH);
mainPanel.setOpaque(false);
//////////////////////////////////// background ???????????????????
/// I could just set backgroung black but i want to add a image
add(mainPanel, BorderLayout.CENTER);
setTitle("Slot Machine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(950, 750);
setResizable(false);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new SlotMachine();
}
public class ReelPanel extends JPanel {
private final static String IMAGE_NAME = "starWars/icon";
protected ImageIcon images[];
private int currentImage = 0;
private final int ANIMATION_DELAY = 150;
private final int TOTAL_IMAGES = 12;
private int width;
private int height;
private Timer animationTimer = new Timer(ANIMATION_DELAY, new SlotMachine.ReelPanel.TimerHandler());
private int index;
public ReelPanel() {
try {
images = new ImageIcon[TOTAL_IMAGES];
for (int count = 0; count < images.length; count++) {
images[ count] = new ImageIcon(IMAGE_NAME + (count + 1) + ".jpg");
}
width = images[ 1].getIconWidth();
height = images[ 1].getIconHeight();
currentImage = 0;
index = 0;
animationTimer.setInitialDelay(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
images[ currentImage].paintIcon(this, g, 0, 0);
if (animationTimer.isRunning()) {
currentImage = (int) (Math.random() * TOTAL_IMAGES);
}
}
public void startAnimation() {
animationTimer.start();
}
public void stopAnimation() {
animationTimer.stop();
}
public int getIndex() {
return index;
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
private class TimerHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
repaint();
index = currentImage;
}
}
}
private class TimeHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
audioPlayer.stop(winMusic);
++timerCounter;
if (timerCounter == 2) {
reel1.stopAnimation();
} else if (timerCounter == 3) {
reel2.stopAnimation();
} else if (timerCounter == 4) {
reel3.stopAnimation();
audioPlayer.stop(continuousMusic);
audioPlayer.stop(audioLoop);
timerCounter = 0;
timer.stop();
spin.setEnabled(true);
if (reel1.getIndex() == reel2.getIndex() && reel1.getIndex() == reel3.getIndex()) {
if (playerMoney > 0) {
playerMoney += bank;
} else {
playerMoney = bank;
}
bank = FUNDS;
winMusic.reset();
audioPlayer.start(winMusic);
} else {
bank += PRICE;
playerMoney -= PRICE;
}
bankLabel.setText("$" + bank + 0);
playerLabel.setText("$" + playerMoney + 0);
if (playerMoney <= 0) {
JOptionPane.showMessageDialog(SlotMachine.this,
"You are out of funds. GAME IS OVER", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
}
}
}
问题内容: 此处已触及该主题,但未提供有关如何创建3D图并在平面中以指定高度插入图像的指示。 因此,要提出一个简单且可复制的案例,假设我使用以下代码创建了一个3D图: 在视觉上,我们有: 在级别上,这里是避免重叠的视觉偏移, 我想插入一张图像, 表示曲线显示特定值的元素。 怎么做? 在此示例中,我并不关心元素与其值之间的完美匹配,因此请随时上传您喜欢的任何图像。另外,如果对匹配不满意,有没有办法让
问题内容: 我知道这个问题已经被问过很多次了,但是我觉得这个问题没有得到正确回答。 我想在Java GUI中将图像用作背景 我尝试了以下方法: 我是Java新手,请耐心等待 问题答案: 放一个 在您的jframe构造函数中
问题内容: 这可能吗?以下是我尝试过的方法,但它完全用黑色填充了圆圈。 问题答案: SVG元素的图像填充是通过SVG模式实现的。
问题内容: 这可能吗?以下是我尝试过的方法,但它完全用黑色填充了圆圈。 问题答案: SVG元素的图像填充是通过SVG模式实现的。
本文向大家介绍C#怎么给PDF添加背景图片,包括了C#怎么给PDF添加背景图片的使用技巧和注意事项,需要的朋友参考一下 今天要实现的是给PDF文件添加图片背景这个功能。PDF是近年来最流行的文件之一,无论是办公还是日常生活中都经常会用到,很多时候,PDF文件的背景色都是白色,看多了难免觉得累,更换PDF的背景不仅可以让眼睛看起来更舒服,还可以让PDF文件看上去更美观。如何实现?作为一名程序猿,当然
问题内容: 我为设置背景图片有一个小问题。 这是我在网站上获得的html: 这是CSS: 我不知道为什么按钮的背景仍然是白色。 问题答案: 令人惊讶的是,这里没有答案解决或提及实际问题。 该CSS选择器说:“给我一个元素与ID 里面 一个元素”,就像这样: 但是,您想要的是 带有* id 的元素。而选择器将是(请注意 button 和 #rock 之间缺少的空间)。 * 正如@Greg已经提到的: