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

Asked

Viewed 1,126 times

2

What I’m trying to do is, when I call an Enum, and instantiate, it shows up with an X value, 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 administers 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 class of the mouseListener:

//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 click on the "IMAGEM1" the Enum receives another value, that is, the enumeration receives the value enumeration = Enumeration.VAR2;, only, instead of changing something or changing the image, it doesn’t happen at all, and worse, it changes the Enum, and even putting System.out.println to check, does not give any message on the console saying whether or not changed...

I only mentioned part of the code that I’m having problems, because mouse events, change values and etc, I’m not having any problems.

  • show all chunk of code because in this one it will really be difficult to catch the error

  • the ideal would be you create a Minimum, Complete and Verifiable Example, It’s a little hard to understand your doubt at the moment

1 answer

3


Every time you change something in Swing (or AWT) that requires a new rendering, you need to call the method repaint for the component to "paint" again.

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

Since you are using a logic of your own, the system does not know that "if Enum has changed it needs to paint again", then change the value of the variable in the mousePressed will do nothing. Try, at the end of the method, to call the repaint in its component.

Updating: You have two problems with your code:

  1. There’s 3 instances of ExtensionMouse - one you explicitly created (ExtensionMouse eM = new ExtensionMouse();) and a maid within each MouseAdaptador (which, by the way, you created twice). As there are three objects, changing the value of the One in one of them will not affect the others. And since what changes is different from what you draw, it looks like it hasn’t changed...

    I suggest modifying your code so that there is only one instance of ExtensionMouse. Make your MouseAdaptador receive a 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 class MainLoop, pass the already created object to the MouseAdaptador:

    //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, that when you change the value of Enum in one part of the code (mouse) this is reflected in the others (drawing).

  2. As my original response pointed out, it is necessary to call repaint after an Enum change. I suggest ExtensionMouse keep 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 creation of the instance inside the constructor because I’m not sure whether or not it can access the this before that)

  • I appreciate the help, thank you very much. Unfortunately I am not able to place this method within my class that manages these enums, and unfortunately I will not be able to put the full code. I have tried this method (repaint) in this class, and in the other qque are managing both the switch enums and the graphical part, and nothing happened. :/

  • @Andréelias Let’s go in parts: 1) how many objects there are in the class ExtensionMouse? The object responsible for the drawing is the same object updated by your mousePressed? 2) Who does this object draw? It is not itself (for it is not a Component), then it must be another(s). Show the code where the ExtensionMouse.paint is called. 3) Although you cannot show the complete code, show at least the part where you change the value of Enum (methods mousePressed and/or mouseClicked). I believe that the key will be in this method. In fact, these are one-component methods, or am I mistaken?

  • Okay. I modified the question, for the best understanding of all. Take a look... Also, sorry for some mistakes or not knowing some answers, I’m still "Noob" in java, and I’m learning, and so on and so forth.

  • @Andréelias Thanks for the clarification, now it’s easy to see where the problem was. I updated my answer, see if this solves.

  • PUTZ Man, thank you very much, solved my problem!!! Thanks anyway, I thank you, mainly, and the others who tried to help, even if little, but, helped, :X !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.