java - Properly Initializing Graphics (or maybe some other thing, I am not sure) in Java
问题描述
I am back with a new question about the same code as last time. I took a lot of the suggestions that were given, and changed a few things. The program compiles, but no rectangle appears with any input. People in the last thread said that the code would work with the issues fixed, but this doesn't seem to work. I don't know if it a problem with the graphics (I think someone else suggested to do what I did in the code I have now) or with the way I am accepting key inputs.
Anyways, the program below is supposed to have a rectangle drawn, and for that rectangle be moved one coordinate to the right or left depending on if you press the <- or -> key on your keyboard. However, no rectangle appears with any input.
import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;
public class game extends Applet
{
Thread loopThread;
boolean left = false;
boolean right = false;
int platPos = 50;
public void run()
{
Graphics g = null;
int i, j;
long startTime;
if (loopThread == null)
{
loopThread = new Thread();
loopThread.start();
}
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
startTime = System.currentTimeMillis();
while(Thread.currentThread() == loopThread)
{
updatePlatter(g);
}
}
public void updatePlatter(Graphics g)
{
if(left)
{
g.setColor(new Color(255,255,255)); //sets color to white
g.fillRect(50+platPos, 200, 100, 20); //draws over whatever existing rectangle there is with white
platPos--;
g.setColor(new Color(100,100,100)); //sets new color
g.fillRect(50+platPos,200, 100,20); //draws new rectangle
left = false;
}
if(right)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos,200,100,20);
platPos++;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200,100,20);
right = false;
}
}
public boolean keyDown(Event e, int key)
{
if (key == Event.LEFT)
left = true;
if (key == Event.RIGHT)
right = true;
return true;
}
}
Also I want to use applet, not JFrame. Just a personal preference. Thanks for any help! ^.^
解决方案
首先将图形提供给您的小程序。它是一个抽象类,当您使用小程序时,Java 会为您提供正确的实现。你不能只是实例化它。有一些方法可以处理/重新绘制小程序。所以我建议使用paint方法对矩形进行初始绘制,然后在循环中调用repaint。顺便说一句,使用线程进行这样的循环是不可读的代码(而且它不起作用)。您甚至不需要 run() 方法;) 当您创建小程序时,它开始工作。
所以我们可以这样做:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
public class Game extends Applet
{
boolean left = false;
boolean right = false;
int platPos = 0;
@Override
public void paint(Graphics g)
{
g.fillRect(50+platPos, 0, 100, 20);
System.out.println("repaint");
if(left)
{
g.setColor(new Color(255,255,255)); //sets color to white
g.fillRect(50+platPos, 0, 100, 20); //draws over whatever existing rectangle there is with white
platPos--;
g.setColor(new Color(100,100,100)); //sets new color
g.fillRect(50+platPos,0, 100,20); //draws new rectangle
left = false;
}
if(right)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos,200,100,20);
platPos++;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200,100,20);
right = false;
}
}
@Override
public boolean keyDown(Event e, int key)
{
if (key == Event.LEFT) {
left = true;
}
if (key == Event.RIGHT) {
right = true;
}
repaint();
return true;
}
}
推荐阅读
- python - Python如何根据元组列表中的值对字典列表进行排序
- flutter - 从 GitHub 中删除颤振 repo
- java - 在 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java/bin/java 的 JAVA_HOME 中找不到 java
- ios - 在 Chrome 移动设备和 iOS Safari 移动设备中呈现 SVG 时出现问题
- java - 关于与数组索引超出范围有关的错误
- javascript - 点击按钮打开评论回复表
- c++ - 类中未调用虚函数
- php - Netbeans IDE (12.2):PHP 8.0 构造函数提升的语法错误
- python - 多输出神经网络模型
- c++ - 链表包含一个类抛出异常 0xC0000005