gpt4 book ai didi

如果鼠标静止,Java Canvas 的 fps 较低

转载 作者:行者123 更新时间:2023-12-02 09:54:10 38 4
gpt4 key购买 nike

如果我没有以某种方式更新屏幕,对 canvas.repaint() 的几次调用似乎会被完全跳过。移动鼠标时,一切都很好。

我的代码如下:

package yeet.gfxTut;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JFrame;

public class Toot extends Canvas {
private static final long serialVersionUID = 1L;
public static int xPos, yPos, yV, xV;

public static void main(String[] args) throws InterruptedException {
Random rand = new Random();
JFrame frame = new JFrame("My Drawing");
Canvas canvas = new Toot();
canvas.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addMouseListener(new Toot().new TootMListener());
xPos = rand.nextInt(400);
yPos = rand.nextInt(400);
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
while (true) {
Thread.sleep(1000 / 60);
canvas.repaint();
}
}

public void paint(Graphics g) {
Random rand = new Random();
if (xPos < 0) {
xV += rand.nextInt(2) - 1;
xV = 0 - xV;
}
if (yPos < 0) {
yV += rand.nextInt(2) - 1;
yV = 0 - yV;
}
if (xPos > 400) {
xV += rand.nextInt(2) - 1;
xV = 0 - xV;
}
if (yPos > 400) {
yV += rand.nextInt(2) - 1;
yV = 0 - yV;
}
xPos += xV;
yPos += yV;
g.fillOval(xPos, yPos, 6, 6);
}

private class TootMListener implements MouseListener {
Random rand = new Random();

@Override
public void mouseClicked(MouseEvent e) {
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

}
}

我在多个不同的 canvas 项目中遇到了这个问题,有什么帮助吗?

更新:我尝试使用建议的答案,但导致了同样的问题。新代码如下:

package yeet.gfxTut;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Toot extends Canvas implements ActionListener {
private static final long serialVersionUID = 1L;
public static int xPos, yPos, yV, xV;

Timer timer = new Timer(1000/60, this);
public Toot() {
super();
timer.start();
}
public static void main(String[] args) throws InterruptedException {
// timer.start();
Random rand = new Random();
JFrame frame = new JFrame("My Drawing");
Canvas canvas = new Toot();
canvas.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addMouseListener(new Toot().new TootMListener());
xPos = rand.nextInt(400);
yPos = rand.nextInt(400);
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
while (true) {
Thread.sleep(1000 / 60);
canvas.repaint();
}
}

public void paint(Graphics g) {
Random rand = new Random();
if (xPos < 0) {
xV += rand.nextInt(2) - 1;
xV = 0 - xV;
}
if (yPos < 0) {
yV += rand.nextInt(2) - 1;
yV = 0 - yV;
}
if (xPos > 400) {
xV += rand.nextInt(2) - 1;
xV = 0 - xV;
}
if (yPos > 400) {
yV += rand.nextInt(2) - 1;
yV = 0 - yV;
}
xPos += xV;
yPos += yV;
g.fillOval(xPos, yPos, 6, 6);
}

private class TootMListener implements MouseListener {
Random rand = new Random();

@Override
public void mouseClicked(MouseEvent e) {
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

}

@Override
public void actionPerformed(ActionEvent e) {
// if(e.getSource() == timer){
repaint();
// }
}
}

最佳答案

引用第一段代码,而不是

while (true) {
Thread.sleep(1000 / 60);
canvas.repaint();
}

使用 Swing 包中的Timer:

new javax.swing.Timer(1000/60,event->canvas.repaint()).start();

我已经上过你的类(class),并且进展顺利:

public class Toot extends Canvas {
private static final long serialVersionUID = 1L;
public static int xPos, yPos, yV, xV;

public static void main(String[] args) throws InterruptedException {
Random rand = new Random();
JFrame frame = new JFrame("My Drawing");
Canvas canvas = new Toot();
canvas.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
}
});
xPos = rand.nextInt(400);
yPos = rand.nextInt(400);
xV = rand.nextInt(5) + 1;
yV = rand.nextInt(5) + 1;
new javax.swing.Timer(1000/60,e->canvas.repaint()).start();
}

@Override
public void paint(Graphics g) {
Random rand = new Random();
if (xPos < 0) {
xV += rand.nextInt(2) - 1;
xV = Math.abs(xV);
}
if (yPos < 0) {
yV += rand.nextInt(2) - 1;
yV = Math.abs(yV);
}
if (xPos > 400) {
xV += rand.nextInt(2) - 1;
xV = - Math.abs(xV);
}
if (yPos > 400) {
yV += rand.nextInt(2) - 1;
yV = - Math.abs(yV);
}
xPos +=xV;
yPos +=yV;
g.fillOval(xPos, yPos, 6, 6);
}
}

关于如果鼠标静止,Java Canvas 的 fps 较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56116673/

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