主要编辑:我尝试了一个不同的方法与1类和实现运行尝试和打印的“鼹鼠”。我再也没有二等舱了。新的问题是使BufferedImage userInterface打印在所需的坐标,然后打印“鼹鼠”在正确的坐标。当下一个鼹鼠被画出来时,鼹鼠就会消失。
当前的问题:
userInterface是单独的(在代码中它下面没有for-循环),那么它就会出现。如果我将坐标从0,0更改为0,它就不再出现。userInterface就完全停止出现了,即使它的弦仍然是0,0,并且鼹鼠被打印在随机坐标上。这里的问题是UI不再出现,而且marios一直在打印,而以前的那些已经消失了。我正在为我的计算机编程课编写一个疯狂的鼹鼠游戏,然而,我一直有相当大的麻烦,使“鼹鼠”的图像出现和消失。
首先,我不想对鼹鼠使用任何类型的JButton系统。我看过它的使用情况,但如果可能的话,我想使用我的..png。
第二,我的结论是我不知道自己在做什么。一个主要的问题是,我也不知道如何组织这个计划。鼹鼠将以缓慢增长的速度出现,角色将在屏幕上点击。如果用户的鼠标点击在其中一个鼹鼠的区域内,当它们上升时,用户的分数会增加。我不知道我应该上一节课还是上两节课(我见过有人在一节课上这样做)。我也不知道我要对更新分数做些什么。我想要的分数更新独立于重新油漆鼹鼠也。
注意:BufferedImage mario之所以叫马里奥,是因为我正在制作“Wack”。鼹鼠是我画的马里奥的形象,音乐/效果都将来自马里奥。也请原谅我可怕的进口商品。在过去的几个小时里,随着我尝试不同的事情,它们的数量有所增加。我再也不知道该做什么了,它们会被清理干净。
主要班:
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import javax.swing.Timer;
public class WhackAMario extends JFrame implements Runnable
{
int xCoord[] = new int[] {25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609};
int yCoord[] = new int[] {275, 275, 275, 275, 275, 395, 395, 395, 395, 395, 515, 515, 515, 515, 515, 635, 635, 635, 635, 635,};
Random coordGen = new Random();
int x,y,element;
BufferedImage userInterface, mario;
private Thread thread;
private static JFrame frame = new JFrame();
private static Graphics g;
private static Graphics2D g2;
public void randomCoord()
{
element = coordGen.nextInt(20);
x = xCoord[element];
y = yCoord[element];
}
public void run()
{
g = frame.getGraphics();
try
{
userInterface = ImageIO.read(new File("UserInterface.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
g.drawImage(userInterface, 0, 0, null);
for(int i=0; i<25; i++)
{
randomCoord();
try
{
mario = ImageIO.read(new File("Mario Up.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
g.drawImage(mario, x, y, null);
try
{
Thread.sleep(1000);
repaint();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
final WhackComponent whackComponent = new WhackComponent();
class Mouse implements MouseListener
{
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(796, 818);
frame.setVisible(true);
WhackAMario whack = new WhackAMario();
whack.thread = new Thread(whack);
whack.thread.start();
MouseListener listener = new Mouse();
frame.addMouseListener(listener);
//Timer t = new Timer(100, whackComponent);
//t.start();
//
// ImageIcon img = new ImageIcon("Hole.png");
// JLabel label1 = new JLabel(img);
// JPanel panel = new JPanel();
// panel.setLayout(null);
//
// label1.setLocation(400, 400);
//frame.add(label1);
//frame.setVisible(true);
}
}“专业编辑”前的主修班
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import javax.swing.Timer;
public class WhackAMario extends JFrame implements Runnable
{
int xCoord[] = new int[] {25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609};
int yCoord[] = new int[] {275, 275, 275, 275, 275, 395, 395, 395, 395, 395, 515, 515, 515, 515, 515, 635, 635, 635, 635, 635,};
Random coordGen = new Random();
int x,y,element;
BufferedImage userInterface, mario;
private Thread thread;
private static JFrame frame = new JFrame();
private static Graphics g;
private static Graphics2D g2;
public void randomCoord()
{
element = coordGen.nextInt(20);
x = xCoord[element];
y = yCoord[element];
}
public void run()
{
g = frame.getGraphics();
// try
// {
// userInterface = ImageIO.read(new File("UserInterface.png"));
// }
// catch(IOException e)
// {
// e.printStackTrace();
// }
// g.drawImage(userInterface, 0, 50, null);
// for(int i=0; i<25; i++)
// {
// randomCoord();
// try
// {
// mario = ImageIO.read(new File("Mario Up.png"));
// }
// catch(IOException e)
// {
// e.printStackTrace();
// }
// g.drawImage(mario, x, y, null);
// try
// {
// Thread.sleep(1000);
// repaint();
// }
// catch (InterruptedException e)
// {
// e.printStackTrace();
// }
// }
}
public static void main(String[] args)
{
final WhackComponent whackComponent = new WhackComponent();
class Mouse implements MouseListener
{
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(796, 818);
frame.setVisible(true);
frame.add(whackComponent);
// WhackAMario whack = new WhackAMario();
// whack.thread = new Thread(whack);
// whack.thread.start();
MouseListener listener = new Mouse();
frame.addMouseListener(listener);
//Timer t = new Timer(100, whackComponent);
//t.start();
//
// ImageIcon img = new ImageIcon("Hole.png");
// JLabel label1 = new JLabel(img);
// JPanel panel = new JPanel();
// panel.setLayout(null);
//
// label1.setLocation(400, 400);
//frame.add(label1);
//frame.setVisible(true);
}
}组件类:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.concurrent.TimeUnit;
public class WhackComponent extends JComponent
{
int xCoord[] = new int[] {25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609, 25, 171, 317, 463, 609};
int yCoord[] = new int[] {275, 275, 275, 275, 275, 395, 395, 395, 395, 395, 515, 515, 515, 515, 515, 635, 635, 635, 635, 635,};
BufferedImage userInterface, mario;
Random coordGen = new Random();
Thread thread;
int element;
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
try
{
userInterface = ImageIO.read(new File("UserInterface.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
g2.drawImage(userInterface, null, 0, 0);
for(int i=0; i<25; i++)
{
element = coordGen.nextInt(20);
try
{
mario = ImageIO.read(new File("Mario Up.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
g2.drawImage(mario, null, xCoord[element], yCoord[element]);
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void enterMove()
{
}
public void releaseMove()
{
}
public void actionPerformed(ActionEvent event)
{
}
}发布于 2014-05-23 08:16:41
它应该是这样工作的:您在paintComponent(Graphics)中提供绘图逻辑,当系统检测到需要刷新屏幕时,系统会给您回电话。在Swing应用程序中,触发器是用户与计算机交互的提示,例如,按一个键或单击鼠标,而在应用程序中,您需要一个具有自己的主循环的游戏引擎,该引擎显示/隐藏鼹鼠,与音乐引擎交互,并根据世界的状态处理输入事件。在Swing中,通过调用.invalidate(),布局层次结构将失效。
请注意,JButton和其他Swing组件只是一种方便的方式来绘制东西(特别是在布局管理器的帮助下wrt屏幕大小)和处理事件,但是您当然可以使用一个简单的JComponent来编写一个简单的游戏,就像在您的示例中一样(我认为这可能会有所帮助)。
随机提示:paintComponent必须是快速的,我的意思是非常快的,所以您不能从文件系统中读取PNG并将其转换为位图。当游戏初始化时,加载必须精确地发生一次。
https://stackoverflow.com/questions/23823686
复制相似问题