简体   繁体   English

如何实例化绘画方法?

[英]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(). 我正在尝试实例化我的绘画方法,以便可以使用用户输入的数组值作为g.drawLine()的坐标。 When I try to instantiate this method I am receiving an error on the g in poly.paint(g). 当我尝试实例化此方法时,我在poly.paint(g)中的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 在包含范围内使用g的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM