p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Re: "Malformed Expression" amd "illegal Expression" error messages in the SketchView class ! but the expressions don't look malformed or illegal !


Message #1 by dominskis@a... on Mon, 15 Apr 2002 15:47:26
Dear Greg,
  Thank you very much for your reply. With the help of Arthur Ore, the 
errors have been located and I now have a working version of the Sketcher 
program !
thanks again
Stan 





T> he expressions aren't illegal or malformed, but you need to at least 
a> ssume you are getting a compile error for a reason.  Can you at least 
TRY 
t> o look for it, Stan?  

> The compiler might not give you the exact error but it at least is 
telling 
y> ou where to start looking.  Line 108 is not bad, so you need to look at 
t> he lines before it to get at the problem.  You will quickly note that 
you 
f> orgot to put a closing bracket in the 'mouseDragged' method ahead of 
the 
o> ne starting near line 108, 'mouseReleased'.

> 
>>  Dear People,
 > > In doing Ivor Horton's "Beginning Java 2" Chapter 14 page 702
 > > Sketcher program... In the SketchView class I have 
"> > Illegal start of expressions" and "malformed expressions" 
e> > rror messages.
O> > nly the expressions don't look illegal and the expressions do not 
look 
m> > alformed !

> > Below are the error messages and the coding
T> > hanks in advance
S> > stan 

> > 
 > > "SketchView.java": Error #: 204 : illegal start of expression at line 
1> > 08, column 17
"> > SketchView.java": Error #: 206 : malformed expression at line 88, 
c> olumn 
1> > 6
"> > SketchView.java": Error #: 204 : illegal start of expression at line 
1> 61, 
c> > olumn 5
"> > SketchView.java": Error #: 206 : malformed expression at line 108, 
c> olumn 
1> > 7
"> > SketchView.java": Error #: 204 : illegal start of expression at line 
1> 64, 
c> > olumn 5
"> > SketchView.java": Error #: 206 : malformed expression at line 161, 
c> olumn 
5> > 
"> > SketchView.java": Error #: 204 : illegal start of expression at line 
1> 67, 
c> > olumn 5
"> > SketchView.java": Error #: 206 : malformed expression at line 164, 
c> olumn 
5> > 
"> > SketchView.java": Error #: 204 : illegal start of expression at line 
1> 70, 
c> > olumn 5
"> > SketchView.java": Error #: 206 : malformed expression at line 167, 
c> olumn 
5> > 
"> > SketchView.java": Error #: 200 : '}' expected at line 172, column 2

> > 
/> > /Sketcher Program #12

> > import java.awt.*;          //for graphics
i> > mport javax.swing.*;
i> > mport java.util.*;        //for Observer
i> > mport java.awt.geom.*;
i> > mport java.awt.event.*;
i> > mport javax.swing.event.*;   // for mouse input adapter

> >    class SketchView extends JComponent implements Observer,Constants
 > >    {

> >     public SketchView(SketcherCalculator theApp)
 > >     {
 > >       this.theApp = theApp;

> >        //create the mouse listener
 > >       MouseHandler handler = new Mousehandler();

> >        //Listen for button events
 > >       addMouseListener(handler);

> >        //Listen for motion events
 > >       addMouseMotionListener(handler);
 > >    }

> >      //Method called by observable object when it changes
 > >        public void update (Observable o, Object rectangle)
 > >     {
 > >       if(rectangle == null)
 > >        repaint();
 > >        else
 > >          repaint((Rectangle)rectangle);
 > >     }

> >       public void paint(Graphics g)
 > >       {
 > >         //get a 2D device context
 > >         Graphics2D g2D = (Graphics2D)g;
 > >         Iterator elements = theApp.getModel().getIterator();
 > >         Element element;

> >          //go through the list
 > >         while(elements.hasNext())
 > >          {
 > >            //get teh next element
 > >            element = (Element)elements.next();

> >             //set the element color
 > >            g2D.setPaint(element.getColor());

> >             //draw its shape
 > >            g2D.draw(element.getShape());
 > >          }

> >          class Mousehandler extends MouseInputAdapter
 > >          {
 > >            public void mousePressed(MouseEvent e)
 > >             {
 > >               //save the cursor position
 > >               start = e.getPoint();

> >                 //get modifires
 > >                int modifier = e.getModifiers();

> >                 if((modifier & e.BUTTON1_MASK) != 0)
 > >                 {
 > >                   //get graphics context
 > >                   g2D = (Graphics2D)getGraphics();

> >                    // set XOR mode
 > >                   g2D.setXORMode(getBackground());

> >                    //set color
 > >                   g2D.setPaint(theApp.getWindow().getElementColor());

> >                  }
 > >              }

> > 
 > >            public void mouseDragged(MouseEvent e)
 > >             {
 > >               last =   e.getPoint();
 > >               int modifier = e.getModifiers();

> >                if((modifier & e.BUTTON1_MASK) != 0)
 > >                {
 > >                  //is there an element :
 > >                  if(tempElement == null)
 > >                     //no , so create one
 > >                    tempElement =   createElement(start,last);
 > >                  else
 > >                   {
 > >                     // yes - draw to erase it
 > >                     g2D.draw(tempElement.getShape());

> >                      //now modify it
 > >                     tempElement.modify(start,last);

> >                      //draw it
 > >                     g2D.draw(tempElement.getShape());
 > >                   }
 > >                }

> > 
 > >                public void mouseReleased(MouseEvent e)
 > >                 {
 > >                   int modifier =  e.getModifiers();

> >                    if(modifier & e.BUTTON1_MASK != 0)
 > >                    {
 > >                      if(tempElement != null)
 > >                       {
 > >                         // add element to the model
 > >                         the App.getModel().add(tempElement);

> >                          tempElement = null;

> >                        }

> >                        if(g2D != null)
 > >                        {
 > >                          g2D.dispose();
 > >                          g2D = null;
 > >                        }
 > >                       //remove the points
 > >                       start = last = null;
 > >                   }
 > >                }

> > 
 > >             private Element createElement(Point  start, Point end)
 > >              {
 > >                switch(theApp.getWindow().getElementType())
 > >                 {
 > >                   case LINE:
 > >                     return new Element.Line(start, end, 
theApp.getWindow
(> > ).getElementColor());

> >      /*              case RECTANGLE:
 > >                     return new Element.Rectangle(start, end, 
t> > heApp.getWindow().getElementColor());

> >                    case Circle:
 > >                     return new Element.Circle(start, end, 
t> heApp.getWindow
(> > ).getElementColor());

> >                    case CURVE:
 > >                     return new Element.Curve(start, end, 
t> heApp.getWindow
(> > ).getElementColor());

> >       */

> >                  }
 > >                 return null;
 > >            } //end of class MouseHandler

> > 

> >     private SketcherCalculator theApp;

> >     //temporary graphics context
 > >    private Graphics2D g2D;

> >     // Stores cursor     position on press
 > >    private Point start;

> >     //stores cursor position on drag
 > >    private Point last;

> >     //stores a temporary element
 > >    private Element tempElement;
 > > }
}
Message #2 by "Greg Dunn" <greg.dunn@n...> on Mon, 15 Apr 2002 15:09:34
The expressions aren't illegal or malformed, but you need to at least 
assume you are getting a compile error for a reason.  Can you at least TRY 
to look for it, Stan?  

The compiler might not give you the exact error but it at least is telling 
you where to start looking.  Line 108 is not bad, so you need to look at 
the lines before it to get at the problem.  You will quickly note that you 
forgot to put a closing bracket in the 'mouseDragged' method ahead of the 
one starting near line 108, 'mouseReleased'.


> Dear People,
 > In doing Ivor Horton's "Beginning Java 2" Chapter 14 page 702
 > Sketcher program... In the SketchView class I have 
"> Illegal start of expressions" and "malformed expressions" 
e> rror messages.
O> nly the expressions don't look illegal and the expressions do not look 
m> alformed !

> Below are the error messages and the coding
T> hanks in advance
S> stan 

> 
 > "SketchView.java": Error #: 204 : illegal start of expression at line 
1> 08, column 17
"> SketchView.java": Error #: 206 : malformed expression at line 88, 
column 
1> 6
"> SketchView.java": Error #: 204 : illegal start of expression at line 
161, 
c> olumn 5
"> SketchView.java": Error #: 206 : malformed expression at line 108, 
column 
1> 7
"> SketchView.java": Error #: 204 : illegal start of expression at line 
164, 
c> olumn 5
"> SketchView.java": Error #: 206 : malformed expression at line 161, 
column 
5> 
"> SketchView.java": Error #: 204 : illegal start of expression at line 
167, 
c> olumn 5
"> SketchView.java": Error #: 206 : malformed expression at line 164, 
column 
5> 
"> SketchView.java": Error #: 204 : illegal start of expression at line 
170, 
c> olumn 5
"> SketchView.java": Error #: 206 : malformed expression at line 167, 
column 
5> 
"> SketchView.java": Error #: 200 : '}' expected at line 172, column 2

> 
/> /Sketcher Program #12

> import java.awt.*;          //for graphics
i> mport javax.swing.*;
i> mport java.util.*;        //for Observer
i> mport java.awt.geom.*;
i> mport java.awt.event.*;
i> mport javax.swing.event.*;   // for mouse input adapter

>    class SketchView extends JComponent implements Observer,Constants
 >    {

>     public SketchView(SketcherCalculator theApp)
 >     {
 >       this.theApp = theApp;

>        //create the mouse listener
 >       MouseHandler handler = new Mousehandler();

>        //Listen for button events
 >       addMouseListener(handler);

>        //Listen for motion events
 >       addMouseMotionListener(handler);
 >    }

>      //Method called by observable object when it changes
 >        public void update (Observable o, Object rectangle)
 >     {
 >       if(rectangle == null)
 >        repaint();
 >        else
 >          repaint((Rectangle)rectangle);
 >     }

>       public void paint(Graphics g)
 >       {
 >         //get a 2D device context
 >         Graphics2D g2D = (Graphics2D)g;
 >         Iterator elements = theApp.getModel().getIterator();
 >         Element element;

>          //go through the list
 >         while(elements.hasNext())
 >          {
 >            //get teh next element
 >            element = (Element)elements.next();

>             //set the element color
 >            g2D.setPaint(element.getColor());

>             //draw its shape
 >            g2D.draw(element.getShape());
 >          }

>          class Mousehandler extends MouseInputAdapter
 >          {
 >            public void mousePressed(MouseEvent e)
 >             {
 >               //save the cursor position
 >               start = e.getPoint();

>                 //get modifires
 >                int modifier = e.getModifiers();

>                 if((modifier & e.BUTTON1_MASK) != 0)
 >                 {
 >                   //get graphics context
 >                   g2D = (Graphics2D)getGraphics();

>                    // set XOR mode
 >                   g2D.setXORMode(getBackground());

>                    //set color
 >                   g2D.setPaint(theApp.getWindow().getElementColor());

>                  }
 >              }

> 
 >            public void mouseDragged(MouseEvent e)
 >             {
 >               last =   e.getPoint();
 >               int modifier = e.getModifiers();

>                if((modifier & e.BUTTON1_MASK) != 0)
 >                {
 >                  //is there an element :
 >                  if(tempElement == null)
 >                     //no , so create one
 >                    tempElement =   createElement(start,last);
 >                  else
 >                   {
 >                     // yes - draw to erase it
 >                     g2D.draw(tempElement.getShape());

>                      //now modify it
 >                     tempElement.modify(start,last);

>                      //draw it
 >                     g2D.draw(tempElement.getShape());
 >                   }
 >                }

> 
 >                public void mouseReleased(MouseEvent e)
 >                 {
 >                   int modifier =  e.getModifiers();

>                    if(modifier & e.BUTTON1_MASK != 0)
 >                    {
 >                      if(tempElement != null)
 >                       {
 >                         // add element to the model
 >                         the App.getModel().add(tempElement);

>                          tempElement = null;

>                        }

>                        if(g2D != null)
 >                        {
 >                          g2D.dispose();
 >                          g2D = null;
 >                        }
 >                       //remove the points
 >                       start = last = null;
 >                   }
 >                }

> 
 >             private Element createElement(Point  start, Point end)
 >              {
 >                switch(theApp.getWindow().getElementType())
 >                 {
 >                   case LINE:
 >                     return new Element.Line(start, end, theApp.getWindow
(> ).getElementColor());

>      /*              case RECTANGLE:
 >                     return new Element.Rectangle(start, end, 
t> heApp.getWindow().getElementColor());

>                    case Circle:
 >                     return new Element.Circle(start, end, 
theApp.getWindow
(> ).getElementColor());

>                    case CURVE:
 >                     return new Element.Curve(start, end, 
theApp.getWindow
(> ).getElementColor());

>       */

>                  }
 >                 return null;
 >            } //end of class MouseHandler

> 

>     private SketcherCalculator theApp;

>     //temporary graphics context
 >    private Graphics2D g2D;

>     // Stores cursor     position on press
 >    private Point start;

>     //stores cursor position on drag
 >    private Point last;

>     //stores a temporary element
 >    private Element tempElement;
 > }
}
Message #3 by dominskis@a... on Fri, 12 Apr 2002 21:37:30
Dear People,
 In doing Ivor Horton's "Beginning Java 2" Chapter 14 page 702
 Sketcher program... In the SketchView class I have 
"Illegal start of expressions" and "malformed expressions" 
error messages.
Only the expressions don't look illegal and the expressions do not look 
malformed !

Below are the error messages and the coding
Thanks in advance
Sstan 


 "SketchView.java": Error #: 204 : illegal start of expression at line 
108, column 17
"SketchView.java": Error #: 206 : malformed expression at line 88, column 
16
"SketchView.java": Error #: 204 : illegal start of expression at line 161, 
column 5
"SketchView.java": Error #: 206 : malformed expression at line 108, column 
17
"SketchView.java": Error #: 204 : illegal start of expression at line 164, 
column 5
"SketchView.java": Error #: 206 : malformed expression at line 161, column 
5
"SketchView.java": Error #: 204 : illegal start of expression at line 167, 
column 5
"SketchView.java": Error #: 206 : malformed expression at line 164, column 
5
"SketchView.java": Error #: 204 : illegal start of expression at line 170, 
column 5
"SketchView.java": Error #: 206 : malformed expression at line 167, column 
5
"SketchView.java": Error #: 200 : '}' expected at line 172, column 2


//Sketcher Program #12

import java.awt.*;          //for graphics
import javax.swing.*;
import java.util.*;        //for Observer
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.event.*;   // for mouse input adapter

   class SketchView extends JComponent implements Observer,Constants
    {

    public SketchView(SketcherCalculator theApp)
     {
       this.theApp = theApp;

       //create the mouse listener
       MouseHandler handler = new Mousehandler();

       //Listen for button events
       addMouseListener(handler);

       //Listen for motion events
       addMouseMotionListener(handler);
    }

     //Method called by observable object when it changes
        public void update (Observable o, Object rectangle)
     {
       if(rectangle == null)
        repaint();
        else
          repaint((Rectangle)rectangle);
     }

      public void paint(Graphics g)
       {
         //get a 2D device context
         Graphics2D g2D = (Graphics2D)g;
         Iterator elements = theApp.getModel().getIterator();
         Element element;

         //go through the list
         while(elements.hasNext())
          {
            //get teh next element
            element = (Element)elements.next();

            //set the element color
            g2D.setPaint(element.getColor());

            //draw its shape
            g2D.draw(element.getShape());
          }

         class Mousehandler extends MouseInputAdapter
          {
            public void mousePressed(MouseEvent e)
             {
               //save the cursor position
               start = e.getPoint();

                //get modifires
                int modifier = e.getModifiers();

                if((modifier & e.BUTTON1_MASK) != 0)
                 {
                   //get graphics context
                   g2D = (Graphics2D)getGraphics();

                   // set XOR mode
                   g2D.setXORMode(getBackground());

                   //set color
                   g2D.setPaint(theApp.getWindow().getElementColor());

                 }
              }


            public void mouseDragged(MouseEvent e)
             {
               last =   e.getPoint();
               int modifier = e.getModifiers();

               if((modifier & e.BUTTON1_MASK) != 0)
                {
                  //is there an element :
                  if(tempElement == null)
                     //no , so create one
                    tempElement =   createElement(start,last);
                  else
                   {
                     // yes - draw to erase it
                     g2D.draw(tempElement.getShape());

                     //now modify it
                     tempElement.modify(start,last);

                     //draw it
                     g2D.draw(tempElement.getShape());
                   }
                }


                public void mouseReleased(MouseEvent e)
                 {
                   int modifier =  e.getModifiers();

                   if(modifier & e.BUTTON1_MASK != 0)
                    {
                      if(tempElement != null)
                       {
                         // add element to the model
                         the App.getModel().add(tempElement);

                         tempElement = null;

                       }

                       if(g2D != null)
                        {
                          g2D.dispose();
                          g2D = null;
                        }
                       //remove the points
                       start = last = null;
                   }
                }


             private Element createElement(Point  start, Point end)
              {
                switch(theApp.getWindow().getElementType())
                 {
                   case LINE:
                     return new Element.Line(start, end, theApp.getWindow
().getElementColor());

     /*              case RECTANGLE:
                     return new Element.Rectangle(start, end, 
theApp.getWindow().getElementColor());

                   case Circle:
                     return new Element.Circle(start, end, theApp.getWindow
().getElementColor());

                   case CURVE:
                     return new Element.Curve(start, end, theApp.getWindow
().getElementColor());

      */

                 }
                 return null;
            } //end of class MouseHandler



    private SketcherCalculator theApp;

    //temporary graphics context
    private Graphics2D g2D;

    // Stores cursor     position on press
    private Point start;

    //stores cursor position on drag
    private Point last;

    //stores a temporary element
    private Element tempElement;
 }
}

  Return to Index