- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
初级 Java 开发人员。尝试制作一个俄罗斯方 block 小程序作为我个人项目的一部分。
我现在可以在屏幕上绘制俄罗斯方 block ,但我无法让它每秒垂直向下。
代码:
public class InitialScreen extends JApplet implements ActionListener {
public JPanel cards = new JPanel();
private JPanel introPanel = new JPanel();
public CardLayout c1 = new CardLayout();
public void init() {
initiateIntroScreen();
//game();
add(cards, BorderLayout.NORTH);
setSize(500, 100);
}
private void initiateIntroScreen() {
Frame title = (Frame)this.getParent().getParent();
cards.setLayout(c1);
JLabel centralWords = new JLabel("Click the following button options: 'Play' or 'Instructions'.");
JButton playBtn = new JButton("Play!");
JButton instructionsBtn = new JButton("Instructions!");
introPanel.add(centralWords);
introPanel.add(playBtn);
introPanel.add(instructionsBtn);
cards.add(introPanel,"1");
playBtn.addActionListener(this);
playBtn.addActionListener(new MainGame(cards,c1));
}
@Override
public void actionPerformed(ActionEvent e) {
setSize(300,410);
getContentPane().setBackground(Color.BLACK);
}
这就是 JApplet 的初始屏幕。有两个按钮。当您按下“播放”按钮时,它将进入主游戏屏幕。
public class MainGame extends JApplet implements ActionListener {
private JPanel cards;
private CardLayout c1;
private JPanel gamePanel = new JPanel();
public MainGame(JPanel cards, CardLayout c1) {
this.c1 = c1;
this.cards = cards;
gamePanel.add(new Tetris_Block(new int[10][20]));
}
@Override
public void actionPerformed(ActionEvent e) {
JLabel scoreLbl = new JLabel("Score:");
gamePanel.add(scoreLbl);
cards.add(gamePanel,"game");
c1.show(cards,"game");
}
这是玩俄罗斯方 block 的游戏画面。在构造函数中,它调用俄罗斯方 block block 。
public class Tetris_Block extends JComponent implements ActionListener {
static Color[] colors =
{darkGray, green, blue, red,
yellow, magenta, pink, cyan};
int[][] a;
int w, h;
static int horizontalPos, verticalPos = 0;
static int size = 20;
private int verticalPos1 = 1;
public Tetris_Block(int[][] a) {
this.a = a;
w = a.length;
h = a[0].length;
square_Block();
startTimer();
}
private void nextMove() {
verticalPos++;
verticalPos1++;
}
public void square_Block(){ //Horizontal || Vertical || Colour
//Horizontal never changes for this as I just want the blocks to go down.
a[0][verticalPos] = 3;
a[0][verticalPos1] = 3;
a[1][verticalPos] = 3;
a[1][verticalPos1] = 3;
}
@Override
public void actionPerformed(ActionEvent e) {
nextMove();
square_Block();
System.out.println(verticalPos);
}
public void startTimer(){
Timer timer = new Timer(1000,this);
timer.start();
}
public void paintComponent(Graphics g) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
g.setColor(colors[a[i][j]]);
g.fill3DRect(i * size, j * size,
size, size, true);
}
}
}
public Dimension getPreferredSize() {
return new Dimension(w * size, h * size);
}
我的目标是使垂直位置每秒增加 1(因此它以秒为间隔向下移动窗口。
我不认为计时器功能有问题。当我打印verticalPos时,它每秒打印出递增的值,但是它只是在屏幕上显示新位置 - 这就是问题所在。
现在的窗口图像。
[img] /image/sHQVA.png
最佳答案
首先在 Tetris_Block
的 actionPerformed
方法中添加对 repaint
的调用
@Override
public void actionPerformed(ActionEvent e) {
nextMove();
square_Block();
System.out.println(verticalPos);
// This is important
repaint();
}
这将在事件队列上安排一个绘制事件,最终将调用您的 paintComponent
方法(间接)
这将使方 block 开始移动。您将遇到的下一个问题是您实际上并没有从之前的位置“移除” block ,因此它会继续在屏幕上流血/生长
您可以通过将 block 的颜色传递给 square_Block
来解决这个问题,例如......
public void square_Block(int color) { //Horizontal || Vertical || Colour
//Horizontal never changes for this as I just want the blocks to go down.
a[0][verticalPos] = color;
a[0][verticalPos1] = color;
a[1][verticalPos] = color;
a[1][verticalPos1] = color;
}
然后“休息”当前位置的 block ,更新位置然后设置新的 block 颜色;
@Override
public void actionPerformed(ActionEvent e) {
square_Block(0);
nextMove();
square_Block(3);
System.out.println(verticalPos);
repaint();
}
关于java - 如何让俄罗斯方 block 每秒下落(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33926848/
我想要一个按钮,单击该按钮会在指定时间后刷新当前页面。 我目前有: setTimeout(function reload(){ location = '' },1000) Relo
我有一个场景,我需要每秒向服务器发送数据(非常少的字节)。这个细节很重要,所以我也需要对其进行加密。 深入研究后,我认为我们可以使用 HTTP 协议(protocol),但在这种情况下,HTTP he
我在即将进行的测试的学习指南中得到了这个问题。我不明白如何解决这个问题。今天是我的考试,我希望能得到一些帮助。 如果 CPU 每条指令发出一个内存请求并且计算机以 200 MIPS 运行,大约需要多少
我正在编写一个程序来确定每秒可以运行多少个 NOP,但我得到的数字似乎非常小。 int main() { struct timeval tvStart, tvDiff, tvEnd;
我想实现每秒 5-1 百万次远程函数调用。假设我们有一台开始计算的 Central 计算机,还有一台执行计算的 Worker 计算机。实际配置中会有很多Worker计算机。 假设我们的任务是计算一个[
下面的代码向最后一个 div 添加了一个类: $(".mydivs:last").addClass('added'); 这适用于页面加载,但 div 在动画中,因此顺序会改变。有没有办法让代码每秒运行
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwa
我刚刚在 HBase 中创建了一个表并用数据填充了它。从 7 个区域服务器看来,数据已写入区域服务器 6 和 7。 但我不明白为什么服务器 6 和 7 的每秒请求数为零? 最佳答案 读取请求计数 和写
我正在开发一个聊天应用程序,需要您的帮助。如果有人在线,我将信息存储在数据库中。现在我有一个功能,可以显示您的好友列表以及聊天伙伴是否在线。问题是:如果有人上网,我想经常检查。到目前为止,我有一个每秒
我正在编写一个快速而肮脏的脚本来检测服务器停机时间。我有一台服务器和两个远程客户端,它们每秒对服务器执行一次 ping 操作以查看它是否已启动。 如果您最多可以每分钟运行一次 cron,我该如何设置任
有什么方法可以让 Graphite 绘制图表请求吗? 当你从 nginx_status 检索 nginx 请求时,你正在向 Graphite 发送一个绝对值,所以我在想是否有任何方法可以获取每秒的速率
我正在构建一个 Android 应用程序,它将加速度计和陀螺仪数据记录到文本文件中。在大多数教程中,他们使用的方法涉及创建两个文本文件,并每秒打开和关闭它们 50 次。即: private stati
嘿,我目前在我的 mysql 上有超过 300+ qps。在相当繁重的 PHP 网站上大约有 12000 UIP 一天/没有 cron。我知道在没有看到该网站的情况下很难判断是否可以,但您是否认为这完
我希望每秒钟使用我的鼠标移动功能旋转矩形我做错了什么?我怎样才能做我的变换,以便高度和旋转每隔一个元素改变一次?我用 if ( i % 2 == 0){} 试了一下 function modifyRe
有没有办法配置具有恒定负载的性能测试用例(例如,每秒 3 个新请求,持续 1 分钟)? 其他负载测试库具有此功能来设置请求率(例如 Artillery.io、Vegeta)。 k6 有设置VUs 的方
(只是想提一下,这是我的第一个问题,如果我做错了什么,我深表歉意)。我正在制作一个解析 CSV 文件并将其保存为列表的 Python 程序。但是,该程序需要用户输入他们希望将数据发送到服务器的速度。我
如何使用 python 将毫秒转换为帧数?我知道视频的帧速率(每帧 25 秒) 2683480 2684448 最佳答案 我最终决定手动执行此操作,因为我仍然没有弄清楚 ffmpeg 函数等 25fp
我有一个动态表,它按从主要编号到次要编号的降序排列。我想用 jQuery 在前 2 行放置红色背景,在接下来的行放置橙色背景,在接下来的 2 行放置黄色背景,在接下来的 3 行放置绿色背景。 表结构:
构建 Rails 应用程序 (ruby 2.4.0p0/Rails 5.1.4) 并使用我的 Macbook air (MacOS High Sierra 10.13.2) 在本地进行测试,但我一直遇
这个问题已经有答案了: NSTimer not firing when runloop is blocked (2 个回答) 已关闭 9 年前。 我使用 NSTimer,它每秒触发一次并更新一个标签,
我是一名优秀的程序员,十分优秀!