Question: Need to fix the below java program. My program is supposed to fill circle(oval) in a 4x4 grid of labels when a user clicks on
Need to fix the below java program. My program is supposed to fill circle(oval) in a 4x4 grid of labels when a user clicks on a specific square and un-paint(remove the filled circle) if the same square is clicked again. Also, the previous square that was painted should remain until that square is clicked again. I'm having trouble removing the filled circle. import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; import javax.swing.border.Border; /** * * * This GUI application displays 4 x 4 squares. When the user clicks on a square, * a circle appears in the square and disappear when the user clicks the square again. */ public class DY_FillingTheGrid_2 extends JFrame { JFrame frame = new JFrame(); JPanel gridPanel = new JPanel(); // Create panel JLabel[] gridLabel = new JLabel[17]; JLabel locatedLabel = new JLabel(); /* * Constructor */ public DY_FillingTheGrid_2() { setTitle("Filling The Grid"); // Create title setPreferredSize( new Dimension( 480, 500 ) ); // Set size of the frame setDefaultCloseOperation(EXIT_ON_CLOSE); // Set exit operation on close button setLayout(new BorderLayout()); // Create BorderLayout buildGrid(); // Call method to build grid add(gridPanel, BorderLayout.CENTER); // Add panel to frame and set BorderLayout pack(); setVisible(true); } /** * Paint method */ public void paint(Graphics g) { double width = locatedLabel.getSize().getWidth(); // Initialize width to the size of label double height = locatedLabel.getSize().getHeight(); // Initialize height to the size of label super.paint(g); // Call super class paint method // Paint the circle g.fillOval(locatedLabel.getX()+8, locatedLabel.getY()+30, (int) width, (int) height); } /** * Private inner class MyMouseListener that responds to the event of the mouse */ private class MyMouseListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e){ locatedLabel = (JLabel)e.getSource(); if (locatedLabel.getForeground().equals(Color.BLACK)) { repaint(locatedLabel.getX()+8, locatedLabel.getY()+30, (int)locatedLabel.getSize().getWidth(), (int)locatedLabel.getSize().getHeight()); } else { locatedLabel.setForeground(Color.WHITE); repaint(locatedLabel.getX()+8, locatedLabel.getY()+30, (int)locatedLabel.getSize().getWidth(), (int)locatedLabel.getSize().getHeight()); } } } /** * The buildGrid method builds 4 x 4 grid with labels and panel as a container */ private void buildGrid() { Border border = BorderFactory.createLineBorder(Color.BLUE, 1); // Set grid line color gridPanel = new JPanel(); // Create Panel gridPanel.setLayout(new GridLayout(4,4)); // Set to GridLayout 4 x 4 // Create 16 labels for each grid for (int i=1; i < gridLabel.length; i++) { gridLabel[i] = new JLabel(); gridLabel[i].setBorder(border); gridPanel.add(gridLabel[i]); gridLabel[i].addMouseListener(new MyMouseListener()); } } /** * Main method * @param args */ public static void main(String[] args) { DY_FillingTheGrid_2 fg = new DY_FillingTheGrid_2(); } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
