Java调用方法不起作用

时间:2017-02-09 03:38:04

标签: java

我需要从方法main中的扫描仪获取输入到方法绘制中,我不知道该怎么做。通常我可以做instance.paint();但它不起作用。我想我必须对paint方法中的参数做些什么,但我不知道是什么。请帮忙。

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ChessSet extends JPanel
{
    public static void main()
    {
        ChessSet inst1 = new ChessSet();
        Scanner key = new Scanner( System.in );
        System.out.println( "How many pawns would you like? (0-4): " );
        int numPawns = key.nextInt();

        System.out.println( "How many bishops would you like? (0-4): " );
        int numBishops = key.nextInt();


        JFrame ourFrame = new JFrame();
        ourFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        ourFrame.setSize(1000,1000);

        ChessSet ourChessSet = new ChessSet();
        ourFrame.add( ourChessSet );
        ourFrame.setBackground( Color.white );

        ourFrame.setVisible(true);


    }

    public void paint( Graphics canvas )
    {
        ChessSet inst1 = new ChessSet();
        this.drawPawn( canvas, 100, 140, Color.black );
        this.drawKing( canvas, 375, 50, Color.black );
        this.drawRook( canvas, 600, 110, Color.black );
        this.drawQueen( canvas, 100, 535, Color.black );
        this.drawKnight( canvas, 350,480, Color.black );
        this.drawBishop( canvas, 700, 480, Color.black );

        canvas.drawString( "King 1", 10, 10 );
        canvas.drawString( "Rook 1", 10, 30 );
        canvas.drawString( "Queen 1", 10, 20 );  
        canvas.drawString( "Pawn " + numPawn, 100, 10 );
        canvas.drawString( "Bishop " + numBishop, 100, 30 );
        canvas.drawString( "Knight 1", 100, 20 );
    }
}

0 个答案:

没有答案