Reputation: 11
My code:
package a2048;
import javax.swing.JFrame;
public class GameWindow extends JFrame {
public GameWindow() {
add(new Main());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(409, 435);
setLocationRelativeTo(null);
setTitle("2048");
setResizable(true);
setVisible(true);
}
public static void main(String[] args) {
new GameWindow();
}
}
that is one class, the other:
package a2048;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JPanel implements MouseListener
{
int randx, randy, quit;
int a1, a2, a3, a4, b1, b2, b3 ,b4, c1, c2, c3, c4, d1, d2, d3, d4;
int GetRandom(int min, int max)
{
int range = (max - min) + 1;
double rand = (Math.random() * range) + min;
return (int) rand;
}
public Main()
{
setBackground(Color.BLACK);
setDoubleBuffered(true);
}
public void paint(Graphics g)
{
quit = 0;
a1 = a2 = a3 = a4 = b1 = b2 = b3 = b4 = c1 = c2 = c3 = c4 = d1 = d2 = d3 = d4 = 0;
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g2d.drawLine(0, 0, 500, 0);
g2d.drawLine(0, 100, 500, 100);
g2d.drawLine(0, 200, 500, 200);
g2d.drawLine(0, 300, 500, 300);
g2d.drawLine(0, 400, 500, 400);
g2d.drawLine(0, 0, 0, 500);
g2d.drawLine(100, 0, 100, 500);
g2d.drawLine(200, 0, 200, 500);
g2d.drawLine(300, 0, 300, 500);
g2d.drawLine(400, 0, 400, 500);
Font myFont = new Font("TimesRoman", Font.PLAIN, 75);
g2d.setFont(myFont);
while(quit == 0)
{
randx = GetRandom(1, 4);
randy = GetRandom(1, 4);
if(randx == 1)
{
if(randy == 1 && a1 == 0)
{
a1 = 2;
}
else if(randy == 2 && a2 == 0)
{
a2 = 2;
}
else if(randy == 3 && a3 == 0)
{
a3 = 2;
}
else if(randy == 4 && a4 == 0)
{
a4 = 2;
}
}
else if(randx == 2)
{
if(randy == 1 && b1 == 0)
{
b1 = 2;
}
else if(randy == 2 && b2 == 0)
{
b2 = 2;
}
else if(randy == 3 && b3 == 0)
{
b3 = 2;
}
else if(randy == 4 && b4 == 0)
{
b4 = 2;
}
}
else if(randx == 3)
{
if(randy == 1 && c1 == 0)
{
c1 = 2;
}
else if(randy == 2 && c2 == 0)
{
c2 = 2;
}
else if(randy == 3 && c3 == 0)
{
c3 = 2;
}
else if(randy == 4 && c4 == 0)
{
c4 = 2;
}
}
else if(randx == 4)
{
if(randy == 1 && d1 == 0 && a1 == 0)
{
d1 = 2;
}
else if(randy == 2 && d2 == 0 && a1 == 0)
{
d2 = 2;
}
else if(randy == 3 && d3 == 0 && a1 == 0)
{
d3 = 2;
}
else if(randy == 4 && d4 == 0 && a1 == 0)
{
d4 = 2;
}
}
if(a1 > 0)
g2d.drawString(String.valueOf(a1), (randx * 100) - 70, (randy * 100) - 25);
if(a2 > 0)
g2d.drawString(String.valueOf(a2), (randx * 100) - 70, (randy * 100) - 25);
if(a3 > 0)
g2d.drawString(String.valueOf(a3), (randx * 100) - 70, (randy * 100) - 25);
if(a4 > 0)
g2d.drawString(String.valueOf(a4), (randx * 100) - 70, (randy * 100) - 25);
if(b1> 0)
g2d.drawString(String.valueOf(b1), (randx * 100) - 70, (randy * 100) - 25);
if(b2 > 0)
g2d.drawString(String.valueOf(b2), (randx * 100) - 70, (randy * 100) - 25);
if(b3 > 0)
g2d.drawString(String.valueOf(b3), (randx * 100) - 70, (randy * 100) - 25);
if(b4 > 0)
g2d.drawString(String.valueOf(b4), (randx * 100) - 70, (randy * 100) - 25);
if(c1 > 0)
g2d.drawString(String.valueOf(c1), (randx * 100) - 70, (randy * 100) - 25);
if(c2 > 0)
g2d.drawString(String.valueOf(c2), (randx * 100) - 70, (randy * 100) - 25);
if(c3 > 0)
g2d.drawString(String.valueOf(c3), (randx * 100) - 70, (randy * 100) - 25);
if(c4 > 0)
g2d.drawString(String.valueOf(c4), (randx * 100) - 70, (randy * 100) - 25);
if(d1 > 0)
g2d.drawString(String.valueOf(d1), (randx * 100) - 70, (randy * 100) - 25);
if(d2 > 0)
g2d.drawString(String.valueOf(d2), (randx * 100) - 70, (randy * 100) - 25);
if(d3 > 0)
g2d.drawString(String.valueOf(d3), (randx * 100) - 70, (randy * 100) - 25);
if(d4 > 0)
g2d.drawString(String.valueOf(d4), (randx * 100) - 70, (randy * 100) - 25);
quit = 1;
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void mouseExited(MouseEvent e)
{
repaint();
}
public void mouseReleased(MouseEvent e)
{
repaint();
}
public void mousePressed(MouseEvent e)
{
repaint();
}
public void mouseEntered(MouseEvent e)
{
repaint();
}
public void mouseClicked(MouseEvent e)
{
a1 = 5;
repaint();
}
}
I don't understand why the mouseClicked method is not doing anything when I click the window. What i want it to do is set a1 to 5 and then print that on the screen, but instead it is simply not doing anything. What do I change to make it so that MouseListener is working correctly?
Upvotes: 0
Views: 2438
Reputation: 10955
A MouseListener
needs to be registered with what it is listening to to be able to do anything.
Whenever you click the mouse on something, it will generate a MouseEvent
and invoke methods on anything which is listening to it, passing that event in as a parameter.
You seem to be implementing your listener as part of your JPanel
, so in addition to adding it as a component in your GameWindow
, you also need to register it as a mouse listener on the window.
Replace
add(new Main());
with
Main main = new Main();
addMouseListener(main);
add(main);
EDIT
A cleaner way to do this suggested by HovercraftFullOfEels would be to have the
Main JPanel
listen for mouse events on itself. To do this you would call addMouseListener()
in the constructor of Main rather than in the enclosing JFrame
:
public Main() {
addMouseListener(this);
setBackground(Color.BLACK);
setDoubleBuffered(true);
}
Upvotes: 3