为什么paint(Graphics g)没有绘制图像? [英] Why is paint(Graphics g) not drawing image?
问题描述
以下是我将图像绘制到屏幕的代码:
Here is my code to draw image to screen:
public class Main extends Applet implements Runnable, KeyListener{
private URL base;
private Image image, image1,image2, image3, background;
private Graphics second;
String imageString;
@Override
public void init() {
// Setting Size of screen
setSize(800, 600);
setBackground(Color.BLACK);
// Set game focus
setFocusable(true);
addKeyListener(this);
// Import frames
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("show image");
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
// Image Setups
// Background
background = getImage(base, "data/background.png");
//Images
image1 = getImage(base, "data/image1.png");
image2 = getImage(base, "data/image2.png");
image3 = getImage(base, "data/image3.png");
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
imageString = randomString();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
paint(g);
g.drawImage(image, 0, 0, this);
}
public void paint(Graphics g) {
g.drawImage(background, 0, 0, this);
if (imageString == "color of green"){
g.drawImage(image1, 0, 0, this);
} else if (imageString == "red of color"){
g.drawImage(image2, 0, 0, this);
} else if (imageString == null){
g.drawImage(image3, 0, 0, this);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
}
public String randomString(){
int randNum;
String randString = null;
String randString1 = null;
randNum = (int)(Math.random()*2 + 1);
if (randNum == 1){
randString = "color";
randString1 = "green";
} else if (randNum == 2){
randString = "red";
randString1 = "color";
}
return randString + " of " + randString1;
}
}
没有任何内容屏幕,为什么?我相信它不会识别randString +的randString +因为如果我让它只返回randString,并在我的paint(图形g)中设置我的if语句,它可以工作,但是这段代码不...为什么?
谢谢!
Nothing is drawn on the screen, why? I believe its not recognizing "randString + " of " randString1" because if I made it return randString only, and set my "if statements" in my paint (Graphics g), it works, but this code does not... why?
Thanks!
推荐答案
首先这是错误的方法:它使'红色'
First this is around the wrong way: it makes 'red of color'
randString = "red";
randString1 = "color";
其次,使用==比较字符串是不正确的。 ==仅当对象相同时才为真。要测试字符串的'值'是否相等,你需要.Equals()。
我认为更好的方法是改变布尔颜色,整数或枚举颜色字符串。同样在randomString中,不需要有2个字符串变量,只需要一个。
Secondly, using == to compare strings is incorrect. == will be true only if the objects are identical. To test if the 'value' of Strings are equal you need .Equals().
I think a better approach is to make the colors Booleans, ints or enums instead of Strings. Also in randomString there's no need to have 2 string variables, you just need one.
这篇关于为什么paint(Graphics g)没有绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!