命令行输入

时间:2019-03-22 00:36:20

标签: java grid jframe jpanel

有什么方法可以只从拖动中获得初始按动,因为现在当在同一正方形上拖动时,它可以在黑白之间切换。另外,我正在尝试从命令行获取将构成网格的矩形的宽度和高度,如果用户拧紧或未输入任何内容,则默认将它们设置为50。我尝试创建一个方法是因为我真的不知道如何将它们放置在主体中,然后在JPanel中使用它。

public class Clicky extends JFrame {

private static class Board extends JPanel {

    public int BRICK_WIDTH = 50;
    public int BRICK_HEIGHT = 50;
    public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    public double width = screenSize.getWidth();
    public double height = screenSize.getHeight();
    public double bWidth;
    public double bHeight;
    private int COLS = (int) (width / bWidth);
    private int ROWS = (int) (height / bHeight);
    private Color CO = Color.BLACK;

    private boolean[][] isWhite = new boolean[COLS + 1][ROWS + 1];

    public Board() {


        System.out.println("WIdth:" + COLS + "Height:" + ROWS);
        setBackground(Color.BLACK);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mx = e.getX();
                my = e.getY();
                System.out.printf("X: %d Y: %d ", mx, my);
                isWhite[(int) (mx / bWidth)][(int) (my
                        / bHeight)] = !isWhite[(int) (mx / bWidth)][(int) (my / bHeight)];
                repaint();
            }
        });

        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                mx = e.getX();
                my = e.getY();
                isWhite[(int) (mx / bWidth)][(int) (my
                        / bHeight)] = !isWhite[(int) (mx / bWidth)][(int) (my / bHeight)];
                int gridx = e.getX();
                int gridy = e.getY();
                System.out.println(gridx);
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });
    }

    @Override
    protected void paintComponent(Graphics g,String[] args ) {
        super.paintComponent(g);

        drawBricks(g);
        getValues(args);
    }

    private double x;
    private double y;

    public void getValues(String[] args){
         try
         {
             bWidth = Integer.valueOf(args[0]);
         }

         catch (IndexOutOfBoundsException | NumberFormatException ex)
         {
    // If the argument was bad then use the default.
         bWidth = BRICK_WIDTH;
         }

         try
         {
             bHeight = Integer.valueOf(args[1]);
        }
        catch (IndexOutOfBoundsException | NumberFormatException ex)
        {
    // If the argument was bad then use the default.
         bHeight = BRICK_HEIGHT;
        }
}
       private void drawBricks(Graphics g) {

        Graphics2D brick = (Graphics2D) g.create();
        x = 0;
        y = 0;
        for (int j = 0; j <= ROWS; j++) {
            for (int a = 0; a <= COLS; a++) {
                if (isWhite[a][j]) {
                    brick.setColor(Color.WHITE);
                } else {
                    brick.setColor(Color.BLACK);
                }
                Rectangle2D.Double rect = new Rectangle2D.Double(x, y, bWidth, bHeight);
                brick.fill(rect);

                brick.setColor(Color.gray);
                brick.draw(rect);
                x += bWidth;
            }
            repaint();
            x = 0;
            y += bHeight;
        }

    }

    public int mx = -100;
    public int my = -100;
}

public Clicky() {

    setDefaultCloseOperation(EXIT_ON_CLOSE); // mai bine cu exit on close
    setSize(800, 820);
    add(new Board());
}

public static void main(String[] args) {

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Clicky().setVisible(true);
        }
    });
}
}

1 个答案:

答案 0 :(得分:1)

没有可以监听的事件,它将告诉您拖动事件何时开始。您可以自己写一个。您将需要某种有状态变量,可以在第一次接收到鼠标拖动事件时进行切换,然后将其用作后续鼠标拖动事件的防护。您可以在释放鼠标事件时重置有状态变量。参见MouseEventMouseEventListener

关于您的其他问题-我不太确定您要问什么。您是否想知道如何从命令行获取用户提供的数据,或者试图弄清楚提供后如何使用它们?

对于前者,command line arguments非常简单。对于后者,您所要做的就是编写一个setter方法,该方法将验证输入并在调用方提供的值有效时覆盖默认值。