Skip to main content
added 43 characters in body
Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21

ThisThe below code is placed in a method . But it seems too large and I would like to shorten it. Is there a better way to do what it does?

This seems too large and I would like to shorten it. Is there a better way to do what it does?

The below code is placed in a method . But it seems too large and I would like to shorten it. Is there a better way to do what it does?

deleted 8 characters in body; edited title; deleted 22 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Swing - Shortening the mouseClicked method

Code:This seems too large and I would like to shorten it. Is there a better way to do what it does?

The above code seems too large and I would like to shorten it. Is there a better way to do what the above code does?

Swing - Shortening the mouseClicked method

Code:

The above code seems too large and I would like to shorten it. Is there a better way to do what the above code does?

mouseClicked method

This seems too large and I would like to shorten it. Is there a better way to do what it does?

Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21

Swing - Shortening the mouseClicked method

Code:

ImageIcon switchoffIMG = new ImageIcon("switch1.jpg");
ImageIcon switchonIMG  = new ImageIcon("switch2.jpg");

JLabel switch1 = new JLabel(switchoffIMG);
JLabel switch2 = new JLabel(switchoffIMG);
JLabel switch3 = new JLabel(switchoffIMG); /* Will add these into different JPanels */

boolean switch1state, switch2state, switch3state;
switch1state = switch2state = switch3state = false;

switch1.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e)
    {
        if(switch1state == false)
        {
            if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
                switch1state = true;
        }else
        {
            if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
                switch1state = false;
        }

        paintStuff();
    }
});

switch2.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e)
    {
        if(switch2state == false)
        {
            if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
                switch2state = true;
        }else
        {
            if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
                switch2state = false;
        }

        paintStuff();
    }
});

switch3.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e)
    {
        if(switch3state == false)
        {
            if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY() <= OFFBUTTONDOWN))
                switch3state = true;
        }else
        {
            if((e.getX() >= ONBUTTONLEFT && e.getX() <= ONBUTTONRIGHT) && (e.getY() >= ONBUTTONTOP && e.getY() <= ONBUTTONDOWN))
                switch3state = false;
        }

        paintStuff();
    }
});

The above code seems too large and I would like to shorten it. Is there a better way to do what the above code does?

Additional information (if required):

public void paintStuff()
{
    if(switch1state)
    {
        switch1.setIcon(switchonIMG);
        left.add(bulb1);              //Add image to JPanel
    }else
    {
        switch1.setIcon(switchoffIMG);
        left.remove(bulb1);           //Remove image from JPanel
    }
    if(switch2state)
    {
        switch2.setIcon(switchonIMG);
        mid.add(bulb2);               //Add image to JPanel
    }else
    {
        switch2.setIcon(switchoffIMG);
        mid.remove(bulb2);            //Remove image from JPanel
    }
    if(switch3state)
    {
        switch3.setIcon(switchonIMG);
        right.add(bulb3);             //Add image to JPanel
    }else
    {
        switch3.setIcon(switchoffIMG);
        right.remove(bulb3);          //Remove image from JPanel
    }
    repaint();
}

and

final static int OFFBUTTONTOP   = 75;
final static int OFFBUTTONLEFT  = 30;
final static int OFFBUTTONRIGHT = 65;
final static int OFFBUTTONDOWN  = 115;

final static int ONBUTTONTOP   = 35;
final static int ONBUTTONLEFT  = 25;
final static int ONBUTTONRIGHT = 60;
final static int ONBUTTONDOWN  = 75;