gpt4 book ai didi

java - 命令行输入

转载 作者:行者123 更新时间:2023-12-02 10:06:45 24 4
gpt4 key购买 nike

有没有办法只从拖动中获得初始按下,因为现在当拖动到同一个方 block 上时它会在黑色和白色之间切换。另外,我试图从命令行获取将构成网格的矩形的宽度和高度,如果用户搞砸了或者没有输入任何内容,那么它们默认设置为 50。我尝试创建一个方法,因为我真的不知道如何将它们放入 main 中,然后在 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);
}
});
}
}

最佳答案

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

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

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

关于java - 命令行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55291226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com