90 likes | 94 Views
Look at applet below: import java.awt.*; import java.awt.geom.Rectangle2D; import java.applet.Applet; public class buttonsApplet extends Applet{ private Rectangle2D.Double myRec; private double x = 100.00;
E N D
Look at applet below: import java.awt.*; import java.awt.geom.Rectangle2D; import java.applet.Applet; public class buttonsApplet extends Applet{ private Rectangle2D.Double myRec; private double x = 100.00; private double y = 100.00; //initial position public buttonsApplet () { // create the rectangle object myRec = new Rectangle2D.Double(x,y,50,50); } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; // redraw whenever necessary g2.setColor(Color.blue); g2.fill(myRec); } }
We want to provide 4 buttons to move the rectangle… an UP button to move it up by 10 pixels a DOWN button to move it down by 10 pixels a LEFT button to move it to the left by 10 pixels a RIGHT button to move it to the right by 10 pixels CODE WOULD NOW HAVE:
public class butApplet2 extends Applet{ Rectangle2D.Double myRec; double x = 100.00; double y = 100.00; //initial position public butApplet2 () { // create the rectangle object myRec = new Rectangle2D.Double(x,y,50,50); //create buttons JButton upbutton = new JButton("UP"); JButton downbutton = new JButton("DOWN"); JButton leftbutton = new JButton("LEFT"); JButton rightbutton = new JButton("RIGHT");
//setup UP button listener class UpListener implements ActionListener { public void actionPerformed (ActionEvent ev){ y = y -10; myRec.setRect(x,y,50,50); repaint(); } } UpListener uplisten = new UpListener(); upbutton.addActionListener(uplisten); //setup DOWN button listener class DownListener implements ActionListener { public void actionPerformed (ActionEvent ev){ y = y + 10; myRec.setRect(x,y,50,50); repaint(); } } DownListener downlisten = new DownListener(); downbutton.addActionListener(downlisten);
//setup left button listener class LeftListener implements ActionListener { public void actionPerformed (ActionEvent ev){ x = x - 10; myRec.setRect(x,y,50,50); repaint(); } } LeftListener leftlisten = new LeftListener(); leftbutton.addActionListener(leftlisten); //setup up button listener class RightListener implements ActionListener { public void actionPerformed (ActionEvent ev){ x = x + 10; myRec.setRect(x,y,50,50); repaint(); } } RightListener rightlisten = new RightListener(); rightbutton.addActionListener(rightlisten);
JPanel panel = new JPanel(); panel.add(upbutton); panel.add(downbutton); panel.add(leftbutton); panel.add(rightbutton); JFrame frame = new JFrame(); frame.setContentPane(panel); frame.pack(); frame.show(); } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.blue); g2.fill(myRec); } }
It would be great to eliminate some of this repetition…. HOW?? Write a method which: instantiates a button defines a listener class creates a listener object registers the button returns the registered button method that attaches the listener
the Method which eliminates repetition… public JButton makeButton(String wording, final int deltax, final int deltay){ JButton button = new JButton(wording); //create button object class TheListener implements ActionListener { //define listener class public void actionPerformed (ActionEvent ev){ y = y + deltay; x = x + deltax; myRec.setRect(x,y,50,50); repaint(); } } TheListener alisten = new TheListener(); //create listener object button.addActionListener(alisten); //register listener object w/ button return button; //returns button w/ listener attached }
public class butApplet3 extends Applet{ // same instance data public butApplet3 () { JButton upbutton = makeButton("UP",0, -10); JButton downbutton = makeButton("DOWN",0,10); JButton leftbutton = makeButton("LEFT", -10,0); JButton rightbutton = makeButton("RIGHT",10,0); myRec = new Rectangle2D.Double(x,y,50,50); JPanel panel = new JPanel(); panel.add(upbutton); panel.add(downbutton); panel.add(leftbutton); panel.add(rightbutton); JFrame frame = new JFrame(); frame.setContentPane(panel); frame.pack(); frame.show(); } • public void paint (Graphics g) { • //still remains the same