Está en la página 1de 6

@Program to draw and fill a rectangle with the colour button pressed. Import java.awt.*; Import java.awt.event.

*; Class ActionEvent3 extends Frame Implements ActionListener { Color col=Color.pink; Public static void main(String as[]) { ActionEvent3 f=new ActionEvent3(); f.setSize(200,200); f.setVisible(true); } Public ActionEvent3() { FlowLayout fl=new FlowLayout(); setLayout(fl); Button b1=new Button(red); Button b2=new Button(green); Button b3=new Button(blue); add(b1); add(b2);

add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); public void actionPerformed(ActionEvent ac) { String s=ae,getActionCommand(); if(s.equals(red)) col=Color.red; if(s.equals(green)) col=Color.green; if(s.equals(blue)) col=Color.blue; repaint(); } Public void paint(Graphics g) { g.setColor(col); g.fillRect(10,100,100,1000); } }

@Program to fill the color from the color listed in the list box. Import java.awt.*; Import java.awt.event.*; class ActionEvent31 extends Frame implements.ActionListener { Color col=Color.pink; Public static void main(String as[]) { ActionEvent31 f=new ActionEvent31(); f.setSize(300,300); f.setVisible(true); } Public ActionEvent31() { FlowLayout fl=new FlowLayout(); setLayout(fl); List 1st=new List(); 1st.add(red); 1st.add(green); 1st.add(blue); add(1st);

1st.addActionListener(this); } public void actionperformed(ActionEvent ae) { String s=ae.getActionCommand(); if(s.equals(red)) col=Color.red; if(s.equals(green)) col=Color.green; if(s.equals(blue)) col=Color.blue; repaint(); } Public void paint(Graphics g) { g.setColor(col); g.fillRect(10,100,100,1000); } }

@Filling the rectangle from the color shown on the menu. import java.awt.*; import java.awt.event.*; class ActionEvent32 extends Frame implements ActionListner { Color col=Color.pink; Public static void main(String as[]) { ActionEvent32 f=new ActionEvent32(); f.setSize(200,200); f.setVisible(true); } Public ActionEvent32() { FlowLayout f1=new FlowLayout(); setLayout(f1); MenuBar mb=new MenuBar(); Menu m=new Menu(color); MenuItem m1=new Menuitem(red); MenuItem m2=new Menuitem(green); MenuItem m3=new Menuitem(blue);

m.add(m1); m.add(m2); m.add(m3); mb.add(m); m.addActionListner(this); setMenuBar(mb); } Public void actionperformed(ActionEvent ae){ String s=ae.getActionCommand(); if(s.equals(red)) col=Color.red; if(s.equals(green)) col=Color.green; if(s.equals(blue)) col=Color.blue; repaint(); } Public void paint(Graphics g){ g.setColor(col); g.fillRect(10,100,100,1000); } }

También podría gustarte