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
MyFrameCode:
package week4;
//import javax.swing.UIManager;
public class Main
{
public static void main(String[] arg)
{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new 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(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
Code:
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!
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?"
Not much has to be changed really.
I added this to the actionperformed:
And a bit to the MyPanel:Code:public void actionPerformed(ActionEvent e) { mp.drawCircles(Integer.parseInt(tf.getText())); }
I think you'll figure it out what i changed and why... seems straightforward to meCode: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()); } }
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.
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
MyFrameCode:/*
* 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();
}
}
MyPanelCode:/*
* 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()));
}
}
The problem I'm having is how to draw multiple Circles with the amount you fill in in the JTextfield.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(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());
}
}
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?"
This will do the trick
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)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()); } }
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?"
Yea sure. Ask ahead![]()
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?"
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.
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
so the variable number in the code above is the number you got from the input?Code:for (int regel = 0, total = 0; regel < y + 1 && total < number; regel++) {
for (int hori = 0; hori < x && total < number; hori++) {
(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?"
That is correct.
So after the program has drawed 5 circles it goes out of the 2 for loops. Otherwise it would continue drawing.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks