Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Circle Drawing Program

setpreferredsize

  • Please log in to reply
10 replies to this topic

#1 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 02 March 2010 - 03:19 AM

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


package week4;


//import javax.swing.UIManager;

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

new MyFrame();
}

}


MyFrame

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(mp, BorderLayout.CENTER);
JPanel p = new JPanel();
p.add(tf);
tf.addActionListener(this);
c.add(p, BorderLayout.SOUTH);

pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{

}


}



MyPanel



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


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

}

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


}



Hope someone got an answer for me ;)!

Edited by SterAllures, 02 March 2010 - 05:02 AM.

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

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 02 March 2010 - 09:46 AM

Not much has to be changed really.

I added this to the actionperformed:
public void actionPerformed(ActionEvent e)
        {
            mp.drawCircles(Integer.parseInt(tf.getText()));
        }

And a bit to the MyPanel:
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 :P

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

Edited by wim DC, 03 March 2010 - 08:27 AM.

  • 0

#3 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 03 March 2010 - 04:10 AM

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

Posted Image

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


Main
/*
* 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
/*
* 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(mp, BorderLayout.CENTER);
JPanel p = new JPanel();
p.add(tf);
tf.addActionListener(this);
c.add(p, BorderLayout.SOUTH);

pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
mp.drawCircles(Integer.parseInt(tf.getText()));
}


}




MyPanel
/*
* 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(800, 800));
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int regel = 1 ; regel <= 4 ; regel++ )
{
for ( int hori = 1 ; hori <= 6 ; hori++)
{
g.drawOval(50, 50, 100, 100);
}
}



/*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?

Edited by SterAllures, 03 March 2010 - 07:18 AM.

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

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 03 March 2010 - 08:24 AM

This will do the trick ;)
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)
  • 1

#5 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 03 March 2010 - 09:45 AM

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

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

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!!!
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 03 March 2010 - 10:33 AM

Yea sure. Ask ahead :D
  • 0

#7 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 15 March 2010 - 04:34 AM

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.
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 15 March 2010 - 05:08 AM

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

In the actionperformed of the button:
mp.drawCircles(Integer.parseInt([B]tf.getText()[/B]));

Edited by wim DC, 15 March 2010 - 06:20 AM.

  • 0

#9 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 15 March 2010 - 05:33 AM

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

for (int regel = 0, total = 0; regel < y + 1 && total < number; regel++) {
for (int hori = 0; hori < x && total < number; hori++) {


so the variable number in the code above is the number you got from the input?


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

#10 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 15 March 2010 - 06:21 AM

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

#11 SterAllures

SterAllures

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 188 posts

Posted 15 March 2010 - 06:24 AM

Oke I get it :D.

Well I have no more question I guess.

I understand everything you helped me with!.

Thanks alot for helping me out, helps me to get ahead of my exams ;p
  • 0
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download