How to add another circle in java coding

so today I'm was making a program and as i'm still a beginner I'm still learning but i'd like to know how to add another circle, for instance I have two units, Red and Blue, I have added the randomize which randomly selects the x and y position, but when I click start it only shows one circle which is red, the blue one is not even there, I know i have not done some coding, but here's my program, please help thanks :)

so yh :) thanks in advance.

Upvotes: 1

Views: 229

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

A few things to change here:

  • Drop all AWT components (Canvas, Panel, etc...) and replace them with their equivalent Swing one (JPanel, JTextField...). This will avoid rendering issues and bring double buffering (without any code to perform).
  • Don't ever use c.getGraphics().
  • Override paintComponent(Graphics g) and use the Graphics g parameter provided there (see also this link for some example)
  • To perform "animation" use a javax.swing.Timer. All updates to the UI must be done on the EDT (Event Dispatching Thread). Read also about concurrency in Swing
  • When using JOptionPane.showMessageDialog (or any other dialog), provide a valid parent component and not null. This will allow proper parenting of windows (avoiding dialogs to be hidden by other frames).

Upvotes: 5

Related Questions