Results 1 to 8 of 8
  1. #1
    wata is offline Member
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default Question about school assignment

    Hello,
    I have to make a little app for school, I think 99.9% of you will find this so easy that it's not even worth replying to,
    but here we go:

    there need to be 1 field, where the user gives a number (1-99), he than presses a button, and will see a row of red circles, matching the number just entered (8 circles if the user entered 8). and every third circle has a blue color (3,6,9,...).
    Thats it.
    this is the code i have, but I've been searching for hours and nothing seems to do the trick.
    I would really apreciate the help.
    Cheers

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import java.awt.*;
    
    public class Main extends JFrame  {
    	public static void main(String[] args) {
    		 	JFrame frame = new Main();
    		    frame.setSize( 800, 200 );
    		    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		    frame.setTitle( "Eerste applicatie" );
    		    JPanel paneel = new Paneel();
    		    frame.setContentPane(paneel);
    		    frame.setVisible( true );
    
    	}
    	}
    	class Paneel extends JPanel{
    	private JTextField invoervak;
    	private JButton okknop;
    	public Paneel(){
    		invoervak = new JTextField (2);
    		okknop = new JButton("klikkeren");
    		add (invoervak);
    		add (okknop);
    	}
    class OkKnopHandler implements ActionListener{
    	public void actionPerformed( ActionEvent e ){
    		String invoerstring = invoervak.getText();
    		int getal = Integer.parseInt(invoerstring);
    	}
    }
    class Cirkels extends JPanel {
    	private int x=10, d=20, y=20;
    	public Cirkels() {
    	setBackground( Color.WHITE );
    			}
    public void paintComponent( Graphics g ) {
    	super.paintComponent( g );
    	g.setColor( Color.red );
    	
    	for (int i = 0; i< getal ; i++)
    	{
    	g.fillOval(x,y, d, d);
    	x=x+20;
    	}
    	}
    		
    	}
    	}

  2. #2
    weddy69 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    hi, first you have a error or your code...
    in paintComponent you have your varable getal you never pass or declare this varable in your class.
    that is where I would first start looking. Track this down and I think your program may work.

  3. #3
    wata is offline Member
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks
    How do you think I will need to do the color changing for every third circle to blue ?

  4. #4
    FF764 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    hi,
    for the every 3rd circle being blue problem, just add an if statement into the for loop in the void paintComponent() using a conditional
    Java Code:
    if ((i+1)/3 == 0) {
       g.setColor(Color.BLUE) 
    }
    else {
       g.setColor(Color.RED)
    }
    The app wont work at the moment because you havent added an instance of cirkels to the content pane of the JFrame.

    creating a new instance of Cirkels and adding it to the JPanel/Content pane of the JFrame...
    Java Code:
    Cirkels cirkels = new Cirkels();
    pannel.add(cirkels);
    the "graphics g" part is not an error, i dont know exactly how it is handeled by the jpanel, but it works as it is :D

  5. #5
    wata is offline Member
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much, but I seem to do something wrong, because I still get errors.
    I'm sorry for being such newbie :)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import java.awt.*;
    
    public class Main extends JFrame  {
    	public static void main(String[] args) {
    		 	JFrame frame = new Main();
    		    frame.setSize( 800, 200 );
    		    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		    frame.setTitle( "Eerste applicatie" );
    		    JPanel paneel = new Paneel();
    		    frame.setContentPane(paneel);
    		    frame.setVisible( true );
    
    	}
    	}
    	class Paneel extends JPanel{
    	private JTextField invoervak;
    	private JButton okknop;
    	
    	public Paneel(){
    		Cirkels cirkels = new Cirkels();
    		pannel.add(cirkels);
    		invoervak = new JTextField (2);
    		okknop = new JButton("klikkeren");
    		add (invoervak);
    		add (okknop);
    	}
    class OkKnopHandler implements ActionListener{
    	public void actionPerformed( ActionEvent e ){
    		String invoerstring = invoervak.getText();
    		int getal = Integer.parseInt(invoerstring);
    	}
    }
    class Cirkels extends JPanel {
    	private int x=10, d=20, y=20;
    	public Cirkels() {
    	setBackground( Color.WHITE );
    			}
    public void paintComponent( Graphics g ) {
    	String invoer = invoervak.getText();
    	super.paintComponent( g );
    	g.setColor( Color.red );
    	
    	for (int i = 0; i< invoer ; i++)
    	{
    		if ((i+1)/3 == 0) {
    			   g.setColor(Color.BLUE) 
    			}
    			else {
    			   g.setColor(Color.RED)
    			}
    	g.fillOval(x,y, d, d);
    	x=x+20;
    	}
    	}
    		
    	}
    	}

  6. #6
    FF764 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    so, took a while... but it seems as if the layout is causing problems... here is the code that works... have fun playing around with it ;)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class Main extends JFrame  {
        public Main() {
            super();
            setSize( 800, 800 );
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            setTitle( "Eerste applicatie" );
            JPanel paneel = new Paneel();
            setContentPane(paneel);
            setVisible( true );
        }
        
        public static void main(String[] args) {
            Main temp = new Main();
        }
    }
    
    class Paneel extends JPanel {
        private JTextField invoervak;
        private JButton okknop;
        private int getal;
        private Cirkels cirkels;
        
        public Paneel(){
            setSize(800,800);
            invoervak = new JTextField (20);
            okknop = new JButton("klikkeren");
            okknop.addActionListener(new OkKnopHandler());
            cirkels = new Cirkels();
            setLayout(new BorderLayout());
            NorthPanel aPanel = new NorthPanel();
            add (aPanel, BorderLayout.NORTH);
            aPanel.add (invoervak);
            aPanel.add (okknop);
            add (cirkels, BorderLayout.CENTER);
        }
        
        class OkKnopHandler implements ActionListener{
            public void actionPerformed( ActionEvent e ){
                String invoerstring = invoervak.getText();
                getal = Integer.parseInt(invoerstring);
                cirkels.repaint();
            }
        }
        
        class NorthPanel extends JPanel {
            
        }
        
        class Cirkels extends JPanel {
            private int x, d=20, y=20;
            
            public Cirkels() {
                setSize(500,500);
                setBackground( Color.WHITE );
            }
        
            public void paintComponent( Graphics g ) {
                super.paintComponent( g );
                g.setColor( Color.red );
                
                x = 10;
                int temp = 0;
                for (int i = 0; i < getal; i++)
                {
                    temp++;
                    if(temp == 3) {
                        g.setColor(Color.BLUE);
                        temp = 0;
                    }
                    else {
                        g.setColor(Color.RED);
                    }
                    g.fillOval(x, y, d, d);
                    x = x + 20;
                }
            }
        }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As long as we're showing code, myself, I would draw each circle in its own JLabel and if the panel holding the circles used a flow layout it could allow flow layout to automatically wrap the circles to the next line when they reach the end of the line. I also used a JSlider because it works nicer here. Something like so:

    edit: weddy is completely correct. Complete solution code deleted.
    Last edited by Fubarable; 08-18-2009 at 02:17 PM.

  8. #8
    weddy69 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    HI all,

    I know this is a school assignment so I limit just handing out code, in respect to helping figure out problems I'm all for it.

    With explanation to your question of how to change color Fubarable had it really nice way of doing so.

    color = < t/f statement> ? <true > : <false> ;
    <statement> ? <true> : <false> is a conditional operator expressions
    <statement> (i + 1) % 3 using this % operator is a mod operation
    <true> Color.red
    <false> Color.blue

    this is a one line piece of code that is very useful for this type of program.

Similar Threads

  1. Reord management for a school
    By 435.mahesh in forum Advanced Java
    Replies: 4
    Last Post: 03-22-2009, 02:59 PM
  2. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM
  3. Taking Java In School Need Help
    By xEuPhOrIcSx in forum New To Java
    Replies: 7
    Last Post: 02-04-2008, 08:02 AM
  4. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:51 PM
  5. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 08:12 AM

Posting Permissions

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