- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
基本上当屏幕刷新时,它会闪烁白色,然后一秒钟后,它会再次闪烁。我在 mac 上工作,在 eclipse 上屏幕不闪烁,但一旦我将它放入小程序中,它就开始闪烁。
import java.awt.event.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.net.URL;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*; //Copyright MRE Dev
//Anyone that steals this code will be prostituted
public class RadicalRacingApplet extends Applet implements KeyListener {
final int WIDTH = 900, HEIGHT = 650;
double p1Speed = 0;
double p2Speed = 0;
double bullet1Speed = 7;
double bullet2Speed = 7;
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
int p1Direction = 1;
int p2Direction = 1;
int counterp1 = 0;
int counterp2 = 0;
int scoreN = 0;
int bullets = 0;
int p1Deaths = 0;
int p2Deaths = 0;
final int shoot = 1;
final int shootOther = 3;
final int shoot2 = 1;
final int shoot2Other = 3;
final int normal = 1;
final int big = 3;
final int normal2 = 1;
final int big2 = 3;
int bullets2 = 0;
int bigBullets = 0;
int bigBullets2 = 0;
String scoreS = " ";
URL eng = this.getClass().getResource ("hit.wav");
AudioClip snd = JApplet.newAudioClip(eng);
Rectangle p1 = new Rectangle(135,220,30,30);
Rectangle p2 = new Rectangle(700,300,30,30);
//Obstacles
Rectangle ob1 = new Rectangle(475,75,50,600);
Rectangle bullets1 = new Rectangle(0,0,10,5);
Rectangle bullets2Rect = new Rectangle(0,0,10,5);
Rectangle powerUp1 = new Rectangle(475,680,50,50);
Rectangle bigBullet = new Rectangle(200,600,50,50);
Rectangle bigBullet2 = new Rectangle(800,600,50,50);
public void init() {
Move1 m1 = new Move1();
m1.start();
Bullets1 b1 = new Bullets1();
b1.start();
Move2 m2 = new Move2();
m2.start();
Bullets2 b2 = new Bullets2();
b2.start();
PowerUps p1 = new PowerUps();
p1.start();
BigBullet1 bg1 = new BigBullet1();
bg1.start();
BigBullet2 bg2 = new BigBullet2();
bg2.start();
snd.loop();
}
public void paint(Graphics g) {
super.paint(g);
super.repaint();
g.setColor(Color.blue);
g.fillRect(0,0,1000,750);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial",Font.PLAIN,16));
g.setColor(Color.BLACK);
g.fillRect(ob1.x, ob1.y, ob1.width, ob1.height);
g.setColor(Color.WHITE);
g.drawString(scoreS, 100, 100);
g.fill3DRect(powerUp1.x, powerUp1.y, powerUp1.width, powerUp1.height, true);
g.fill3DRect(bigBullet.x, bigBullet.y, bigBullet.width, bigBullet.height,true);
g.fill3DRect(bigBullet2.x, bigBullet2.y, bigBullet2.width, bigBullet2.height, true);
g.setColor(Color.YELLOW);
g.setFont(new Font("Arial",Font.BOLD,30));
g.drawString("P1 Deaths " + p1Deaths, 200, 50);
g.drawString("P2 Deaths" + p2Deaths, 400, 50);
Image img1;
Image img2;
Image img3;
Image img4;
Image img5;
Image img6;
Image img7;
Image img8;
try{
URL url = this.getClass().getResource("p1.png");
URL url2 = this.getClass().getResource("p1Right.png");
URL url3 = this.getClass().getResource("p1Left.png");
URL url4 = this.getClass().getResource("p1Down.png");
URL url5 = this.getClass().getResource("bullet.png");
URL url6 = this.getClass().getResource("bullet2.png");
URL url7 = this.getClass().getResource("bigBullet.png");
URL url8 = this.getClass().getResource("bigBullet2.png");
img1 = Toolkit.getDefaultToolkit().getImage(url);
img2 = Toolkit.getDefaultToolkit().getImage(url2);
img3 = Toolkit.getDefaultToolkit().getImage(url3);
img4 = Toolkit.getDefaultToolkit().getImage(url4);
img5 = Toolkit.getDefaultToolkit().getImage(url5);
img6 = Toolkit.getDefaultToolkit().getImage(url6);
img7 = Toolkit.getDefaultToolkit().getImage(url7);
img8 = Toolkit.getDefaultToolkit().getImage(url8);
if(p1Direction == UP) {
g.drawImage(img1, p1.x, p1.y, this);
}
if(p1Direction == RIGHT) {
g.drawImage(img2, p1.x, p1.y, this);
}
if(p1Direction == LEFT) {
g.drawImage(img3, p1.x, p1.y, this);
}
if(p1Direction == DOWN) {
g.drawImage(img4, p1.x, p1.y, this);
}
if(p2Direction == UP) {
g.drawImage(img1, p2.x, p2.y, this);
}
if(p2Direction == RIGHT) {
g.drawImage(img2, p2.x, p2.y, this);
}
if(p2Direction == LEFT) {
g.drawImage(img3, p2.x, p2.y, this);
}
if(p2Direction == DOWN) {
g.drawImage(img4, p2.x, p2.y, this);
}
if(bullets == shoot) {
g.drawImage(img5, bullets1.x, bullets1.y, this);
}
if(bullets == shootOther) {
g.drawImage(img6, bullets1.x, bullets1.y, this);
}
if(bullets2 == shoot2) {
g.drawImage(img6, bullets2Rect.x, bullets2Rect.y, this);
}
if(bullets2 == shoot2Other) {
g.drawImage(img5, bullets2Rect.x, bullets2Rect.y, this);
}
if((bigBullets == big) && (bullets == shoot)) {
g.drawImage(img7,bullets1.x, bullets1.y, this);
}
if((bigBullets2 == big2) && (bullets2 == shoot2Other)) {
g.drawImage(img7, bullets2Rect.x, bullets2Rect.y, this);
}
if((bigBullets2 == big2) && (bullets2 == shoot2)) {
g.drawImage(img8, bullets2Rect.x, bullets2Rect.y, this);
}
if((bigBullets == big) && (bullets == shootOther)) {
g.drawImage(img8, bullets1.x, bullets1.y, this);
}
}
catch(Exception e) {
System.out.println("The images dont work");
}
}
public class Move1 extends Thread implements KeyListener {
public void run() {
addKeyListener(this);
while(true) {
try {
if(p1.intersects(ob1)) {
p1.x = 135;
p1.y = 220;
p1Deaths++;
}
if(p1.intersects(p2)) {
p1.x = 135;
p1.y = 220;
p1Deaths++;
}
if(p1.x >1000) {
p1.x = 0;
}
if(p1.y > 750) {
p1.y = 0;
}
if(p1.x <0) {
p1.x = 1000;
}
if(p1.y < 0) {
p1.y = 750;
}
if(p1Direction == UP) {
p1.y-=(int)p1Speed;
if(p1Speed <=3) {
p1Speed+=.2;
}
}
if(p1Direction == DOWN) {
p1.y+=(int)p1Speed;
if(p1Speed <=3) {
p1Speed+=.2;
}
}
if(p1Direction == LEFT) {
p1.x-=(int)p1Speed;
if(p1Speed <=3) {
p1Speed+=.2;
}
}
if(p1Direction == RIGHT) {
p1.x+=(int)p1Speed;
if(p1Speed <=3) {
p1Speed+=.2;
}
}
Thread.sleep(35);
}
catch(Exception e) {
break;
}
}
}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
if(event.getKeyChar() == 'a') {
p1Direction = LEFT;
}
if(event.getKeyChar() == 's') {
p1Direction = DOWN;
}
if(event.getKeyChar() == 'd') {
p1Direction = RIGHT;
}
if(event.getKeyChar() == 'w') {
p1Direction = UP;
}
}
}
public class Move2 extends Thread implements KeyListener {
public void run() {
addKeyListener(this);
while(true) {
try {
if(p2.intersects(ob1)) {
p2.x = 700;
p2.y = 300;
p2Deaths++;
}
if(p2.intersects(p1)) {
p2.x = 700;
p2.y = 300;
p2Deaths++;
}
if(p2.x >1000) {
p2.x = 0;
}
if(p2.y > 750) {
p2.y = 0;
}
if(p2.x <0) {
p2.x = 1000;
}
if(p2.y < 0) {
p2.y = 750;
}
if(p2Direction == UP) {
p2.y-=(int)p2Speed;
if(p2Speed <=3) {
p2Speed+=.2;
}
}
if(p2Direction == DOWN) {
p2.y+=(int)p2Speed;
if(p2Speed <=3) {
p2Speed+=.2;
}
}
if(p2Direction == LEFT) {
p2.x-=(int)p2Speed;
if(p2Speed <=3) {
p2Speed+=.2;
}
}
if(p2Direction == RIGHT) {
p2.x+=(int)p2Speed;
if(p2Speed <=3) {
p2Speed+=.2;
}
}
Thread.sleep(35);
}
catch(Exception e) {
break;
}
}
}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
if(event.getKeyChar() == 'j') {
p2Direction = LEFT;
}
if(event.getKeyChar() == 'k') {
p2Direction = DOWN;
}
if(event.getKeyChar() == 'l') {
p2Direction = RIGHT;
}
if(event.getKeyChar() == 'i') {
p2Direction = UP;
}
}
}
public class Bullets1 extends Thread implements KeyListener{
public void run() {
addKeyListener(this);
while(true) {
try {
if(bullets1.intersects(p2)) {
p2.x = 700;
p2.y = 300;
p2Deaths++;
}
if(bullets1.x > 1000) {
bullet1Speed = 7;
bullets = 0;
bullets1.x = 0;
bullets1.y = 0;
}
if(bullets1.x < 0) {
bullet1Speed = 7;
bullets = 0;
bullets1.x = 0;
bullets1.y = 0;
}
double p1X = p1.getX();
double p1Y = p1.getY();
if(bullets == shoot) {
bullets1.x = (int)p1X;
bullets1.y = (int)p1Y;
if(bullet1Speed >= 5) {
bullet1Speed+=5;
}
bullets1.x+=(int)bullet1Speed;
}
if(bullets == shootOther) {
bullets1.x = (int)p1X;
bullets1.y = (int)p1Y;
if(bullet1Speed >=5) {
bullet1Speed+=5;
}
bullets1.x-=(int)bullet1Speed;
}
Thread.sleep(20);
}
catch(Exception e) {
break;
}
}
}
public void keyPressed(KeyEvent event) {
if(event.getKeyChar() == 'e') {
bullets = shoot;
}
if(event.getKeyChar() == 'q') {
bullets = shootOther;
}
}
@Override
public void keyReleased(KeyEvent event) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent event) {
// TODO Auto-generated method stub
}
}
public class Bullets2 extends Thread implements KeyListener{
public void run() {
addKeyListener(this);
while(true) {
try {
if(bullets2Rect.intersects(p1)) {
p1.x = 135;
p1.y = 300;
p1Deaths++;
}
if(bullets2Rect.x < 0) {
bullet2Speed = 7;
bullets2 = 0;
bullets2Rect.x = 0;
bullets2Rect.y = 0;
}
if(bullets2Rect.x > 1000) {
bullet2Speed = 7;
bullets2 = 0;
bullets2Rect.x = 0;
bullets2Rect.y = 0;
}
double p2X = p2.getX();
double p2Y = p2.getY();
if(bullets2 == shoot2) {
bullets2Rect.x = (int)p2X;
bullets2Rect.y = (int)p2Y;
if(bullet2Speed >= 5) {
bullet2Speed+=5;
}
bullets2Rect.x-=(int)bullet2Speed;
}
if(bullets2 == shoot2Other) {
bullets2Rect.x = (int)p2X;
bullets2Rect.y = (int)p2Y;
if(bullet2Speed >=5) {
bullet2Speed+=5;
}
bullets2Rect.x+=(int)bullet2Speed;
}
Thread.sleep(20);
}
catch(Exception e) {
break;
}
}
}
public void keyPressed(KeyEvent event) {
if(event.getKeyChar() == 'u') {
bullets2 = shoot2;
}
if(event.getKeyChar() == 'o') {
bullets2 = shoot2Other;
}
}
@Override
public void keyReleased(KeyEvent event) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent event) {
// TODO Auto-generated method stub
}
}
public class PowerUps extends Thread implements KeyListener {
public void run() {
addKeyListener(this);
while(true) {
try {
int countPower = 0;
int countPower2 = 0;
if(p1.intersects(powerUp1)) {
powerUp1.setSize(0,0);
countPower = 1;
}
if(countPower == 1) {
p1Speed = 7;
Thread.sleep(10000);
}
if(countPower == 0) {
p1Speed = 3;
}
if(p2.intersects(powerUp1)) {
powerUp1.setSize(0,0);
countPower2 = 1;
}
if(countPower2 == 1) {
p2Speed = 7;
Thread.sleep(10000);
}
if(countPower == 0) {
p2Speed = 3;
}
Thread.sleep(35);
}
catch (Exception e) {
break;
}
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
public class BigBullet1 extends Thread {
public void run() {
while(true) {
try {
if(p1.intersects(bigBullet)) {
bigBullet.setSize(0,0);
bigBullets = big;
Thread.sleep(10000);
bigBullets = normal;
}
if(p2.intersects(bigBullet)) {
bigBullet.setSize(0,0);
bigBullets2 = big;
Thread.sleep(10000);
bigBullets2 = normal2;
}
}
catch(Exception e) {
break;
}
}
}
}
public class BigBullet2 extends Thread {
public void run() {
while(true) {
try {
if(p2.intersects(bigBullet2)) {
bigBullet2.setSize(0,0);
bigBullets2 = big2;
Thread.sleep(10000);
bigBullets2 = normal2;
}
if(p1.intersects(bigBullet2)) {
bigBullet2.setSize(0,0);
bigBullets = big2;
bullets1.setSize(10,10);
Thread.sleep(10000);
bigBullets = normal2;
}
}
catch(Exception e) {
break;
}
}
}
}
@Override
public void keyPressed(KeyEvent event) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent event) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent event) {
// TODO Auto-generated method stub
}
最佳答案
首先,不要使用Applet
,使用JApplet
其次,不要覆盖顶级容器的paint
方法,使用类似JPanel
或JComponent
的方法,它们提供双缓冲默认情况下。
第三,不要在任何paint
方法中调用repaint
(事实上,不要调用任何可能发出repaint
请求的东西...)
第四,不要在paint
方法中加载图像??看这个Problems when drawing images in java applets为什么……
已更新
基本上...
public class GamePane extends JPanel implements KeyListener {
final int WIDTH = 900, HEIGHT = 650;
double p1Speed = 0;
double p2Speed = 0;
double bullet1Speed = 7;
double bullet2Speed = 7;
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
int p1Direction = 1;
int p2Direction = 1;
int counterp1 = 0;
int counterp2 = 0;
int scoreN = 0;
int bullets = 0;
int p1Deaths = 0;
int p2Deaths = 0;
final int shoot = 1;
final int shootOther = 3;
final int shoot2 = 1;
final int shoot2Other = 3;
final int normal = 1;
final int big = 3;
final int normal2 = 1;
final int big2 = 3;
int bullets2 = 0;
int bigBullets = 0;
int bigBullets2 = 0;
String scoreS = " ";
URL eng = this.getClass().getResource("hit.wav");
AudioClip snd = JApplet.newAudioClip(eng);
Rectangle p1 = new Rectangle(135, 220, 30, 30);
Rectangle p2 = new Rectangle(700, 300, 30, 30);
//Obstacles
Rectangle ob1 = new Rectangle(475, 75, 50, 600);
Rectangle bullets1 = new Rectangle(0, 0, 10, 5);
Rectangle bullets2Rect = new Rectangle(0, 0, 10, 5);
Rectangle powerUp1 = new Rectangle(475, 680, 50, 50);
Rectangle bigBullet = new Rectangle(200, 600, 50, 50);
Rectangle bigBullet2 = new Rectangle(800, 600, 50, 50);
public BadApplet2() {
Move1 m1 = new Move1();
m1.start();
Bullets1 b1 = new Bullets1();
b1.start();
Move2 m2 = new Move2();
m2.start();
Bullets2 b2 = new Bullets2();
b2.start();
PowerUps p1 = new PowerUps();
p1.start();
BigBullet1 bg1 = new BigBullet1();
bg1.start();
BigBullet2 bg2 = new BigBullet2();
bg2.start();
snd.loop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// This bad, don't do it
//super.repaint();
g.setColor(Color.blue);
g.fillRect(0, 0, 1000, 750);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 16));
g.setColor(Color.BLACK);
g.fillRect(ob1.x, ob1.y, ob1.width, ob1.height);
g.setColor(Color.WHITE);
g.drawString(scoreS, 100, 100);
g.fill3DRect(powerUp1.x, powerUp1.y, powerUp1.width, powerUp1.height, true);
g.fill3DRect(bigBullet.x, bigBullet.y, bigBullet.width, bigBullet.height, true);
g.fill3DRect(bigBullet2.x, bigBullet2.y, bigBullet2.width, bigBullet2.height, true);
g.setColor(Color.YELLOW);
g.setFont(new Font("Arial", Font.BOLD, 30));
g.drawString("P1 Deaths " + p1Deaths, 200, 50);
g.drawString("P2 Deaths" + p2Deaths, 400, 50);
// This is bad, don't do it...
// Move these to the field level of the class
// and load them in the constructor
/*
Image img1;
Image img2;
Image img3;
Image img4;
Image img5;
Image img6;
Image img7;
Image img8;
*/
//try {
// This is bad don't do it...
/*
URL url = this.getClass().getResource("p1.png");
URL url2 = this.getClass().getResource("p1Right.png");
URL url3 = this.getClass().getResource("p1Left.png");
URL url4 = this.getClass().getResource("p1Down.png");
URL url5 = this.getClass().getResource("bullet.png");
URL url6 = this.getClass().getResource("bullet2.png");
URL url7 = this.getClass().getResource("bigBullet.png");
URL url8 = this.getClass().getResource("bigBullet2.png");
img1 = Toolkit.getDefaultToolkit().getImage(url);
img2 = Toolkit.getDefaultToolkit().getImage(url2);
img3 = Toolkit.getDefaultToolkit().getImage(url3);
img4 = Toolkit.getDefaultToolkit().getImage(url4);
img5 = Toolkit.getDefaultToolkit().getImage(url5);
img6 = Toolkit.getDefaultToolkit().getImage(url6);
img7 = Toolkit.getDefaultToolkit().getImage(url7);
img8 = Toolkit.getDefaultToolkit().getImage(url8);
*/
if (p1Direction == UP) {
g.drawImage(img1, p1.x, p1.y, this);
}
if (p1Direction == RIGHT) {
g.drawImage(img2, p1.x, p1.y, this);
}
if (p1Direction == LEFT) {
g.drawImage(img3, p1.x, p1.y, this);
}
if (p1Direction == DOWN) {
g.drawImage(img4, p1.x, p1.y, this);
}
if (p2Direction == UP) {
g.drawImage(img1, p2.x, p2.y, this);
}
if (p2Direction == RIGHT) {
g.drawImage(img2, p2.x, p2.y, this);
}
if (p2Direction == LEFT) {
g.drawImage(img3, p2.x, p2.y, this);
}
if (p2Direction == DOWN) {
g.drawImage(img4, p2.x, p2.y, this);
}
if (bullets == shoot) {
g.drawImage(img5, bullets1.x, bullets1.y, this);
}
if (bullets == shootOther) {
g.drawImage(img6, bullets1.x, bullets1.y, this);
}
if (bullets2 == shoot2) {
g.drawImage(img6, bullets2Rect.x, bullets2Rect.y, this);
}
if (bullets2 == shoot2Other) {
g.drawImage(img5, bullets2Rect.x, bullets2Rect.y, this);
}
if ((bigBullets == big) && (bullets == shoot)) {
g.drawImage(img7, bullets1.x, bullets1.y, this);
}
if ((bigBullets2 == big2) && (bullets2 == shoot2Other)) {
g.drawImage(img7, bullets2Rect.x, bullets2Rect.y, this);
}
if ((bigBullets2 == big2) && (bullets2 == shoot2)) {
g.drawImage(img8, bullets2Rect.x, bullets2Rect.y, this);
}
if ((bigBullets == big) && (bullets == shootOther)) {
g.drawImage(img8, bullets1.x, bullets1.y, this);
}
//} catch (Exception e) {
// System.out.println("The images dont work");
//}
}
// Add the rest of the code
}
我会做更多关于游戏线程的阅读。通常,应该有一个负责更新游戏中各种 Assets 状态的“游戏循环”,而不是几十个线程。
获取此面板并将其添加到您的JApplet
。如果您不知道该怎么做,那么您将阅读 Creating a GUI with Swing 的内容。
更新
至于您的 java.lang.ClassCastException
,我想您正试图将面板作为小程序加载,这显然行不通。
public class RadicalRacingApplet extends JApplet {
public void init() {
setLayout(new BorderLayout());
add(new GamePanel());
}
}
应该让你绕过它......
关于java - 如何阻止 repaint() 方法闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12540315/
我每秒收到数百个事件,持续几秒钟,每个事件都会更新我的模型。如果我在每个事件之后在 invokeLater() 内部调用 repaint() ,那么 repaint 每秒会被调用数百次吗?它是否足够聪
我在类collection中有一个循环,它repaint() JPanel连续 while(loop) { imageicon = (ImageIcon) ois.readOb
我仍在尝试让一个 repaint() 方法在一个单独的类中工作,该类具有一个扩展 JComponent 的类。我已经在这里发布了几篇文章,但到目前为止我还无法让代码正常工作。我得到了一些很好的建议。我
谁能解释一下JPanel.repaint() 方法和JFrame.repaint() 方法之间的区别,我想两者都调用了paintComponent() JPanel 中的方法。 请说明,谢谢 最佳答案
在repaint(long maxDelay)(来自java.awt.Component)中,maxDelay指定之前可以经过的最大毫秒数调用update。 普通的repaint()有这样的最大值吗?
这个问题是基于我不久前遇到的一个简单的 Swing 骰子程序问题。我发布的原始问题是 here并且有一个可接受的答案,但我想确切地知道发生了什么,为什么会出现问题,以及解决方案为什么有效。 我设法削减
我正在为错误程序编写一个插件,并且在使用 repaint() 方法时遇到问题。 简短的问题:有什么方法可以在 JPanel 重绘完成后立即获得通知或与其同步代码吗? 详细版本: 我的程序可以将 xy
我在java中使用2个类class1和class2,它们都位于不同的包中。 class2 中定义了一个 paintComponent() 和一个 test()。我按以下顺序从 class1 调用这些方
Full project here https://github.com/jafetrd/easyImageEditor 我正在尝试使用paintComponent()将颜色应用于图像,这是我使用的类
我正在尝试制作一个 JFrame 程序,将二进制-十进制-十六进制转换器与按钮连接起来。其中一部分,我想绘制圆圈来表示一排圆圈中的二进制数,其中实心圆圈代表一,空心圆圈代表零。但是,当我尝试调用 re
我两周前才开始学习java,所以我对java还不太了解。我正在尝试让一个球在框架内弹跳或移动。但当我在线程中运行它时,它不会重新绘制/更新,但如果我使用 while 循环或计时器,它就可以正常工作,我
我正在尝试制作一款三消游戏。我试图为实际发生的情况创建一些视觉辅助,首先将需要删除的 gem 标记为“黑色”,然后让重力完成它的工作。我正在努力做到这一点,在将它们标记为“黑色”后,我调用了 repa
我正在编写一个 Java 程序,它将根据按下的按钮绘制圆形或矩形。虽然它确实绘制了给定的形状,但在绘制时它会在窗口的左上角(最有可能是(0,0))创建新按钮。我是否违反了paint()/repaint
我正在为学校项目开发一款游戏,一款类似炸弹人的游戏。 我正在使用 swing,并且使用 Canvas 来绘制图形,但 KeyListener 无法正常工作,因此我退出使用 Canvas 并开始使用 p
嘿,我正在尝试学习如何在java中使用图形,并且正在编写一个非常简单的代码,该代码获取图像并将其在窗口中成一条线移动,当它到达窗口边缘时,它会向下移动几个像素并从另一端开始。 我的问题是我的程序在重新
我试图了解 repaint 和 paintComponents 在 Java Swing 中如何工作,并想知道为什么这个程序在执行时只显示“hello”。 class MyLabel extends
我无法让我的重绘方法在我的 SimonPanel 类中工作。起初,我认为这是因为我使用了paint()而不是paintComponent(),但这似乎并没有解决问题。我的 SimonShape.jav
这就是我的任务。 我必须生成 4 张随机卡。之后,当按下“刷新”按钮时,卡片应再次随机化。我已经像这样实现了 repaint() 方法,但它不会改变卡片的显示方式。 public class Four
我看了很多答案,但仍然找不到解决方案。我有一个 JFrame 和两个 JPanel。我想在按下按钮时删除第一个面板并将其替换为第二个面板,但 repaint() 方法不会刷新框架。请帮忙。 这是我的框
我在这里尝试做的是从我的 loadbg() 方法中调用 repaint() 方法。但是,repaint() 不起作用,并且我的图像(存储在 var bg 中)不会加载到屏幕上。关于为什么这行不通的任何
我是一名优秀的程序员,十分优秀!