0

I need to get the inputs from the scanner in the method main, into the method paint, and I do not know how to do that. Normally I can just do instance.paint(); But it isn't working. I think i have to do something with the parameters in the paint method but i don't know what. Please help.

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 );
    }
}
  • What are your expected input and output? – synchronizer Feb 09 '17 at 03:40
  • No, no you don't. You either want to pass the values as parameters via the command line or generate a UI which can collect that information in a more appropriate manner. Also, don't override `paint` without having a much better idea of what it does, instead, override `paintComponent` and call it's super method before performing any custom painting. Painting is for painting, it should not update or modify the state, so `ChessSet` probably doesn't belong there – MadProgrammer Feb 09 '17 at 03:41
  • Painting is controlled by the System, you don't have any control over it, you `paint` method will be called when the system deems it needs to. You need to make your "values" instance variables which will allow you paint routines to access it - no offense, but this is Java 101 – MadProgrammer Feb 09 '17 at 03:42
  • @MadProgrammer they don't call you mad for nothing! – weston Feb 09 '17 at 03:43
  • Did you try using a `JFrame` and then add your JPanel to it. See "https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html" here. You are missing `Frame.pack` – Siddharth Tyagi Feb 09 '17 at 03:43
  • I need the input of the number of pawns and rooks in the paint method. and then when i get them there i can do the draw strings with the correct values and then do if statements to print the correct amount. –  Feb 09 '17 at 03:43
  • @SiddharthTyagi You mean like `JFrame ourFrame = new JFrame();` and `ourFrame.add( ourChessSet );`? Then I'd say, yes, yes they did - and they use `setSize` instead :P – MadProgrammer Feb 09 '17 at 03:44
  • I am sorry, I updated my comment - it seems Frame.pack is missing. – Siddharth Tyagi Feb 09 '17 at 03:45
  • @SiddharthTyagi They are using `setSize` instead, which in their case will work, as they've not overriden `getPreferredSize` of the panel – MadProgrammer Feb 09 '17 at 03:47
  • @jonjoe There are so many ways you might achieve this that doesn't require custom painting. You could use `JLabel`s or `JButtons`, for [example](http://stackoverflow.com/questions/22626640/making-a-chess-board-out-of-jbuttons/22626753#22626753) or [example](http://stackoverflow.com/questions/35710453/images-in-jframe-are-overwriting-each-other-not-displaying-both-images-over-each/35710488#35710488) or if your feeling REALLY brave [example](http://stackoverflow.com/questions/13698217/how-to-make-draggable-components-with-imageicon/13700490#13700490) – MadProgrammer Feb 09 '17 at 03:50
  • @jonjoe If you're really interested in knowing how painting works, take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Feb 09 '17 at 03:51
  • Here is my hole code. I have to take figures i have drawn and ask the user how many pawns they want and then draw that many pawns using if statements in the paint method to print the right amount. –  Feb 09 '17 at 03:51
  • Is there a direct way you can just tell me what i need to do to get my input ints into the method paint @MadProgrammer –  Feb 09 '17 at 03:53
  • @jonjoe Make the variables you want to use instance fields of the `ChessSet` – MadProgrammer Feb 09 '17 at 03:55
  • I am not 100% sure, but don't you need to call super.paint() in your overridden method? – Siddharth Tyagi Feb 09 '17 at 03:57

0 Answers0