[英]How to make a button play a sound as it launches another activity when pressed?
[英]I want Game to play when Play Button is Pressed from another frame
我有三节课。 游戏的菜单框架,从中按下播放按钮时,应该播放包含游戏的框架类。主类创建菜单类的对象,但是问题是当我按下播放按钮时,它变成白色并且经过一段时间它说您的分数为0且游戏结束。 请帮助我解决我尝试过的所有问题。
public class GameFrame extends JFrame implements KeyListener {
protected int x;
protected int y;
private int a;
private int b;
protected int c;
protected int d;
protected int f;
protected int k;
private int h;
private int i;
private int s;
GameFrame() {
/*
* setSize(760,700);
* setLocation(200,25);
* setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* setResizable(false);
* setVisible(true);
*/
setLayout(null);
c = 500;
d = 530;
a = 1;
b = 1;
x = 50;
y = 250;
f = 50;
k = 700;
h = 1;
i = -1;
s = 1;
addKeyListener(this);
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics g1 = (Graphics) g;
g1.setColor(Color.DARK_GRAY);
g1.fillRect(0, 0, 1200, getHeight());
/*
* g1.setColor(Color.blue);
* g1.fillRect(0, 0, 850, getHeight());
* g1.setColor(Color.green);
* g1.fillRect(0, 0, 845, getHeight());
* g1.setColor(Color.yellow);
* g1.fillRect(0, 0, 840, getHeight());
* g1.setColor(Color.red);
*/
g1.fillRect(0, 0, 835, getHeight());
g1.setColor(Color.gray);
g1.fillRect(0, 0, 830, getHeight());
g1.setColor(Color.BLACK);
g1.fillRect(0, 0, 825, getHeight());
g1.setColor(Color.red);
g1.fillOval(x, y, 20, 20);
g1.setColor(Color.GREEN);
g1.fillOval(k, f, 20, 20);
g1.setColor(Color.blue);
g1.fillRect(c, d, 100, 15);
g1.setColor(Color.red);
g1.fillRect(300, 300, 90, 20);
g1.setColor(Color.pink);
g1.drawRect(0, 80, 760, 3);
g1.setColor(Color.orange);
g1.setFont(new Font(null, Font.BOLD, 20));
g1.drawString("Your Score", 500, 60);
g1.setFont(new Font(null, Font.BOLD, 25));
g1.drawString("Credit:", 50, 60);
g1.setFont(new Font(null, Font.BOLD, 20));
g1.drawString("Mohsin Hussain", 150, 60);
g1.drawString(String.valueOf(points()), 650, 60);
}
// Here is Run Method. From This Method Ball Will Move in Different Direction After Collision
public void Run() {
if (c < 0)
c = 1;
if (c > 740)
c = 675;
if (k < 0)
i = s;
if (k > 700)
i = -s;
k = k + i;
if (f < 80)
h = s;
if ((f + 20 > d && k + 20 > c && f < d + 6 && k < c + 60))
h = -s;
f = f + h;
if (x < 0)
a = s;
if (x > 700)
a = -s;
x = x + a;
if (y < 80)
b = s;
if ((x > 300 && x < 380) && (y == 300) && b < 0) {
b = s;
}
if ((x > 300 && x < 380) && (y == 300) && b > 0) {
b = -s;
}
System.out.println(x);
if ((y + 20 > d && x + 20 > c && y < d + 6 && x < c + 60))// Control the Direction as well
// as Collision of the Grey Ball
// From Rectangle
b = -s;
y = y + b;
finish();// Call the Method of Finish
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
Run();
} // End Bracket of Run Method
// Method of Points that Control the Score as well as Increase the Speed
public int points() {
if ((y + 20 > d && x + 20 > c && y < d + 6 && x < c + 60)
|| (f + 20 > d && k + 20 > c && f < d + 6 && k < c + 60))
s += 1;
return s - 1;
} // /End Bracket of Points Method
// Method of Finish that Control Game Over Logic
private void finish() {
if (y > getHeight() - 20 || f > getHeight() - 20) {
// Logic of Set Time at the End of the Game
/*
* GregorianCalendar date = new GregorianCalendar();
*
* int day = date.get(Calendar.DAY_OF_MONTH); int month = date.get(Calendar.MONTH); int
* year = date.get(Calendar.YEAR);
*
* int second = date.get(Calendar.SECOND); int minute = date.get(Calendar.MINUTE); int
* hour = date.get(Calendar.HOUR);
*
* System.out.println("Ending Date of Game is "+day+"/"+(month+1)+"/"+year);
* System.out.println("Ending Time of Game is "+hour+" : "+minute+" : "+second);
*/
JOptionPane.showMessageDialog(this, "your Score is " + points(), "Game Over",
JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}// End Bracket of If Statement
}
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
c -= 25;
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
c += 25;
} // End of KeyPressed
public void keyReleased(KeyEvent arg0) {} // End of KeyReleased
public void keyTyped(KeyEvent arg0) {}// End of KeyTyped
}
包含游戏菜单的框架,当我们按下播放按钮时,游戏应从该菜单开始。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameMenu extends JFrame {
private JButton play;
private JButton exit;
private JButton about;
private JButton credit;
private ImageIcon img;
private JLabel imgLabel;
private JPanel panel;
private float h = (float) 0.333;
private float b = (float) 0.600;
private float s = 1;
boolean runCheck = false;
public GameFrame frame;
GameMenu() {
setLayout(null);
setResizable(false);
panel = new JPanel();
panel.setLayout(null);
panel.setSize(760, 700);
panel.setBackground(Color.getHSBColor(h, s, b));
add(panel);
play = new JButton("PLAY");
play.setSize(100, 50);
play.setLocation(360, 400);
panel.add(play);
about = new JButton("ABOUT");
about.setSize(play.getSize());
about.setLocation(play.getX(), play.getY() + play.getHeight() + 50);
panel.add(about);
exit = new JButton("EXIT");
exit.setSize(play.getSize());
exit.setLocation(about.getX() + about.getWidth() + 50, about.getY());
panel.add(exit);
credit = new JButton("CREDIT");
credit.setSize(play.getSize());
credit.setLocation(about.getX() - about.getWidth() - 50, about.getY());
panel.add(credit);
img = new ImageIcon("C:\\Users\\Ghulam Haider\\Downloads\\b.jpg");
imgLabel = new JLabel(img);
imgLabel.setSize(imgLabel.getPreferredSize());
imgLabel.setLocation(230, 45);
panel.add(imgLabel);
mouseListener mouse = new mouseListener();
Action action = new Action();
play.addMouseListener(mouse);
}
private class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {}
}
private class mouseListener implements MouseListener, Runnable {
@Override
public void mouseClicked(MouseEvent get) {}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == play) {
frame = new GameFrame();
frame.setSize(760, 700);
frame.setLocation(200, 25);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.Run();
// end of while
}
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void run() {
frame.Run();
// end of while
}
}
}
最后是调用菜单框架的主类。
import javax.swing.JFrame;
public class RunGame {
public static void main(String args[]) {
GameMenu menu = new GameMenu();
menu.setSize(760, 700);
menu.setLocation(200, 25);
menu.setVisible(true);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
但是问题是,当我按下播放按钮时,它变成白色,一段时间后它说您的得分为0,游戏结束了。
您正在EDT上调用GameFrame.Run
方法GameFrame.Run
是单线程的,所有绘画,事件等都在EDT上运行。 如果您有阻止它的东西(例如,像GameFrame.Run
方法一样在EDT上调用Thread.sleep),那么这些都不会发生。 而是创建一个Thread
或使用一个计时器
其他一些建议:
JFrame
-通过重写paintComponent
方法来绘制到JPanel
。 null
布局。 为您要的布局选择适当的LayoutManager 。 ActionListener
而不是MouseListener
mouselistener
),而方法名应以小写字母开头。 避免与API名称发生任何潜在的命名冲突 您的包含finish()的if语句不包含在方括号中。 因此,在看到条件后,它简单地移动并运行finish()
if((y+20>d&&x+20>c&&y<d+6&&x<c+60))//Control the Direction as well as Collision of the Grey Ball From Rectangle
b=-s;
y=y+b;
finish();//Call the Method of Finish
因此,每次调用run方法时,它都会终止,因为也总是调用finish()。
尝试围绕if语句,以便仅在该语句为true时才调用finish()。 或编写一个单独的if语句,在满足条件时调用finish()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.