Rysowaniem planszy za pomocą przycisku błąd. Java

0

Witam mam problem z moją grą.Dodałem do niej przycisk powodujący wyświetlenie planszy i gracza i problem tkwi w tym że po naciśnięciu guzika nie da się sterować graczem. Według mnie coś jest spieprzone przy guziku bo bez niego wszystko chula. Proszę o pomoc.
Oto kod do klasy :

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.InputEvent;
import java.net.URL;
import java.util.Timer;

import javax.sound.sampled.AudioSystem;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

import javafx.scene.media.*;

import javax.print.attribute.standard.Media;

public class Gra extends JFrame implements ActionListener,MouseListener,KeyListener{

	//zmienne potrzebne do burorowania wielowątkowego oraz do płynności animacjii
	private Image image;
	private Plansza p = new Plansza();
	private Timer timer=new Timer();
	

	Toolkit kit = Toolkit.getDefaultToolkit();
	
	
	private JButton b;
	private int pozgraczax=0,pozgraczay=0,ostatnioPrzebytePrzezGraczaPoleKolor=0,AktualnyPoziom;
	private Graphics g=getGraphics();
	private Image Bufor ;
	private Graphics bg;
	private boolean czyGraZaczęta=false;
	
	
	
	public Gra()
	{
		this(false);
		
	}
	
	public Gra(boolean undecorated) throws HeadlessException {
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        setUndecorated(undecorated);
        setResizable(false);
        setTitle("");
		setSize(1000, 1000);
		setLayout(null);
		setVisible(true);
		addKeyListener(this);
		addMouseListener(this);
		timer.scheduleAtFixedRate(p, 5, 5);
		
		
		
		Bufor = createImage(1000,1000);
		bg=Bufor.getGraphics();
		
		
		
		
		b=new JButton("witaj");
		b.setBounds(10, 20, 100, 30);
		b.addMouseListener(this);
		add(b);
		
		
	
		
		
	}

	
	
	public void update(Graphics g)
	{
		bg.clearRect(0, 0, 1000, 1000);
		paint(bg);
		g.drawImage(Bufor, 0, 0, this);
		this.repaint();
		
	}
	public void paint(Graphics g)
	{
		if(czyGraZaczęta)
		{
			for(int i = 0;i<Plansza.plansza.length;i++)
			{
				for(int k = 0;k<Plansza.plansza[0].length;k++)
				{
					switch (Plansza.plansza[i][k]) {
					case 0:
	
						g.setColor(Color.black);
						g.fillRect(100+(k*25), 100+(i*25), 25, 25);
						break;
	
					case 1:
	
						
						g.setColor(Color.orange);
						g.fillRect(100+(k*25), 100+(i*25), 25, 25);
						break;
					case 2:
					
						
						g.setColor(Color.green);
						g.fillRect(100+(k*25), 100+(i*25), 25, 25);
						break;
					case 3:
						
						
						g.setColor(Color.blue);
						g.fillRect(100+(k*25), 100+(i*25), 25, 25);
						break;
					case 4:
					
						g.setColor(Color.white);
						g.fillRect(100+(k*25), 100+(i*25), 25, 25);
						pozgraczax=k;
						pozgraczay=i;
						break;
					}
				}
			
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Gra prob=new Gra(false);
		prob.setVisible(true);
		prob.setDefaultCloseOperation(EXIT_ON_CLOSE);
		AudioSystem s; 
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int c = e.getKeyCode();
		if(czyGraZaczęta)
		{
			if(c==KeyEvent.VK_W)
			{
				if(Plansza.plansza[pozgraczay-1][pozgraczax]==0)
				{
					Plansza.plansza[pozgraczay-1][pozgraczax]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=0;
					this.repaint();
				}
				if(Plansza.plansza[pozgraczay-1][pozgraczax]==3)
				{
					if(Plansza.plansza[pozgraczay-2][pozgraczax]==0)
					{
						Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
						Plansza.plansza[pozgraczay-1][pozgraczax]=4;
						Plansza.plansza[pozgraczay-2][pozgraczax]=3;
						ostatnioPrzebytePrzezGraczaPoleKolor=0;
						this.repaint();
					}
				}
				if(Plansza.plansza[pozgraczay-1][pozgraczax]==2)
				{
					Plansza.plansza[pozgraczay-1][pozgraczax]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=2;
					wygrałeś();
					this.repaint();
				}
			}else
			if(c==KeyEvent.VK_S)
			{
				if(Plansza.plansza[pozgraczay+1][pozgraczax]==0)
				{
					Plansza.plansza[pozgraczay+1][pozgraczax]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=0;
					this.repaint();
				}else
				if(Plansza.plansza[pozgraczay+1][pozgraczax]==3)
				{
					if(Plansza.plansza[pozgraczay+2][pozgraczax]==0)
					{
						Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
						Plansza.plansza[pozgraczay+1][pozgraczax]=4;
						Plansza.plansza[pozgraczay+2][pozgraczax]=3;
						ostatnioPrzebytePrzezGraczaPoleKolor=0;
						this.repaint();
					}
				}
				if(Plansza.plansza[pozgraczay+1][pozgraczax]==2)
				{
					Plansza.plansza[pozgraczay+1][pozgraczax]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=2;
					wygrałeś();
					this.repaint();
				}
			}else
			if(c==KeyEvent.VK_A)
			{
				if(Plansza.plansza[pozgraczay][pozgraczax-1]==0)
				{
					Plansza.plansza[pozgraczay][pozgraczax-1]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=0;
					this.repaint();
				}else
				if(Plansza.plansza[pozgraczay][pozgraczax-1]==3)
				{
					if(Plansza.plansza[pozgraczay][pozgraczax-2]==0)
					{
						Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
						Plansza.plansza[pozgraczay][pozgraczax-1]=4;
						Plansza.plansza[pozgraczay][pozgraczax-2]=3;
						ostatnioPrzebytePrzezGraczaPoleKolor=0;
						this.repaint();
					}
				}
				if(Plansza.plansza[pozgraczay][pozgraczax-1]==2)
				{
					Plansza.plansza[pozgraczay][pozgraczax-1]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=2;
					wygrałeś();
					this.repaint();
				}
			}else
			if(c==KeyEvent.VK_D)
			{
				if(Plansza.plansza[pozgraczay][pozgraczax+1]==0)
				{
					Plansza.plansza[pozgraczay][pozgraczax+1]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=0;
					this.repaint();
				}else
				if(Plansza.plansza[pozgraczay][pozgraczax+1]==3)
				{
					if(Plansza.plansza[pozgraczay][pozgraczax+2]==0)
					{
						Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
						Plansza.plansza[pozgraczay][pozgraczax+1]=4;
						Plansza.plansza[pozgraczay][pozgraczax+2]=3;
						ostatnioPrzebytePrzezGraczaPoleKolor=0;
						this.repaint();
					}
				}
				if(Plansza.plansza[pozgraczay][pozgraczax+1]==2)
				{
					Plansza.plansza[pozgraczay][pozgraczax+1]=4;
					Plansza.plansza[pozgraczay][pozgraczax]=ostatnioPrzebytePrzezGraczaPoleKolor;
					ostatnioPrzebytePrzezGraczaPoleKolor=2;
					wygrałeś();
					this.repaint();
				}
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		Object z = e.getSource();
		if(z==b)
		{	
			remove(b);
			czyGraZaczęta=true;
			p.zrubplansze();
			this.repaint();
		}
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	void wygrałeś()
	{
		int wybór=JOptionPane.showConfirmDialog(this, "Wygrałeś","Wygrałeś",JOptionPane.YES_NO_OPTION);
		if(wybór==JOptionPane.YES_OPTION)
		{
			if(AktualnyPoziom==3)
			{
				AktualnyPoziom=0;
				
				
			}
			else
			{
				AktualnyPoziom++;
				
				
			}
			
		}
		if(wybór==JOptionPane.NO_OPTION)
		{
			AktualnyPoziom=0;
		}
		if(wybór==JOptionPane.CLOSED_OPTION)
		{
			AktualnyPoziom=0;
		}
		
	}

	

} 

Ato kod do klasy Placnsza:

import java.util.TimerTask;


public class Plansza extends TimerTask 
{
	
	static int[][] plansza=new int[15][30];
	public void zrubplansze()
	{
		int plansza1[][]={
				{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
				{1,2,2,0,3,0,0,3,0,3,3,0,3,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
				{1,2,2,3,0,3,0,3,3,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
				{1,2,2,0,0,0,3,0,0,0,3,0,0,1,0,0,1,0,0,3,0,0,3,0,3,0,3,3,0,1},
				{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,3,0,3,0,3,0,3,0,1},
				{1,0,1,0,0,0,0,3,0,0,0,0,1,0,0,0,1,0,3,0,1,0,0,0,3,0,3,0,0,1},
				{1,3,0,0,0,0,0,0,1,0,1,0,1,3,0,0,1,3,0,0,0,3,0,3,1,3,0,3,0,1},
				{1,0,1,0,0,0,0,0,1,0,1,3,3,0,0,3,0,0,1,0,3,0,1,0,3,0,3,0,0,1},
				{1,0,1,0,0,0,0,0,1,0,0,0,1,0,3,0,1,0,3,0,1,0,0,3,0,3,0,3,0,1},
				{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
				{1,0,0,3,0,0,3,0,1,0,1,0,1,0,0,0,1,0,0,3,0,0,0,0,3,0,0,1,4,1},
				{1,0,3,0,0,3,0,1,0,0,3,3,1,3,0,3,0,1,0,1,1,0,0,0,0,1,0,0,0,1},
				{1,3,3,3,0,0,3,0,1,0,1,0,0,0,1,0,0,3,3,0,3,0,3,1,0,3,0,1,0,1},
				{1,0,3,0,3,1,0,0,3,0,1,0,1,0,0,3,0,1,0,1,3,0,0,3,0,0,0,0,0,1},
				{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
				};
		
		for(int i = 0;i<plansza1.length;i++)
		{
			for(int k = 0;k<plansza1[0].length;k++)
			{
				plansza[i][k]=plansza1[i][k];
			}
		
		}
	}

	@Override
	public void run() {
	
		
		
	}
	
}

1

Do obsługi przycisku użyj ActionListener a nie MouseListener, czyli przełóż ten kod do metody actionPerformed albo utwórz anonimową klasę wewn (będzie lepiej czytelne)

0

'ActionListener nic niedał

0

OK problem rozwiązany . Udało mi się jakoś to rozwiązać dzięki za chęć pomocy :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1