Issue with AWT drawString method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Issue with AWT drawString method

I have an update function where the bird falls minus one unit per second (bird.position.y += bird.velocity.y * deltaTime) (bird.velocity.y is -1.0f), and I can up its position.y by pressing SPACE by 0.4 unit (if (Key.isPressed(Key.SPACE) { bird.position.y += 0.4f). Now with "commenting aka // aka /**/" the drawString method everything works perfectly fine I can see the bird before it falls of the screen. Now when I uncomment the drawString method, it takes some time to load and then when it finally loads the bird is no longer in the screen, meaning it fell off already, how can I solve this problem? @Override public void paintComponent(Graphics g) { Graphics2D g2D = (Graphics2D)g; g2D.setBackground(new Color(255, 255, 255)); bird.draw(); // work perfectly fine (draws green circle) g2D.setColor(new Color(0, 0, 0); g2D.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 30)); g2D.drawString("Current Score: bird.score", width() / 2, height() / 2); } Also if you know, how to I center the bounding box of a text to a certain position?

29th Dec 2020, 5:01 AM
Frumkin
Frumkin - avatar
0 Answers