简体   繁体   中英

Draw line after a button pressed in java applet

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM