2013-12-13 43 views
0

我试图用线条填充三角形。挑战不在于使用填充多边形,而是使用循环并填充线条。无论如何,我还没有弄清楚这个循环。我将创建ints并使用for循环添加到它们。填充三角形/编译器错误

我还设置了休息了,我得到这个荒谬的编译器错误,所有的地方:

error: not a statement 

error: ';' expected 

这很奇怪,因为我已经初始化的整数。有 ;在那里。

import javax.swing.JApplet; 
import java.awt.*; 

public class Tri extends JApplet 
    { 


    public static void main(String[] args) 
    { 

     int 1x = 0; 
     int 1y = 140; 
     int 2x = 120; 
     int 2y = 140; 
     int 3x = 60; 
     int 3y =0; 

       public void paint (Graphics page) 
       { 

        page.drawLine (1x, 1y, 2x, 2y); 
        page.drawLine (2x, 2y, 3x, 3y); 
        page.drawLine (3x, 3y, 1x, 1y); 

       } 

    } 
    } 
+2

'import java.awt。*'?我希望你不打算在你的代码中的任何地方使用'List' ... – corsiKa

回答

3

您的变量不能以数字开头。

您不能嵌套方法。 (我想你可能会考虑嵌套类,这是允许的)

您需要确保您声明的变量在范围内。

public class App { 
    int x1 = 0;   //<<<<<<------change your variable names 
    int y1 = 140; 
    int x2 = 120; 
    int y2 = 140; 
    int x3 = 60; 
    int y3 =0; 

public static void main(String[] args) 
    { 

     App app = new App();  //instantiate an instance 
     app.paint(g); //dunno where you get g, but paint needs to be its own method. 




    } 

    public void paint (Graphics page) 
       { 

        page.drawLine (1x, 1y, 2x, 2y); 
        page.drawLine (2x, 2y, 3x, 3y); 
        page.drawLine (3x, 3y, 1x, 1y); 

       } 
} 
+0

我拿出公共静态void main,并运行。 – munchschair