Displaying changing graphics in Java

Some hints on how to display changing graphics in Java using a simple example I have borrowed from the following site: http://www.dreamincode.net/forums/topic/30222-working-with-graphics-in-java/ I use the same approach to draw a number of concentric circles of differing sizes, but with a small delay in between each draw, refreshing the display at each iteration to give the illustration of the changing graphics. Full Java code follows: [code language="java"] import java.awt.*; import javax.swing.*; public class MainApp extends JPanel { private static final long serialVersionUID = 1L; static JFrame frame = new JFrame("BullsEye"); static int endpoint = 220; static int x, y; public static void main(String args[]) { MainApp eye = new MainApp(); frame.add(eye); frame.setSize(300,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (x = 20; x <= 110; x += 10) { endpoint = endpoint - 20; y = x + 10; frame.setVisible(true); try { Thread.sleep(200); frame.revalidate(); frame.repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } // This method fires to draw our circles when we go to paint it on the frame public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.drawOval(x,y,endpoint,endpoint); } } [/code] See video for quick demonstration: https://player.vimeo.com/video/294010165

Comments

Popular posts from this blog

Using the Supervisor Controller Pattern to access View controls in MVVM

Getting started with Tesseract optical character recognition (OCR) library in Visual Studio

How to send an e-mail via Google SMTP using C#