这似乎是一个非常愚蠢的问题,但我真的需要帮助,我正在做一个任务,绘制一个背景透明渐变图像,然后在它上面绘制一些对象,问题是我想绘制这个图像一次,但其他对象将被多次绘制以执行一些动画
代码如下,这是我想运行一次的代码,我创建了一个 boolean 变量= false,然后将其设置为true
public void drawLockLayer(Graphics g) {
try {
lock = Image.createImage(Paths.lock);
g.drawImage(lock, 0, 0, LGBMainMidlet.width, LGBMainMidlet.height);
System.out.println("After Draw Image");
drawOnce = true;
} catch (Exception ex) {
ex.printStackTrace();
}
}
其他代码如下
public void paint(Graphics g, Rectangle rect) {
synchronized (g) {
if (drawOnce == false) {
drawLockLayer(g);
}
}
pos = (int) ((System.currentTimeMillis() % 2700) / 300);
int i = 0;
g.setColor(bgColor);
g.fillRoundRect(startX, startY, width, height, 20, 20);
g.setColor(fgColor);
g.setFont(font);
g.drawString(loadMsg, startX + (spacing / 4), startY + (spacing / 4));
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
int thickness = 6;
if (i == pos) {
thickness = 14;
} else if (i == pos - 1) {
thickness = 10;
}
g.fillRect((startX + x * spacing - (thickness / 2)) + (width / 3), (startY + y * spacing - (thickness / 2)) + (height / 3), thickness, thickness);
i++;
}
}
}
它是进入方法但不是绘制背景图像,我想做的是锁定图形对象,直到他完成绘制图像然后继续其他代码
谁能帮忙
最佳答案
代码的几个问题:
- 在图形上同步通常是一个坏主意
- paint 只能从一个线程调用。您确定不是并且您确实需要同步吗?矩形从哪里来?
- 为了更容易维护,应该在paint()中将drawOnce设置为true
关于java - J2ME 中锁定图形对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243981/