简体   繁体   English

在小程序上仅绘制一次字符串

[英]Drawing string on applet only once

New in Java and might be repeated question. Java 中的新内容,可能会出现重复问题。

Using full screen applet without title bar to render images on ever 10 millisecond time interval with help of paint method.使用不带标题栏的全屏小程序在绘画方法的帮助下以每 10 毫秒的时间间隔渲染图像。 At the same time I want specific text to be displayed on applet screen at any position.同时,我希望在任何 position 的小程序屏幕上显示特定文本。

Since images are dynamic hence I am rendering on 10 millisecond but text is fix and not going to change in whole life of applet.由于图像是动态的,因此我在 10 毫秒内渲染,但文本是固定的,不会在小程序的整个生命周期中改变。

Now concern is, if I draw text in paint method then its burden on paint method to draw image and text on every 10 millisecond time.现在担心的是,如果我在paint方法中绘制文本,那么它对paint方法的负担每10毫秒绘制一次图像和文本。

public void paint(Graphics  g) 
{   
    if(img != null)
    {
        g.drawImage(img, 0, 0, null);   
        g.drawString("Hey there!", 0, 0); //Additional load
    }           
}   

Cannot place text on title bar too.也不能在标题栏上放置文本。

Is it possible to draw text at once and rendered images frequently?是否可以一次绘制文本并频繁渲染图像?

If you want to only draw the text once, then I would recommend having a boolean variable outside your paint method:如果您只想绘制一次文本,那么我建议您在绘制方法之外使用 boolean 变量:

boolean drawn = true;
public void paint(Graphics g){
    if(drawn) g.drawString(...);
}

The problem with this is that, if you have any moving pixels or objects, (change position each frame) or simply anything that's supposed to disappear, you'll have to write a lot more to erase it.这样做的问题是,如果您有任何移动的像素或对象,(每帧更改 position)或任何应该消失的东西,您将不得不写更多的东西来擦除它。 It's simply more readable to layer paint commands on top of each other, and can be edited and created more easily.将绘画命令叠加在一起更易于阅读,并且可以更轻松地编辑和创建。

public void paint(Graphics  g) 
{ 

Should instead be:应该是:

public void paint(Graphics  g) 
{ 
     super.paint(g); // paint the background, borders etc.

So given doing that will erase the previously drawn text, the answer to your question is - no.因此,如果这样做会删除先前绘制的文本,那么您的问题的答案是 - 否。

BTW - look up 'premature optimization'.顺便说一句 - 查找“过早优化”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM