2013-11-24 27 views
0
import java.awt.Graphics; 
import java.util.Scanner; 
import javax.swing.JApplet; 

public class Polygon extends JApplet{ 

    public static void main (String[] args) { 

    int i,j; 

    int poly[]=new int[6]; 
    System.out.println("Enter 3 pairs of coordinates for the polygon:"); 
    Scanner scan = new Scanner (System.in); 

    for (i=0;i<poly.length;i++) { 
     poly[i]=scan.nextInt(); 
    } 

    Polygon polygon = new Polygon(); 
    poly.paint(g); 
    } 

    public void paint(Graphics g) { 
    g.drawLine(20, 20, 200, 200); 
    } 
} 

我想實例化我的繪畫方法,以便我可以使用用戶輸入的數組值作爲g.drawLine()的座標。當我嘗試實例化這種方法時,我收到了poly.paint(g)中g的錯誤。任何人都可以給我一些指導我如何解決這個問題?如何實例化繪畫方法?

+2

實例化的概念適用於類,而不是方法中使用g。 –

+1

你還沒有定義'g'在那裏。 –

+1

你確實需要閱讀關於Java的入門教程。然後練習不涉及Swing的簡單練習。然後,閱讀關於Swing的入門教程。 –

回答

0
Should be something like this 


public void paint(Graphics g) { 
    g.drawLine(20, 20, 200, 200); 

    Polygon polygon = new Polygon(); 
    poly.paint(g); 
} 

如果你的包含範圍