Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示和删除JFrame中的BuffereImage(猛击一摩尔)_Java - Fatal编程技术网

Java 显示和删除JFrame中的BuffereImage(猛击一摩尔)

Java 显示和删除JFrame中的BuffereImage(猛击一摩尔),java,Java,主要编辑:我尝试了一种不同的方法,使用1个类和Runnable的实现来打印“moles”。我再也没有第二节课了。新版本是在所需坐标处打印buffereImage用户界面,然后在正确坐标处随机打印“moles”。当绘制下一个摩尔时,该摩尔应消失 当前的问题: 如果用户界面是单独的(代码中没有在其下方看到的for循环),则它将出现。如果我将坐标从0,0更改,它将不再显示 当我添加了打印“moles”的for循环(也称为mario,因此是Whack-a-mario)时,用户界面将完全停止显示,即使其坐

主要编辑:我尝试了一种不同的方法,使用1个类和Runnable的实现来打印“moles”。我再也没有第二节课了。新版本是在所需坐标处打印
buffereImage用户界面
,然后在正确坐标处随机打印“moles”。当绘制下一个摩尔时,该摩尔应消失

当前的问题:

  • 如果
    用户界面
    是单独的(代码中没有在其下方看到的for循环),则它将出现。如果我将坐标从0,0更改,它将不再显示

  • 当我添加了打印“moles”的for循环(也称为mario,因此是Whack-a-mario)时,
    用户界面
    将完全停止显示,即使其坐标仍然为0,0,并且moles在随机坐标下打印。这里的问题是,用户界面不再出现,马里奥牌游戏继续被打印,而之前的版本也不会消失


  • 我正在为我的计算机编程课编写一个“打鼹鼠”游戏,然而,我一直很难让“鼹鼠”的图像出现和消失

    首先,我不想对鼹鼠使用任何类型的JButton系统。我见过它被使用,但如果可能的话,我想使用我的.png

    第二,我的结论是我不知道自己在做什么。一个主要的问题是,我也不知道如何组织这个项目。摩尔将以缓慢增加的速度出现,角色将在屏幕上单击。如果用户的鼠标点击在其中一个痣的区域内,则用户的分数将增加。我不知道我应该上一节课还是两节课(我见过人们在一节课上做这件事)。我也不知道我要怎么更新分数。我希望分数的更新独立于痣的重新粉刷

    注意:
    bufferedimagemario
    之所以被称为mario,是因为我正在制作“Wack-A-mario”。鼹鼠是我画的马里奥的图像,音乐/效果都来自马里奥。也请原谅我可怕的进口。在过去的几个小时里,随着我尝试各种各样的东西,它们的数量增加了。我再也不知道该做什么了,他们会被清理干净的


    主要类别:

    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);
        }
    }
    
    导入java.awt.event.MouseListener;
    导入java.awt.event.MouseEvent;
    导入javax.swing.JFrame;
    导入java.awt.Color;
    导入javax.swing.*;
    导入javax.swing.border.*;
    导入javax.swing.*;
    导入javax.swing.border.*;
    导入java.awt.*;
    导入java.util.*;
    导入java.awt.event.*;
    导入java.lang.reflect.*;
    导入javax.imageio.*;
    导入java.io.*;
    导入java.awt.image.*;
    导入javax.swing.Timer;
    公共类WhackAMario扩展JFrame实现Runnable
    {
    int xCoord[]=新的int[]{25171317463609,25171317463609,251713174636363609};
    int yCoord[]=新int[]{275,275,275,395,395,395,395,515,515,515,515,515,635,635,635,635,};
    Random coordGen=新的Random();
    int x,y,元素;
    BuffereImage用户界面,mario;
    私有线程;
    私有静态JFrame=新JFrame();
    私有静态图形;
    专用静态图形2d-g2;
    公共合作
    {
    元素=coordGen.nextInt(20);
    x=xCoord[元素];
    y=yCoord[元素];
    }
    公开募捐
    {
    g=frame.getGraphics();
    尝试
    {
    userInterface=ImageIO.read(新文件(“userInterface.png”);
    }
    捕获(IOE异常)
    {    
    e、 printStackTrace();
    }
    g、 drawImage(用户界面,0,0,null);
    
    对于(inti=0;i它应该是这样工作的:在
    paintComponent(图形)中提供绘图逻辑
    ,当系统检测到屏幕需要刷新时,会给您回电话。在Swing应用程序中,触发器通常是用户与计算机交互,例如通过按键或单击鼠标,而在应用程序中,您需要一个带有自己主循环的游戏引擎,该引擎显示/隐藏鼹鼠,与m进行交互usic引擎并根据世界状态处理输入事件。在Swing中,通过调用
    .invalidate()
    ,布局层次结构将失效

    请注意,
    JButton
    和其他Swing组件只是一种方便的绘制东西(特别是在布局管理器的帮助下绘制屏幕大小)和处理事件的方法,但是您当然可以使用一个简单的JComponent来编写一个简单的游戏,如您的示例中所示(我认为可能会有所帮助)


    随机提示:
    paintComponent
    必须非常快,我的意思是非常快,因此您无法从文件系统读取PNG并将其转换为位图。加载必须在游戏初始化时恰好发生一次

    ,因此如果paintComponent需要非常快地发生并且在游戏初始化时发生一次,我不能去对于包含Thread.sleep的for循环,我正在使用。从主类延迟一次又一次调用paintComponent会起作用吗?如果是,延迟重新绘制的最佳方法是什么。不,应该连续调用paintComponent()。只有在游戏初始化时才能加载位图一次
        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)
        {
    
        }
    }