Shape problem

From: Ian Stanley (ianthestan_at_hotmail.com)
Date: 12/28/03


Date: Mon, 29 Dec 2003 00:22:16 +1030

hi,
Am posting here as suggested by other java group
continuing a long standing problem of mine(cutdown version code below) - I
am getting a compiler error which tells me that:
MyShape should be declared abstract; it does not define getbounds2D() in
myShape.
I am aware that there are ten methods in class Shape that need to be
defined. However, I have tried defining the specified method to return a
rectangle
but can't seem to get it right as just get more errors and don't know where
to go from here.
Tried extending rectangle but must be doing something wrong here as well.
Also, declaring the class abstract does not work in this case as the problem
moves to the sub-class.
I need to keep this class strucure as I have many more methods & classes
which perform various operations etc(my full version works OK as it does not
use inheritance).
Is there a way around without implementing Shape?
The idea in this example is to draw rectangles and after selecting a rotate
mode button and clicking inside a drawn rectangle this shape is rotated.
Any help appreciated as to what I have done wrong, at least help to get it
to the point where it compiles and works so I can move on?
Thanking you in advance
regards
Ian

   import java.awt.*;
   import java.awt.event.*;
   import java.awt.geom.*;
   import java.util.*;
   import javax.swing.*;
   public class rot2
   {
      public static void main(String[] args)
      {
         Draw D = new Draw() ;
      }
   }
   interface Constants {
      final int RECTANGLE = 1, ROTATE = 2, w = 100, h = 75;
   }
   class Draw extends JFrame
   implements Constants, ActionListener{
      JPanel bp = new JPanel() ;
      DrawJPanel drawPanel ;
      Container content = getContentPane() ;
      JButton rectButton, rotateButton, quitButton;
      Draw() {
         super() ;
         content.setLayout(new BorderLayout()) ;
         drawPanel = new DrawJPanel() ;
         bp.add(rectButton = new JButton("Rectangle")) ;
         bp.add(rotateButton = new JButton("rotate")) ;
         bp.add(quitButton = new JButton("Quit")) ;
         quitButton.addActionListener(
                                 new ActionListener(){
                                    public void actionPerformed(ActionEvent
                                                      e) {
                                       System.exit(0) ;
                                    }
                                 }) ;
         rectButton.addActionListener(this) ;
         rotateButton.addActionListener(this) ;
         content.add(bp, BorderLayout.NORTH) ;
         content.add(drawPanel, BorderLayout.CENTER) ;
         setSize(750, 500) ;
         setVisible(true) ;
      }
      public void actionPerformed(ActionEvent e) {
         String cmd = e.getActionCommand() ;
         switch (cmd.charAt(0)) {
            case 'R':
               drawPanel.setMode(RECTANGLE) ;
               break ;
            case 'r':
               drawPanel.setMode(ROTATE) ;
               break ;
         }
      }
   }
   class DrawJPanel extends JPanel
   implements Constants
   {
      Vector v = new Vector();
      MyShape current_shape;
      int mode;
      int newMode;
      private int x1, y1, x2, y2;
      public DrawJPanel()
      {
         addMouseListener(
                            new MouseAdapter() {
                               public void mousePressed(MouseEvent evt)
                               {
                                  int x = evt.getX();
                                  int y = evt.getY();
                                  if (mode == RECTANGLE){
                                     v.add(new Rectangle(x, y, w, h));
                                     repaint();
                                  }
                                  else if (mode == ROTATE){
                                     if(findMyShape(x,y)!=false){
                                        rotateMyShape(current_shape,x,y);
                                        repaint();
                                     }
                                  }
                               }
                            }
                         );
      }
      void setMode(int newMode) {
         mode =newMode ;
      }
      boolean findMyShape(int x, int y)
      {
         for (int i = v.size()-1; i >= 0; i-- ) // start at end of Vector
         {
            MyShape s = (MyShape)v.elementAt(i);
            if( s.contains(x, y) )
            {
               current_shape = s;
               return true;
            }
         }
         return false;
      }
      public MyShape rotateMyShape(MyShape current_shape, int x, int y)
      {
         AffineTransform tr =
         AffineTransform.getRotateInstance(Math.toRadians(45), x, y);
         Shape ts = tr.createTransformedShape(current_shape);
         v.addElement(ts);
         v.removeElement(current_shape);
         repaint();
         return current_shape;
      }

      public void paintComponent(Graphics gr)
      {
         super.paintComponent(gr);
         Graphics2D g = (Graphics2D)gr;
         for(int i=0; i<v.size(); i++)
            g.draw((MyShape)v.elementAt(i));
      }
   }
   class MyShape extends JComponent implements Shape {
      int x, y ;
      int w, h ;
      MyShape(int x, int y, int w, int h) {
         super() ;
         this.x = x ;
         this.y = y ;
         this.w = w ;
         this.h = h ;
      }
   }
   class Rectangle extends MyShape {
      Rectangle(int x, int y, int w, int h) {
         super(x, y, w, h) ;
      }
      public void paint(Graphics g) {
         g.setColor(Color.BLUE) ;
         g.drawRect(x, y, w-1, h-1) ;
      }
   }


Quantcast