4
\$\begingroup\$

I'm currently trying to get somewhat familiar with swing and so I made a little game called "Mouse LorMorR" (Mouse Left or Middle or Right). The game consists of a timer, a score display and three JPanels that display which mouse button you should click. By clicking the wrong mouse button or letting the timer run out the timer and score reset.

I'd like to know what I can improve.

MainClass

package com.Skrelp.LorMorR;

public class MainClass {

    public static void main(String[] args) {
        new LorMorRFrame();
    }

}

LorMorRFrame

package com.Skrelp.LorMorR;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LorMorRFrame extends JFrame implements MouseListener{

    private static final long serialVersionUID = -508527838627274300L;

    private static final int RIGHT = 0;
    private static final int MIDDLE = 1;
    private static final int LEFT = 2;

    public boolean colorBash = false;
    private int easterEgg = 0;
    private int wantedMouseButton = MIDDLE;
    JPanel mainPanel, subPanelCenter, subPanelSouth, panelLeft, panelRight, panelMiddle;
    JButton buttonLeft, buttonRight, buttonMiddle;
    ExtraPanel extraPanel;

    public LorMorRFrame(){
        super("Mouse LorMorR");
        GridLayout grid13 = new GridLayout(1, 3);
        mainPanel = new JPanel(new BorderLayout());
        subPanelCenter = new JPanel(grid13);
        subPanelSouth = new JPanel(grid13);
        panelLeft = new JPanel();
        buttonLeft = new JButton("Left");
        panelRight = new JPanel();
        buttonRight = new JButton("Right");
        panelMiddle = new JPanel();
        buttonMiddle = new JButton("Middle");
        extraPanel = new ExtraPanel();
        getContentPane().add(mainPanel);
        mainPanel.add(extraPanel, BorderLayout.NORTH);
        mainPanel.add(subPanelCenter, BorderLayout.CENTER);
        mainPanel.add(subPanelSouth, BorderLayout.SOUTH);
        subPanelCenter.add(panelLeft, grid13);
        subPanelCenter.add(panelMiddle, grid13);
        subPanelCenter.add(panelRight, grid13);
        subPanelSouth.add(buttonLeft, grid13);
        subPanelSouth.add(buttonMiddle, grid13);
        subPanelSouth.add(buttonRight, grid13);
        panelLeft.setBackground(Color.WHITE);
        panelMiddle.setBackground(Color.WHITE);
        panelRight.setBackground(Color.WHITE);
        buttonLeft.setBackground(Color.WHITE);
        buttonMiddle.setBackground(Color.WHITE);
        buttonRight.setBackground(Color.WHITE);
        setSize(500,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        ckeckForResizing();
        addMouseListener(this);
        start();
        prepareEasterEgg();
    }

    private void prepareEasterEgg() {
        buttonLeft.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                if (easterEgg == 0){
                    easterEgg += 1;
                }else{
                    easterEgg = 0;
                }
            }
        });
        buttonMiddle.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                if(easterEgg == 1){
                    easterEgg +=10;
                }else{
                    easterEgg = 0;
                }
            }
        });
        buttonRight.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                if(easterEgg == 11){
                    easterEgg +=100;
                    if (colorBash){
                        colorBash = false;
                    }else{
                        colorBash = true;
                    }
                }else{
                    easterEgg = 0;
                }
            }
        });
    }

    private void start() {
        panelMiddle.setBackground(Color.BLACK);
    }

    private void ckeckForResizing() {
        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                extraPanel.setFrameWidth(getWidth());
            }
        });

    }


    private void next() {
        Random rand = new Random();
        if (colorBash){
            Color color = new Color(rand.nextInt(200)+25, 
                    rand.nextInt(200)+25,
                    rand.nextInt(200)+25);
            panelLeft.setBackground(color);
            panelMiddle.setBackground(color);
            panelRight.setBackground(color);
            wantedMouseButton = rand.nextInt(3);
            if (wantedMouseButton == RIGHT){
                if (rand.nextBoolean()) panelRight.setBackground(color.brighter());
                else panelRight.setBackground(color.darker());
            }   
            if (wantedMouseButton == LEFT){
                if (rand.nextBoolean()) panelLeft.setBackground(color.brighter());
                else panelLeft.setBackground(color.darker());
            }
            if (wantedMouseButton == MIDDLE){
                if (rand.nextBoolean()) panelMiddle.setBackground(color.brighter());
                else panelMiddle.setBackground(color.darker());
            }
        }else{
            panelLeft.setBackground(Color.WHITE);
            panelMiddle.setBackground(Color.WHITE);
            panelRight.setBackground(Color.WHITE);
            wantedMouseButton = rand.nextInt(3);
            if (wantedMouseButton == RIGHT){
                panelRight.setBackground(Color.BLACK);
            }   
            if (wantedMouseButton == LEFT){
                panelLeft.setBackground(Color.BLACK);
            }
            if (wantedMouseButton == MIDDLE){
                panelMiddle.setBackground(Color.BLACK);
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent evt) {
        if(extraPanel.firstClick){
            extraPanel.firstClick = false;
            extraPanel.timerStopped = false;
            extraPanel.setScore(0);
            extraPanel.setDelay(100);
        }
        if (SwingUtilities.isRightMouseButton(evt)){
            if(wantedMouseButton == RIGHT){
                next();
                extraPanel.upScore();
                extraPanel.reduceDelay();
            }else{
                extraPanel.firstClick = true;
                extraPanel.timerStopped = true;
            }
        }else if (SwingUtilities.isMiddleMouseButton(evt)){
            if(wantedMouseButton == MIDDLE){
                next();
                extraPanel.upScore();
                extraPanel.reduceDelay();
            }else{
                extraPanel.firstClick = true;
                extraPanel.timerStopped = true;
            }
        }else if (SwingUtilities.isLeftMouseButton(evt)){
            if(wantedMouseButton == LEFT){
                next();
                extraPanel.upScore();
                extraPanel.reduceDelay();
            }else{
                extraPanel.firstClick = true;
                extraPanel.timerStopped = true;
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent evt) {
    }

    @Override
    public void mouseExited(MouseEvent evt) {
    }

    @Override
    public void mousePressed(MouseEvent evt) {
    }

    @Override
    public void mouseReleased(MouseEvent evt) {
    }
}

ExtraPanel

package com.Skrelp.LorMorR;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class ExtraPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = -4682418373205077458L;

    private int frameWidth = 100;
    private int delay = 100;
    private int actualDelay = 100;
    private static final int PREFERRED_HEIGHT = 40;

    public boolean timerStopped = true;
    public boolean firstClick = true;

    private int score = 0;

    public ExtraPanel(){
        super();
        setPreferredSize(new Dimension(2000, PREFERRED_HEIGHT));
        draw();
    }

    private void draw() {
        ActionListener drawer = new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    repaint();
                    if (!timerStopped){
                        if (delay > 0){
                            delay-= 1;
                        }
                        else {
                            timerStopped = true;
                            firstClick = true;
                        }
                    }
                }               
        };
        Timer t = new Timer(10, drawer);
        t.start();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        setBackground(Color.WHITE);
        g.setColor(Color.BLACK);
        g.drawString(getScore(), getFrameWidth()/2-g.getFontMetrics().stringWidth(getScore()), getHeight() - 2);
        g.setColor(Color.RED);
        g.fillRect(0, 0,
                getFrameWidth() - (getFrameWidth()-getFrameWidth()*delay/actualDelay),
                getHeight()/2);
    }

    private String getScore() {
        return Integer.toString(score);
    }

    public void setScore(int score){
        this.score  = score;
    }

    public void upScore(){
        score++;
    }

    public int getDelay() {
        return delay;
    }

    public void setDelay(int delay) {
        this.delay = delay;
        this.actualDelay = delay;
    }

    public void reduceDelay(){
        delay = 100 - score/4;
        actualDelay = 100 - score/4;
        if (delay < 10 && actualDelay < 10){
            delay = 10;
            actualDelay = 10;
        }
    }

    public int getFrameWidth() {
        return frameWidth;
    }

    public void setFrameWidth(int framewidth) {
        this.frameWidth = framewidth;
    }

}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your LorMorRFrame seems too long! Try separating those ActionListeners from the prepare<Something> methods into a single separate class that implements it.

I understand this is your first game version, but I'd also suggest to improve a little more on the main game controller class: try not to automatically prepare and start the game. The user might want to just see their history, so preparing was unnecessary. And if the user wants to play, he probably also wants to have some time to get ready before starting: it could be after any key, for example. Furthermore, I couldn't find what happens when the game ends, you could enhance that gaming life cycle architecture.

Note: I'm sorry I didn't have time to run your code, so consider my feedback just from a coding perspective.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.