简体   繁体   中英

How do I instantiate a paint method?

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

I am trying to instantiate my paint method so that I can use the user inputted array values to use as coordinates for the g.drawLine(). When I try to instantiate this method I am receiving an error on the g in poly.paint(g). Can anyone give me some guidance on how I can resolve this issue?

Should be something like this


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

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

Where you're using g within the containing scope

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