Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Circle Drawing Program

  1. #1
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Circle Drawing Program

    Hiya,

    I have to make a program for a school assignment that draws a x number of ovals when you type in a number in a JTextfield.

    Normally I know how to do it. But I have to do it in 3 different classes, a class Main, MyFrame and MyPanel.

    Now I don't know how to draw the circles in the MyFrame class with a for loop in the ActionListener.

    This is what I got so far.

    (Made this in Netbeans so that's where the package is for)

    Main
    Code:

    package week4
    ;


    //import javax.swing.UIManager;

    public class Main
    {
        public static 
    void main(String[] arg)
        {
           
    //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            
    new MyFrame();
        }


    MyFrame
    Code:
    package week4;

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



    public class 
    MyFrame extends JFrame implements ActionListener
    {

        private 
    MyPanel mp;
        public 
    JTextField tf;
        private 
    JButton b1;


        public 
    MyFrame()
        {
            
    mp = new MyPanel();
            
    tf = new JTextField(20);

            
    Container c getContentPane();
            
    c.add(mpBorderLayout.CENTER);
            
    JPanel p = new JPanel();
            
    p.add(tf);
            
    tf.addActionListener(this);
            
    c.add(pBorderLayout.SOUTH);
            
            
    pack();
            
    setVisible(true);
            
    setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
            public 
    void actionPerformed(ActionEvent e)
            {
      
            }
            



    MyPanel

    Code:

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


    public class 
    MyPanel extends JPanel
    {
        public 
    MyPanel()
        {
            
    setBackground(Color.WHITE);
            
    setPreferredSize(new Dimension(9001000));

        }

        public 
    void paintComponent(Graphics g)
        {
            
    super.paintComponent(g);
        }

        


    Hope someone got an answer for me !
    Last edited by SterAllures; 03-02-2010 at 05:02 AM.
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: Circle Drawing Program

    Not much has to be changed really.

    I added this to the actionperformed:
    Code:
    public void actionPerformed(ActionEvent e)
            {
                mp.drawCircles(Integer.parseInt(tf.getText()));
            }
    And a bit to the MyPanel:
    Code:
    public class MyPanel extends JPanel
    {
        int number;
        Random random;
        
        public MyPanel()
        {
            setBackground(Color.WHITE);
            setPreferredSize(new Dimension(800, 800));
            random = new Random();
        }
    
        public void paintComponent(Graphics g)
        {
            for(int i=0 ; i<number ; i++){
                g.fillOval(random.nextInt(780), random.nextInt(780), 10, 10);
            }
    
        }
    
        public void drawCircles(int n) {
            number=n;
            paintComponent(this.getGraphics());
        }
    }
    I think you'll figure it out what i changed and why... seems straightforward to me

    Actionperformed triggers if you enter in the TextField. No error handling is in there, so if you don't enter an integer it'll get messy
    Last edited by wim DC; 03-03-2010 at 08:27 AM.

  4. #3
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: Circle Drawing Program

    Hey Oxano,

    Thanks for that I've looked through it and Understand most of it, thanks for that!

    But I think I explained it a bit wrong.

    I have to create a program that draws a number of Circles horizontal next to eachother. so the amount I fill in in the JTextfield has to be the number of Circles.

    It looks like this



    What I changed and got so far is this(due to your help ):


    Main
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package week4;

    /**
     *
     * @author i7-Mel
     */
    //import javax.swing.UIManager;

    public class Main
    {
        public static 
    void main(String[] arg)
        {
           
    //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            
    new MyFrame();
        }


    MyFrame
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package week4;

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

    /**
     *
     * @author MelvinBlokhuijzen
     */


    public class MyFrame extends JFrame implements ActionListener
    {

        private 
    MyPanel mp;
        public 
    JTextField tf;


        public 
    MyFrame()
        {
            
    mp = new MyPanel();
            
    tf = new JTextField(20);

            
    Container c getContentPane();
            
    c.add(mpBorderLayout.CENTER);
            
    JPanel p = new JPanel();
            
    p.add(tf);
            
    tf.addActionListener(this);
            
    c.add(pBorderLayout.SOUTH);
            
            
    pack();
            
    setVisible(true);
            
    setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
            public 
    void actionPerformed(ActionEvent e)
            {
            
    mp.drawCircles(Integer.parseInt(tf.getText()));
            }
            


    MyPanel
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package week4;
    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;


    /**
     *
     * @author MelvinBlokhuijzen
     */
    public class MyPanel extends JPanel
    {
        
    int number;


        public 
    MyPanel()
        {
            
    setBackground(Color.WHITE);
            
    setPreferredSize(new Dimension(800800));
        }

        public 
    void paintComponent(Graphics g)
        {
            
    super.paintComponent(g);
            for (
    int regel regel <= regel++ )
                    {
                for ( 
    int hori hori <= hori++)
                            {
                    
    g.drawOval(5050100100);
                }
                    }

            
            
            
    /*for(int i=0 ; i<number ; i++)
            {
                g.drawOval(100, 100, 50, 50);
            }*/
        
    }

        public 
    void drawCircles(int n)
        {
            
    number=n;
            
    paintComponent(this.getGraphics());
        }


    The problem I'm having is how to draw multiple Circles with the amount you fill in in the JTextfield.
    Also the screen has to exactly stop at the last Horizontal circle with the getWidth() and pack() commands.

    Any Ideas?
    Last edited by SterAllures; 03-03-2010 at 07:18 AM.
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  5. #4
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: Circle Drawing Program

    This will do the trick
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    
    
    /**
     * @author MelvinBlokhuijzen
     */
    public class MyPanel extends JPanel {
        int number;
        int x, y;
        int circleDiameter = 400;
    
    
        public MyPanel() {
            setBackground(Color.WHITE);
            setPreferredSize(new Dimension(800, 800));
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int regel = 0, total = 0; regel < y + 1 && total < number; regel++) {
                for (int hori = 0; hori < x && total < number; hori++) {
                    g.drawOval((hori * circleDiameter), (regel * circleDiameter), circleDiameter, circleDiameter);
                    total++;
                }
            }
    
    
            /*for(int i=0 ; i<number ; i++)
            {
                g.drawOval(100, 100, 50, 50);
            }*/
        }
    
        public void drawCircles(int n) {
            number = n;
            x = this..getWidth() / circleDiameter;
            y = this.getHeight() / circleDiameter;
    
            paintComponent(this.getGraphics());
        }
    
    }
    So basicly i first calculate how many i can put on a row(x). Then i calculate how many rows are available(y). Then just keep drawing circles untill the amount of circles i drawed equals 'number' or untill it is filled, and the maximum number of rows is reached (y)

  6. #5
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: Circle Drawing Program

    Oxano I wanted to thank you this works perfectly!!!!!!!!!!

    Now I'm going to study the cody to actually understand what you did there .

    If I have any question about it could I still ask them?

    Really +rep for this and for all the trouble you did for me ;p.

    Thanks!!!
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  7. #6
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: Circle Drawing Program

    Yea sure. Ask ahead

  8. #7
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: Circle Drawing Program

    Well as you know the code worked and I had some time to look it over but I had one question.

    This method:
    public void drawCircles(int n)

    it says int n.

    is that a pointer to somenthing? I don't really get twhat it is used for.
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  9. #8
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: Circle Drawing Program

    n is the amount of circles that the user gave in.

    In the actionperformed of the button:
    Code:
    mp.drawCircles(Integer.parseInt(tf.getText()));
    Last edited by wim DC; 03-15-2010 at 07:20 AM.

  10. #9
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: Circle Drawing Program

    So the variable n let's say it's 5 goes into number so variable number changes into 5

    and the variable number is used here
    Code:
     for (int regel 0total 0regel && total numberregel++) {
                for (
    int hori 0hori && total numberhori++) { 
    so the variable number in the code above is the number you got from the input?


    (Hope you understand it ;p)
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  11. #10
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: Circle Drawing Program

    That is correct.
    So after the program has drawed 5 circles it goes out of the 2 for loops. Otherwise it would continue drawing.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 03:30 AM
  2. TASM circle area. plz help
    By thatsme in forum Assembly
    Replies: 1
    Last Post: 01-09-2010, 04:37 AM
  3. Filing a circle
    By CrashBeta in forum Visual Basic Programming
    Replies: 1
    Last Post: 09-13-2008, 11:26 AM
  4. [ C++ ] Drawing Program.
    By Max_Payne in forum C and C++
    Replies: 6
    Last Post: 01-23-2008, 07:18 PM
  5. Replies: 0
    Last Post: 05-26-2007, 08:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts