Hi Everyone,
I am writing this program to display this GUI with buttons ,I am trying to add an action listener to add functionality to the buttons.
Problem:I am trying to display the colour of the buttons as in (i.e. When a blue button is clicked a blue rectangle is displayed in the center pane)
Now I have managed to draw the rectangle on the center pane but when I add the action Listener to the button it does not compile it generates two errors.
Can someone have a look at my code please , i dont know how to call the paint function into the Listener
errors:
shape.java:43:cannot find symbol
symbol: method setColor(java.awt.Color)
location: class MyPanel
disp.setColor(Color.red)
shape.java:43:cannot find symbol
symbol: method fillRectr(int,int,int,int)
location: class MyPanel
disp.fillRectr(100,50,100,50)
code:
shape.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class shape extends JFrame{ JButton b1 = new JButton("Red"); JButton b2 = new JButton("Green"); JButton b3 = new JButton("Blue"); JButton b4 = new JButton("Black"); JButton b5 = new JButton("Erase"); JPanel east = new JPanel(); MyPanel disp = new MyPanel(); JPanel center = new JPanel(); shape(){ setupGUI(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void setupGUI(){ Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(BorderLayout.CENTER,disp); cp.add(disp); cp.add(BorderLayout.EAST,east); east.setLayout(new GridLayout(5,1)); east.add(b1); east.add(b2); east.add(b3); east.add(b4); east.add(b5); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("RED")){ disp.setColor(Color.red); disp.fillRect(100,50,100,50); //setColor(Color.red); //fillRect(100,50,100,50); } } }); setSize(350,450); setVisible(true); } public static void main(String args[]){ shape Pane = new shape(); } }
MyPanel.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; class MyPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.white); g.drawRect(100,50,100,50); g.fillRect(100,50,100,50); } }