如何将Graphics变量从主类传递给方法并仍然可以使用它?

时间:2014-08-26 15:49:43

标签: java graphics awt

我试图在我的" Bar"上课,但我不确定要传递给" draw"来自主类的方法。当我试图通过" Graphics"变量到方法,它给我一个错误。

import javax.swing.JOptionPane;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class CodeMain extends Applet {

    static String inputZipCode = JOptionPane.showInputDialog("Enter 5-digit Zip Code:");
    static int firstDigit = Integer.parseInt(inputZipCode.substring(0,1));
    static int secondDigit = Integer.parseInt(inputZipCode.substring(1,2));
    static int thirdDigit = Integer.parseInt(inputZipCode.substring(2,3));
    static int fourthDigit = Integer.parseInt(inputZipCode.substring(3,4));
    static int fifthDigit = Integer.parseInt(inputZipCode.substring(4,5));

    public static void main(String[] args) {
        Bar barGraphic = new Bar();

        int checkDigit = zipCodeDigits.findCheckDigit(inputZipCode);

        String firstDigitCode = zipCodeDigits.digitCode(firstDigit);
        String secondDigitCode = zipCodeDigits.digitCode(secondDigit);
        String thirdDigitCode = zipCodeDigits.digitCode(thirdDigit);
        String fourthDigitCode = zipCodeDigits.digitCode(fourthDigit);
        String fifthDigitCode = zipCodeDigits.digitCode(fifthDigit);
        String checkDigitCode = zipCodeDigits.digitCode(checkDigit);

        barGraphic.draw(firstDigitCode);
    }

}




import java.awt.*;
import java.awt.geom.Line2D;

public class Bar {

    void draw (Graphics g, String digitCode) {
        Graphics2D g2 = (Graphics2D)g;

        Line2D.Double bar = new Line2D.Double(50, 50, 30, 30);
        g2.draw(bar);
    }

}

0 个答案:

没有答案