我做了一个使用图形的功能,我想在主要调用它它不起作用

时间:2015-06-10 13:33:29

标签: java graphics applet

主要:

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);



}

App类:

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);

}

如果我将该函数移动到可以工作的绘制,但是它无法接收颜色x和y,并且我无法在main中调用函数多少次。 我尝试了很多方法,但我不能,请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试将Graphics对象传递到drawBow中的main

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