I made a function that uses graphics and I wanted to call it in the main it did not work

83 views Asked by At

The main:

public class AppMain {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    App a1 = new App();

    JFrame f2 = new JFrame(); 

    f2.getContentPane().add(a1);

    f2.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            a1.stop();
            a1.destroy();
            System.exit(0);
        }
    });

    f2.setVisible(true);

    f2.setSize(200, 200);

    a1.setBackground(Color.white);

    //the function
    //a1.drawBow(null, 40, 40, Color.BLACK);
    a1.drawBow(a1.getGraphics(), 40, 40, Color.BLACK);



}

The App class:

public class App extends Applet{


int x1,y1,x2,y2;

public void paint(Graphics g1) {


        g1.setPaintMode();

        g1.setColor(Color.BLUE);

        g1.drawString("Hello!", 50, 50);

        g1.setColor(Color.red);

        g1.drawString("Hello!", 51, 51);

        g1.setColor(Color.RED);

        g1.draw3DRect(80, 70, 20, 20, true);

        g1.setColor(Color.black);

        g1.fill3DRect(81, 71, 19, 19, true);

        g1.setColor(Color.blue);

        g1.fill3DRect(82, 72, 17, 17, true);


}

public void init() {

    setSize(200, 200);

    setVisible(true);

    repaint();


}

//the function
public void drawBow(Graphics g1,int x , int y,Color c){

    g1.setColor(c);
    x1=x;
    y1=y;
    x2=x1+10;
    y2=y1+10;
    g1.drawLine(x1,y1,x2,x2);
    x1=x2+10;
    y1=y2+35;
    g1.drawLine(x2,y2,x1,y1);
    x2=x1-10;
    y2=y1+35;
    g1.drawLine(x1,y1,x2,y2);
    x1=x2-10;
    y1=y2+10;
    g1.drawLine(x2,y2,x1,y1);
    x2=x1;
    y2=y1-90;
    g1.drawLine(x1,y1,x2,y2);

}

If I move the function to paint that will work, but it can not receive a color x and y, and I can not call a function how many times I want in main. I tried a lot of ways and I could not, please help.

1

There are 1 answers

1
Tushar On

Try passing a Graphics object to drawBow in main:

//the function
a1.drawBow(a1.getGraphics(), 40, 40, Color.BLACK);