Skip to main content
removed off-topic request to add features, included closing brace in code block
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463

I have this project that should respond to events.Can someone see if Can my code can be refractorrefactored and become shorter. What are your advise? Am I using actionListeneractionListener properly? I'm also having trouble creating a star if someone could do that please let me know how to create such shape. Thank you.

public class DifferentShapes extends JPanel implements Shapes{

protected JMenuBar menuBar = new JMenuBar();
protected JRadioButton small = new JRadioButton("small");
protected JRadioButton medium = new JRadioButton("medium");
protected JRadioButton large = new JRadioButton("large");
protected Color color = Color.BLACK;
protected Graphics shape;
protected int display = -1;
protected int colorNo = -1;
int divider2 = 2;
int divider4 = 4;
public DifferentShapes()
{
    
    JMenu shapes = new JMenu("Shapes");
    JMenuItem circle = shapes.add(new JMenuItem("circle"));
    circle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 0;
           repaint();
        }   
    });
    JMenuItem square = shapes.add(new JMenuItem("square"));
    square.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            display = 1;
            repaint();
        }         
    });
    JMenuItem triangle = shapes.add(new JMenuItem("triangle"));
    triangle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 2;
           repaint();
        }   
    });
    
    JMenu colors = new JMenu("Colors");
    JMenuItem red = colors.add(new JMenuItem("red"));
    red.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 0;
           color = Color.RED;
           repaint();
        }   
    });
   JMenuItem blue = colors.add(new JMenuItem("blue"));
    blue.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 1;
           color = Color.BLUE;
           repaint();
        }   
    });
    JMenuItem yellow = colors.add(new JMenuItem("yellow"));
    yellow.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 2;
           color = Color.YELLOW;
           repaint();
        }   
    });
  
    menuBar.add(shapes);
    menuBar.add(colors);
    
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(small);
    buttonGroup.add(medium);
    buttonGroup.add(large);
    this.add(small);
    this.add(medium);
    this.add(large);
    large.setSelected(true);
    small.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 4;
           divider4 = 6;repaint();
        }
    
    });
    medium.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 3;
           divider4 = 5;repaint();
        }
    
    });
    large.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 2;
           divider4 = 4;repaint();
        }
    
    });
   
}
public static void main(String[] args) 
{
    Runnable runnable = new Runnable(){

        @Override
        public void run() {
            DifferentShapes panel = new DifferentShapes();
            JFrame frame = new JFrame("Different Shapes");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,400);
            frame.setVisible(true);  
            frame.add(panel);
            frame.setJMenuBar(panel.getMenuBAr());
        }
        
    };EventQueue.invokeLater(runnable);   
}  

@Override
public void paintComponent(Graphics shape)
{
    super.paintComponent(shape);
    switch(colorNo)
    {
        case 0 : shape.setColor(color);break;
        case 1 : shape.setColor(color);break;
        case 2 : shape.setColor(color);break;
    }
    switch(display)
    {
        case 0 : circle(shape);break;
        case 1 : square(shape);break;
        case 2 : triangle(shape);break;          
    } 
}
//4 4 2 2
@Override
public void circle(Graphics shape) {
   shape.fillOval(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getWidth()/divider2);
}

@Override
public void square(Graphics shape) {
   shape.fillRect(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getHeight()/divider2);
}

@Override
public void triangle(Graphics shape) {
    int[] x = new int[3];
    int[] y = new int[3];
    x[0] = getWidth() / 4;
    x[1] = getWidth() / 2;
    x[2] = 3 * getWidth() / 4;
    y[0] = 3 * getHeight() / divider4;
    y[1] = getHeight() / divider4;
    y[2] = y[0];
    Polygon polygon = new Polygon(x, y, 3);
    shape.fillPolygon(polygon);
}

private JMenuBar getMenuBAr() {
    return menuBar;
}
}

}

I have this project that should respond to events.Can someone see if my code can be refractor and become shorter. What are your advise using actionListener? I'm also having trouble creating a star if someone could do that please let me know how to create such shape. Thank you.

public class DifferentShapes extends JPanel implements Shapes{

protected JMenuBar menuBar = new JMenuBar();
protected JRadioButton small = new JRadioButton("small");
protected JRadioButton medium = new JRadioButton("medium");
protected JRadioButton large = new JRadioButton("large");
protected Color color = Color.BLACK;
protected Graphics shape;
protected int display = -1;
protected int colorNo = -1;
int divider2 = 2;
int divider4 = 4;
public DifferentShapes()
{
    
    JMenu shapes = new JMenu("Shapes");
    JMenuItem circle = shapes.add(new JMenuItem("circle"));
    circle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 0;
           repaint();
        }   
    });
    JMenuItem square = shapes.add(new JMenuItem("square"));
    square.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            display = 1;
            repaint();
        }         
    });
    JMenuItem triangle = shapes.add(new JMenuItem("triangle"));
    triangle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 2;
           repaint();
        }   
    });
    
    JMenu colors = new JMenu("Colors");
    JMenuItem red = colors.add(new JMenuItem("red"));
    red.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 0;
           color = Color.RED;
           repaint();
        }   
    });
   JMenuItem blue = colors.add(new JMenuItem("blue"));
    blue.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 1;
           color = Color.BLUE;
           repaint();
        }   
    });
    JMenuItem yellow = colors.add(new JMenuItem("yellow"));
    yellow.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 2;
           color = Color.YELLOW;
           repaint();
        }   
    });
  
    menuBar.add(shapes);
    menuBar.add(colors);
    
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(small);
    buttonGroup.add(medium);
    buttonGroup.add(large);
    this.add(small);
    this.add(medium);
    this.add(large);
    large.setSelected(true);
    small.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 4;
           divider4 = 6;repaint();
        }
    
    });
    medium.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 3;
           divider4 = 5;repaint();
        }
    
    });
    large.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 2;
           divider4 = 4;repaint();
        }
    
    });
   
}
public static void main(String[] args) 
{
    Runnable runnable = new Runnable(){

        @Override
        public void run() {
            DifferentShapes panel = new DifferentShapes();
            JFrame frame = new JFrame("Different Shapes");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,400);
            frame.setVisible(true);  
            frame.add(panel);
            frame.setJMenuBar(panel.getMenuBAr());
        }
        
    };EventQueue.invokeLater(runnable);   
}  

@Override
public void paintComponent(Graphics shape)
{
    super.paintComponent(shape);
    switch(colorNo)
    {
        case 0 : shape.setColor(color);break;
        case 1 : shape.setColor(color);break;
        case 2 : shape.setColor(color);break;
    }
    switch(display)
    {
        case 0 : circle(shape);break;
        case 1 : square(shape);break;
        case 2 : triangle(shape);break;          
    } 
}
//4 4 2 2
@Override
public void circle(Graphics shape) {
   shape.fillOval(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getWidth()/divider2);
}

@Override
public void square(Graphics shape) {
   shape.fillRect(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getHeight()/divider2);
}

@Override
public void triangle(Graphics shape) {
    int[] x = new int[3];
    int[] y = new int[3];
    x[0] = getWidth() / 4;
    x[1] = getWidth() / 2;
    x[2] = 3 * getWidth() / 4;
    y[0] = 3 * getHeight() / divider4;
    y[1] = getHeight() / divider4;
    y[2] = y[0];
    Polygon polygon = new Polygon(x, y, 3);
    shape.fillPolygon(polygon);
}

private JMenuBar getMenuBAr() {
    return menuBar;
}

}

I have this project that should respond to events. Can my code can be refactored and become shorter? Am I using actionListener properly?

public class DifferentShapes extends JPanel implements Shapes{

protected JMenuBar menuBar = new JMenuBar();
protected JRadioButton small = new JRadioButton("small");
protected JRadioButton medium = new JRadioButton("medium");
protected JRadioButton large = new JRadioButton("large");
protected Color color = Color.BLACK;
protected Graphics shape;
protected int display = -1;
protected int colorNo = -1;
int divider2 = 2;
int divider4 = 4;
public DifferentShapes()
{
    
    JMenu shapes = new JMenu("Shapes");
    JMenuItem circle = shapes.add(new JMenuItem("circle"));
    circle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 0;
           repaint();
        }   
    });
    JMenuItem square = shapes.add(new JMenuItem("square"));
    square.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            display = 1;
            repaint();
        }         
    });
    JMenuItem triangle = shapes.add(new JMenuItem("triangle"));
    triangle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 2;
           repaint();
        }   
    });
    
    JMenu colors = new JMenu("Colors");
    JMenuItem red = colors.add(new JMenuItem("red"));
    red.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 0;
           color = Color.RED;
           repaint();
        }   
    });
   JMenuItem blue = colors.add(new JMenuItem("blue"));
    blue.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 1;
           color = Color.BLUE;
           repaint();
        }   
    });
    JMenuItem yellow = colors.add(new JMenuItem("yellow"));
    yellow.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 2;
           color = Color.YELLOW;
           repaint();
        }   
    });
  
    menuBar.add(shapes);
    menuBar.add(colors);
    
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(small);
    buttonGroup.add(medium);
    buttonGroup.add(large);
    this.add(small);
    this.add(medium);
    this.add(large);
    large.setSelected(true);
    small.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 4;
           divider4 = 6;repaint();
        }
    
    });
    medium.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 3;
           divider4 = 5;repaint();
        }
    
    });
    large.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 2;
           divider4 = 4;repaint();
        }
    
    });
   
}
public static void main(String[] args) 
{
    Runnable runnable = new Runnable(){

        @Override
        public void run() {
            DifferentShapes panel = new DifferentShapes();
            JFrame frame = new JFrame("Different Shapes");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,400);
            frame.setVisible(true);  
            frame.add(panel);
            frame.setJMenuBar(panel.getMenuBAr());
        }
        
    };EventQueue.invokeLater(runnable);   
}  

@Override
public void paintComponent(Graphics shape)
{
    super.paintComponent(shape);
    switch(colorNo)
    {
        case 0 : shape.setColor(color);break;
        case 1 : shape.setColor(color);break;
        case 2 : shape.setColor(color);break;
    }
    switch(display)
    {
        case 0 : circle(shape);break;
        case 1 : square(shape);break;
        case 2 : triangle(shape);break;          
    } 
}
//4 4 2 2
@Override
public void circle(Graphics shape) {
   shape.fillOval(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getWidth()/divider2);
}

@Override
public void square(Graphics shape) {
   shape.fillRect(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getHeight()/divider2);
}

@Override
public void triangle(Graphics shape) {
    int[] x = new int[3];
    int[] y = new int[3];
    x[0] = getWidth() / 4;
    x[1] = getWidth() / 2;
    x[2] = 3 * getWidth() / 4;
    y[0] = 3 * getHeight() / divider4;
    y[1] = getHeight() / divider4;
    y[2] = y[0];
    Polygon polygon = new Polygon(x, y, 3);
    shape.fillPolygon(polygon);
}

private JMenuBar getMenuBAr() {
    return menuBar;
}
}
Source Link

Making use of Events in Java Swing and scaling shapes

I have this project that should respond to events.Can someone see if my code can be refractor and become shorter. What are your advise using actionListener? I'm also having trouble creating a star if someone could do that please let me know how to create such shape. Thank you.

public class DifferentShapes extends JPanel implements Shapes{

protected JMenuBar menuBar = new JMenuBar();
protected JRadioButton small = new JRadioButton("small");
protected JRadioButton medium = new JRadioButton("medium");
protected JRadioButton large = new JRadioButton("large");
protected Color color = Color.BLACK;
protected Graphics shape;
protected int display = -1;
protected int colorNo = -1;
int divider2 = 2;
int divider4 = 4;
public DifferentShapes()
{
    
    JMenu shapes = new JMenu("Shapes");
    JMenuItem circle = shapes.add(new JMenuItem("circle"));
    circle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 0;
           repaint();
        }   
    });
    JMenuItem square = shapes.add(new JMenuItem("square"));
    square.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            display = 1;
            repaint();
        }         
    });
    JMenuItem triangle = shapes.add(new JMenuItem("triangle"));
    triangle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
            
           display = 2;
           repaint();
        }   
    });
    
    JMenu colors = new JMenu("Colors");
    JMenuItem red = colors.add(new JMenuItem("red"));
    red.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 0;
           color = Color.RED;
           repaint();
        }   
    });
   JMenuItem blue = colors.add(new JMenuItem("blue"));
    blue.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 1;
           color = Color.BLUE;
           repaint();
        }   
    });
    JMenuItem yellow = colors.add(new JMenuItem("yellow"));
    yellow.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           colorNo = 2;
           color = Color.YELLOW;
           repaint();
        }   
    });
  
    menuBar.add(shapes);
    menuBar.add(colors);
    
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(small);
    buttonGroup.add(medium);
    buttonGroup.add(large);
    this.add(small);
    this.add(medium);
    this.add(large);
    large.setSelected(true);
    small.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 4;
           divider4 = 6;repaint();
        }
    
    });
    medium.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 3;
           divider4 = 5;repaint();
        }
    
    });
    large.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {
           divider2 = 2;
           divider4 = 4;repaint();
        }
    
    });
   
}
public static void main(String[] args) 
{
    Runnable runnable = new Runnable(){

        @Override
        public void run() {
            DifferentShapes panel = new DifferentShapes();
            JFrame frame = new JFrame("Different Shapes");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,400);
            frame.setVisible(true);  
            frame.add(panel);
            frame.setJMenuBar(panel.getMenuBAr());
        }
        
    };EventQueue.invokeLater(runnable);   
}  

@Override
public void paintComponent(Graphics shape)
{
    super.paintComponent(shape);
    switch(colorNo)
    {
        case 0 : shape.setColor(color);break;
        case 1 : shape.setColor(color);break;
        case 2 : shape.setColor(color);break;
    }
    switch(display)
    {
        case 0 : circle(shape);break;
        case 1 : square(shape);break;
        case 2 : triangle(shape);break;          
    } 
}
//4 4 2 2
@Override
public void circle(Graphics shape) {
   shape.fillOval(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getWidth()/divider2);
}

@Override
public void square(Graphics shape) {
   shape.fillRect(getWidth()/divider4, getHeight()/divider4, getWidth()/divider2, getHeight()/divider2);
}

@Override
public void triangle(Graphics shape) {
    int[] x = new int[3];
    int[] y = new int[3];
    x[0] = getWidth() / 4;
    x[1] = getWidth() / 2;
    x[2] = 3 * getWidth() / 4;
    y[0] = 3 * getHeight() / divider4;
    y[1] = getHeight() / divider4;
    y[2] = y[0];
    Polygon polygon = new Polygon(x, y, 3);
    shape.fillPolygon(polygon);
}

private JMenuBar getMenuBAr() {
    return menuBar;
}

}