0

When i tried this following error came Syntax error on token "(", ; expected Syntax error on token ")", ; expected

at ButtonTest.actionPerformed(ButtonTest.java:58)

import java.awt.*;
import java.awt.event.*;//step-1
import java.applet.Applet;

public class ButtonTest extends Applet implements ActionListener//step-2
{
    Button b1,b2,b3;
    Font f;
    Graphics gc;
    public void init()
    {
            b1=new Button("Request");
            b2=new Button("Grant");
            b3=new Button("Accept");

            f=new Font("Arial",Font.BOLD,12);

            b1.setFont(f);
            b2.setFont(f);
            b3.setFont(f);

            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);

            add(b1);
            add(b2);
            add(b3);
    }

    public void paint(Graphics gc) 
    {
        gc.drawLine(100, 150, 100, 400); 
        gc.drawLine(300, 150, 300, 400); 
        gc.drawOval(95, 155, 10, 10);  //1.1
        gc.drawOval(95, 225, 10, 10);  //1.2
        gc.drawOval(95, 295, 10, 10);  //1.3
        gc.drawOval(95, 365, 10, 10);  //1.4
        gc.drawOval(295, 155, 10, 10);  //2.1
        gc.drawOval(295, 225, 10, 10);  //2.2
        gc.drawOval(295, 295, 10, 10);  //2.3
        gc.drawOval(295, 365, 10, 10);  //2.4

    }
    public void myPaint(Graphics gc)  // this line is not working*******???????
                {
                    gc.drawLine(95, 155, 295, 225);  //1.1 to 2.2
                    gc.drawLine(95, 295, 295, 225);  //1.3 to 2.2
                    gc.drawLine(95, 295, 295, 365);  //1.3 to 2.4
                    gc.drawString(">>>", 260, 220); 
                    gc.drawString(">>>", 218, 255);
                    gc.drawString(">>>", 267, 365);
                } 
    public void actionPerformed(ActionEvent ae)
    {
            if(ae.getSource()==b1)
            {
                    myPaint(gc);     //this line is not working
                    setBackground(Color.red);

             }
            else if(ae.getSource()==b2)
            {
                 setBackground(Color.green);
            }
            else{
                 setBackground(Color.blue);
            }

     }
}
/*<applet code="ButtonTest" width=300 height=300>

*/

error is Syntax error on token "(", ; expected Syntax error on token ")", ; expected

at ButtonTest.actionPerformed(ButtonTest.java:58)

prav
  • 165
  • 1
  • 2
  • 8
  • What kind of Java do you use. In the method `actionPerformed(ActionEvent ae)` you define another method `public void paint(Graphics gc)`. This is impossible – drkunibar Oct 02 '14 at 21:51
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Oct 04 '14 at 02:09

1 Answers1

0

There a few mistakes in your code. After you have edited your code the line

public void myPaint(Graphics gc)

works. The line

myPaint();

does not work because myPaint needs an argument of type Graphics. The method call should look like

myPaint(gc);

I don't know what you really want to do, but you can get the canvas of the applet by calling

this.getGraphics()

There is no need to use gc as an argument in myPaint or to store Graphic as a member in inside your class.

Hope that helps

drkunibar
  • 1,327
  • 1
  • 7
  • 7