开发者

java canvas drawstring

I am wanting a string to be drawn at a random position, then jump to a new position but leave an imprint of itself behind.

There does not appear to be a "stamp" command in java.

How can I do it?

So far I only have a random character that jumps but does not leave a copy of itself behind.

import java.awt.Canvas;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class test extends Canvas {

private static Random random = new Rand开发者_JAVA百科om();

public void paint(Graphics g) {
    g.drawString("X", random.nextInt(10) * 10, random.nextInt(10) * 10);

    try {
        Thread.sleep(250);
    } catch (Exception e ) {}

    repaint();
}

public static void main(String[] argS) {
    test canvas = new test();
    JFrame frame = new JFrame();
    frame.add(canvas);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Recommendations:

  • Don't mix Swing with AWT components -- so instead of a Canvas object, use a JPanel.
  • Override the JPanel's paintComponent method.
  • Call the super.paintComponent method as the first line of paintComponent.
  • Use a Swing Timer to do your animation and never put Thread.sleep in a paint or paintComponent method. In fact you should avoid using Thread.sleep anywhere in a Swing GUI unless you know how to handle concurrency in Swing.
  • Never call repaint() in a paint or paintComponent method. You're lucky that the paint manager is smart enough not to allow that call lead to a recursion-induced stackoverflow error.


When you use repaint, the AWT system will call the canvas' update() method, which will clear the graphics (by painting a rectangle in the background color) and then call paint(). (This is for AWT's heavy-weight components like Canvas - for Swing it is a bit different.)

So, an easy workaround will be given by overriding update to not clear the component:

public void update(Graphics g) {
    this.paint(g);
}

Note that old painted strings still may go away when the system decides your component has to be invalidated, e.g. after another window covered it.

Unrelated to this:

You should never sleep in a painting method (other maybe for debugging). The painting will be done in the event dispatch thread, and as long as the paint method is not finished, no events will be dispatched. Don't do this.

Instead, have a separate thread (or even the main thread) do a loop which sleeps and calls repaint.

Also, as said by Hovercraft, don't mix AWT with Swing. Use an AWT Frame instead of Swing's JFrame, if you want to use Canvas.


Just keep in memory all previous positions and print them out too, maybe in a different color. You also don't say if you want the characters to be printed forever or just to stay visible for a few frames. If you want them to disappear after a while, you could put your random positions in a queue and remove the tail element of the queue (get rid of the older positions so they are not painted) as you paint new frames.

(The recommendations from Hovercraft and Paulo are very useful too.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜