Hi,I got an application that contains buttons that when pressed,draw a figure.For example,if the "triangle" buttons is pressed then a triangle is drawn.I tried to add controls so that the figure that appears could be moved by keyboard keys but for some reason when a button is pressed tthe controls stop working.
the buttons are in a panel when the controls are in another class that is a frame.
My code:
Menu.java
Picture.javaimport java.awt.*; import java.awt.event.*; import java.util.Timer; import java.util.TimerTask; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Menu extends Frame{ private static Picture pic; private static Button b1; private Triangle triangle; private static Button b2; private Serpinsky s; private static Button b3; private Smiley sm; private static Button b4; private Rectangles r; private static Button b5; private MovePingPong mpp; public Menu(String st,Triangle tri,Serpinsky s,Smiley sm,Rectangles r,MovePingPong mpp) { super(st); this.setSize(800, 600); this.addWindowListener(new ExitListener()); ButtonListener al=new ButtonListener(); b1=new Button("Traingle"); b1.setBackground(Color.gray); b1.addActionListener(al); b2=new Button("Serpinsky"); b2.setBackground(Color.gray); b2.addActionListener(al); b3=new Button("Smiley"); b3.setBackground(Color.gray); b3.addActionListener(al); b4=new Button("Rectangles"); b4.setBackground(Color.gray); b4.addActionListener(al); b5=new Button("Ping Pong ball"); b5.setBackground(Color.gray); b5.addActionListener(al); this.mpp=mpp; this.sm=sm; this.s=s; this.triangle=tri; this.r=r; pic=new Picture(this.triangle,this.s,this.sm,this.r,this.mpp); pic.setBackground(Color.LIGHT_GRAY); pic.add(b1); pic.add(b2); pic.add(b3); pic.add(b4); pic.add(b5); this.add(pic); this.setFocusable(true); this.setVisible(true); } public class ButtonListener implements ActionListener,KeyListener{ public ButtonListener(){ addKeyListener(this); } public void actionPerformed(ActionEvent e){ Button b=(Button)e.getSource(); Graphics g=pic.getGraphics(); if(b.equals(Menu.b1)) pic.paintFrameTraing(Menu.this.triangle, g); if(b.equals(Menu.b2)) pic.paintFrameSerpinsky(Menu.this.s, g); if(b.equals(Menu.b3)) pic.paintFrameSmiley(sm, g); if(b.equals(Menu.b4)) pic.paintFrameRectangles(r, g); if(b.equals(Menu.b5)) pic.paintFramePingPong(mpp, g); } public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_RIGHT){ triangle.Move(10, 0); repaint(); } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } public class ExitListener extends WindowAdapter{ @Override public void windowClosing(WindowEvent e){ System.exit(0); } } public static void main(String[] args) { Triangle triangle=new Triangle(Color.black); Serpinsky s=new Serpinsky(); Smiley sm=new Smiley(); Rectangles r=new Rectangles(); MovePingPong mpp=new MovePingPong(5); new Menu("This is an Applicaion Biiaaaaacch!!",triangle,s,sm,r,mpp); } }
import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Picture extends Panel{ public Picture(Triangle tri,Serpinsky s,Smiley sm,Rectangles r,MovePingPong mpp) { super(); this.setLocation(0,85); this.setLayout(new FlowLayout()); this.setSize(800,600); } public void paintFrameTraing(Triangle tri,Graphics g) { g.clearRect(0,0,1500,1000); g.setColor(Color.black); tri.DrawTriangle(g); } public void paintFrameSerpinsky(Serpinsky s,Graphics g){ g.clearRect(0, 0, 1500, 1000); g.setColor(Color.black); s.drawSerpinsky(g); } public void paintFrameSmiley(Smiley sm,Graphics g) { g.clearRect(0, 0, 1500, 1000); g.setColor(Color.black); sm.drawSmiley(g); } public void paintFrameRectangles(Rectangles r,Graphics g){ g.clearRect(0, 0, 1500, 1000); g.setColor(Color.black); r.drawRectangles(g); } public void paintFramePingPong(MovePingPong mpp,Graphics g){ g.setColor(Color.black); g.clearRect(0, 0, 1500, 1000); mpp.drawPingPong(g); } }