Welcome to Dream.In.Code
Become a Java Expert!

Join 149,510 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,345 people online right now. Registration is fast and FREE... Join Now!




Working with graphics in Java.

 
Reply to this topicStart new topic

Working with graphics in Java., Using For Loops with the DrawingPanel in Java.

CerebralAssassin
9 Jul, 2007 - 02:08 PM
Post #1

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 6


My Contributions
Hello, I just started getting into learning Java. I wanted to learn how to create graphics with a compiler. I was suggested to start with a large circle that has multiple smaller circles inside of it centered in one another. I started looking across the internet until I came across a pretty simple looking one, so I thought I would give it a try. Here is what I'm talking about, and what I've been working on for practice:

http://www.daube.ch/docu/graphics/moiree_5.gif

Here is the code I came up with for this:

CODE
import java.awt.*;

public class BullsEye {
    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(500, 500);
        panel.setBackground(Color.WHITE);
        Graphics g = panel.getGraphics();
        
        g.setColor(Color.BLACK);
        g.drawOval(230, 240, 20, 20);
        g.drawOval(220, 230, 40, 40);
        g.drawOval(210, 220, 60, 60);
        g.drawOval(200, 210, 80, 80);
        g.drawOval(190, 200, 100, 100);
        g.drawOval(180, 190, 120, 120);
        g.drawOval(170, 180, 140, 140);
        g.drawOval(160, 170, 160, 160);
        g.drawOval(150, 160, 180, 180);
        g.drawOval(140, 150, 200, 200);
    }
}


Is it possible to use for loops in a code like this to reduce some lines? In my quick code above, the x-coordinates are decrementing by 10 with each line, the y-coordinates are also decrementing by 10 with each line (but they begin with a y-coordinate that doesn't match the first x-coordinate of the first inside circle). In contrast, the width is incrementing by 20 on each line and the height is also incrementing by 20 on each line. I was wondering if someone could modify my code to use a for loop for the circle patterns to reduce some lines and explain to me how you did it? I'm not quite sure how to do for loops with graphics. Thanks in advance.

This post has been edited by CerebralAssassin: 9 Jul, 2007 - 02:12 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Working With Graphics In Java.
9 Jul, 2007 - 03:28 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi,

To help illustrate a solution for you, I modified the program into a swing enabled application so we can see something a bit more graphical. I have put some comments in the code so you can see how I did it. I find that if you see a pattern in a series of statements like that, try to extrapolate it out on a piece of paper. Figure out how each variable relates to one another. How does the value Y relate to X? Y = X + 10. Ever remember "Back substitution" when you did high school algebra? This is how it applies.

CODE

import java.awt.*;
import javax.swing.*;

public class BullsEye extends JPanel {
    public static void main(String args[]) {

          // Create our JFrame (main window) and make it 300 x 300
          JFrame frame = new JFrame("BullsEye");
          frame.setSize(300,300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          // Create an instance of our class and add it to the frame
          BullsEye eye = new BullsEye();
              frame.add(eye);
              frame.setVisible(true);
    }

    // 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);

           // Here is your loop. I started with the smallest x value (20)
           // Your Y value of course is 10 greater than X for each iteration
           // Lastly we start the end point at 220 so first one drawn is 200 and decrements 20 each time
           // Since the endpoint x and y are the same, I just used one variable to represent both

           int endpoint = 220;
           for (int x = 20; x <= 110; x += 10) {
                endpoint = endpoint - 20;
                int y = x + 10;
                g.drawOval(x,y,endpoint,endpoint);
           }

    }
}


Hope it all makes sense. Enjoy!

This post has been edited by Martyr2: 9 Jul, 2007 - 03:34 PM
User is offlineProfile CardPM
+Quote Post

CerebralAssassin
RE: Working With Graphics In Java.
9 Jul, 2007 - 05:48 PM
Post #3

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 6


My Contributions
Thank you for your help. It makes good sense to me. biggrin.gif

This post has been edited by CerebralAssassin: 9 Jul, 2007 - 08:45 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month