How to change values of an enum within the code itself?

2

What I'm trying to do is that when I call an Enum, and instantiate it, it appears with value X, and when I'm messing with it in the code, it changes the value to whatever I want. >

Example:

//Classe dos enums
public class Enumers{
    public enum Enumeration{
        VAR1, VAR2, VAR3;
    }
}

The class that manages the enums:

//Classe em que estou tentando mudá-lo
public class ExtensionMouse implements MouseListener, MouseMotionListener, ActionListener {
    public Enumers.Enumeration enumeration = Enumeraton.VAR1;
    public MainMenu menu = new MainMenu();
    public Point mousePoint;
    public int z = 0;

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        switch (enumeration) {
            case VAR1:
                //A imagem "IMAGEM1" já está setada e funcionando //corretamente
                break;
            case VAR2:
                menu.paint(g2); //A imagem "IMAGEM2" já está setada também e funcionando
                break;g
        }
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        int mouse = arg0.getButton();
        mousePoint = arg0.getPoint();
        switch (enumeration) {
            case VAR1:
                for (int i = 0; i < 3; i++) {
                    if (menu.mI.getRetBts(i).contains(mousePoint)) {
                        if (mouse == MouseEvent.BUTTON1) {
                            if (i == 0) {
                                enumeration = Enumeration.VAR2;
                            } else if (i == 1) {
                                System.out.println("mudou Menu");
                            } else if (i == 2) {
                                System.exit(0);
                            }
                        }
                    }
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        switch (enumeration) {
            case VAR1:
                break;

            case VAR2:
                enumeration = Enumeration.VAR2;
                break;

            case VAR3:
                break;

            default:
                break;
        }
    }
}

The mouseListener class:

//Classe do mouse
public class MouseAdaptador implements MouseListener, ActionListener, MouseMotionListener
{
    ExtensionMouse eM = new ExtensionMouse();

    @Override
    public void mousePressed(MouseEvent e)
    {
        eM.mousePressed(e);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        eM.mouseMoved(arg0);
    }
}

The MainLoop class

public class MainLoop extends JPanel implements ActionListener
{
    private static final long serialVersionUID = 1L;

    //Instances
    ExtensionMouse eM = new ExtensionMouse();
    //

    public MainLoop()
    {
        addMouseListener(new MouseAdaptador());
        addMouseMotionListener(new MouseAdaptador());
        setDoubleBuffered(true);    
    }

    public void paint(Graphics g)
    {   
        eM.paint(g);
    }

    public void actionPerformed(ActionEvent arg0) 
    {
        eM.update();
        eM.actionPerformed(arg0);
    }
}

The problem comes later, when I put the method of mouseClicked() or mousePressed() , that when the mouse clicks on "IMAGE1" the enum receives another value, that is, the enumeration receives the value enumeration = Enumeration.VAR2; , only that, instead of changing something or changing the image, it does not happen at all, and worse, it changes the enum, and even putting System.out.println to check, it does not give any message on the console saying if it changed or not ...

I just quoted part of the code that I'm having problems, because mouse events, change values and etc, I do not have any problems.

    
asked by anonymous 04.05.2014 / 21:54

1 answer

3

Whenever you change anything in Swing (or AWT) that requires a new render, you need to call the repaint for the component to paint itself again.

In other words, the paint method is not called several times without need (as in a video or game, where "every frame of animation" the screen is redesigned), but only when it is needed and ready. The rendered image is saved in a buffer , which is not updated unless the system detects that there has been a change that needs a new call of paint .

As you are using your own logic, the system does not know that "if the enum changed need to paint again," then changing the value of the variable in mousePressed will not do anything. At the end of the method, try calling repaint in your component.

Update: You have two problems with your code:

  • There are 3 instances of ExtensionMouse - one that you have explicitly created ( ExtensionMouse eM = new ExtensionMouse(); ) and one created within each MouseAdaptador (which you have created twice). Since there are three objects, changing the value of the enum in one of them will not affect the others. And since what changes is different from what you draw, it seems that you have not changed ...

    I suggest modifying your code so that there is only a single instance of ExtensionMouse . Make sure your MouseAdaptador receives eM as a parameter:

    public class MouseAdaptador implements MouseListener, ActionListener, MouseMotionListener
    {
        //ExtensionMouse eM = new ExtensionMouse();
        ExtensionMouse em;
    
        public MouseAdaptador(ExtensionMouse eM) {
            this.eM = eM;
        }
    
        ...
    

    And, in the MainLoop class, pass the object already created to the MouseAdaptador constructor:

    //Instances
    ExtensionMouse eM = new ExtensionMouse();
    //
    
    public MainLoop()
    {
        addMouseListener(new MouseAdaptador(eM));
        addMouseMotionListener(new MouseAdaptador(eM));
        setDoubleBuffered(true);    
    }
    

    This will solve the first problem: ensure that there is a single instance of ExtensionMouse - and therefore, when you change the value of the enum in a part of the code (mouse) this is reflected in the others. p>

  • As my original response pointed out, it is necessary to call repaint after a change in enum. I suggest that ExtensionMouse save a reference to MainLoop so that you can make that call.

    Class ExtensionMouse :

    public class ExtensionMouse implements MouseListener, MouseMotionListener, ActionListener{
        public Enumers.Enumeration enumeration = Enumeraton.VAR1;
        public MainMenu menu = new MainMenu();
        public Point mousePoint;
        public int z = 0;
        private MainLoop main; // <--- Adicione essa variável
    
        public ExtensionMouse(MainLoop main) {
            this.main = main;
        }
    
        ...
    
        enumeration = Enumeration.VAR2;
        main.repaint(); // <--- adicionar essa linha após toda mudança na enum
    

    Class MainLoop :

    //Instances
    ExtensionMouse eM;
    //
    
    public MainLoop()
    {
        eM = new ExtensionMouse(this);
        addMouseListener(new MouseAdaptador(eM));
        addMouseMotionListener(new MouseAdaptador(eM));
        setDoubleBuffered(true);    
    }
    

    (I moved the instance creation into the constructor because I'm not sure if it can access this before that)

  • 05.05.2014 / 00:10