java窗口棋盘_棋盘中心和在java中调整大小

本文介绍了一个Java棋盘程序,展示了如何创建一个8x8的棋盘。当前的问题是如何使棋盘在窗口中居中并对调整窗口大小做出响应,保持棋格比例。代码示例中,棋盘由`CheckerboardComponent`类实现,使用`Graphics2D`进行绘制。`CheckersViewer`类则创建了一个包含棋盘组件的JFrame,并设置其大小和标题。为了实现棋盘的居中和自适应调整大小,需要修改棋盘组件的相关逻辑。
摘要由CSDN通过智能技术生成

我写了一个棋盘程序(如下所示)。我的问题是我无法弄清楚如何使用调整大小来居中,并让它按比例调整大小。

我在一份简短的声明中补充道。 Int调整大小(如下所示)我做了一些类似于以前使用半径的靶心的程序。我只是没有丝毫的线索如何在这里实现它。

import java.awt.*;

import javax.swing.JComponent;

public class CheckerboardComponent extends JComponent {

@Override

public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.RED);

int s = 12;

int x = s;

int y = s;

// int resize = Math.min(this.getHeight(), this.getWidth()) / 8 ;

for (int i = 0; i < 8; i++) {

// one row

for (int j = 0; j < 8; j++) {

g2.fill(new Rectangle(x, y, 4 * s, 4 * s) );

x += 4 * s;

if(g2.getColor().equals(Color.RED)){

g2.setColor(Color.BLACK);

}else{

g2.setColor(Color.RED);

}

}

x = s;

y += 4 * s;

if(g2.getColor().equals(Color.RED)){

g2.setColor(Color.BLACK);

}else{

g2.setColor(Color.RED);

}

}

}

}

这是一个观众程序

import javax.swing.*;

public class CheckersViewer {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setSize(430, 450);

frame.setTitle("Checkerboard");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

CheckerboardComponent component = new CheckerboardComponent();

frame.add(component);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值