Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JGrid and JButton issues.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JGrid and JButton issues.

    Hello everyone. I'm working on a project to create some flower petals into a 3x3 grid. Without using the grid I could increment the number of petals on the flower using the JButtons to increment or decrement them. My problem is that when I apply the flower to the 3x3 grid, the flower petals will not change when i use the JButtons. Im using three classes to achieve a solution to my problem.


    flower.JPG


    Flowerc.java creates one single drawing of the flower.

    // Import the basic graphics classes.
    import java.awt.*;
    import javax.swing.*;
     
     
    public class Flowerc extends JPanel{
     
    	int petal = 0;
    	//class called increment
        public void inc(int i){
           	petal+=i; 
        	repaint();
        	System.out.println(petal);}
     
        public Flowerc(int petaln){
        	 super();
        	 setPreferredSize (new Dimension(100, 100));
     
     
        	petal = petaln;
     
        }
     
        public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g.
    	int s = 100;
    	int x = 0;
    	int y = 0;
    	int halfs = s/2;
     
     
    	//DRAWING 2x2 GRID
    	for (x = 0; x<s; x+=halfs){
    		for (y = 0; y<s; y+=halfs){
    		g2d.drawRect(x, y, halfs, halfs);
    	}}
     
    	double ang_inc;
     
    		switch (petal) {
      case 0: 
        ang_inc = 90;
        break;
      case 1: 
        ang_inc = 45;
        break;
      case 2: 
        ang_inc = 22.5;
        break;
      default: 
        ang_inc = 361;
    }
    int width = 10;
    int length = 30;
     
     
             g2d.translate(halfs, halfs); // Translate the center of our coordinates.
     
     		//draw petals      
             for(double i = 0; i <360; i += ang_inc){
     
             g2d.rotate(Math.toRadians(i));  // Rotate the image by i radian.
             g2d.drawOval(-width/2, width, width, length);}
        }
     
    //MAIN CLASS
    ///* 
     	   public static void main(String arg[]){
           JFrame frame = new JFrame("RotateImage");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     //      frame.setSize(300,300);
     
           Flowerc panel = new Flowerc(1);
     
           frame.setContentPane(panel);  
     
           frame.pack();
           frame.setVisible(true);  }
    //*/    
    }

    gridgrid.java creates a 3x3 grid and adds the flowers in it

    import java.awt.*;
    import javax.swing.*;
     
    public class gridgrid extends JPanel
    {
    	int flowerclasspetal = 0;
     
     
    	    public void incr(int i){
           	flowerclasspetal+=i; 
     //   	repaint();
        	System.out.println(flowerclasspetal);
    	    }
     
     
     
       public gridgrid(int flowerclasspetaln)
       {
       	flowerclasspetal = flowerclasspetaln;
     
     
          setLayout (new GridLayout (3, 3));
     
          setBackground (Color.green);
     
    	for (int i = 0; i <9; i ++){
    		add (new Flowerc(flowerclasspetal));
    	}
     
          }
     
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("Layout Manager Demo");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
          gridgrid panel = new gridgrid(1);
    //     
     
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
    }

    flowerPanel.java tries to incooperate the JButtons working which doesnt seem to work at the moment.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    public class flowerPanel extends JPanel
    {
       private JButton but1; 
       private JButton but2;
     
     
       private JLabel label1;
       private JLabel label2;
       private int count;
       private 	Flowerc flower = new Flowerc(0);
       private gridgrid flowergrid = new gridgrid(0);
     
       public flowerPanel ()
       {
     
    //set buttons n listener
     
          but1 = new JButton ("+");
          but1.addActionListener (new ButtonListener(1));
          but2 = new JButton ("-");
          but2.addActionListener (new ButtonListener(-1));
     
    //set labels
          label1 = new JLabel ("Add Petals");
     
    //add to panelz
    	add (label1);
    	add (but1);
    	add (but2);
    	add (flowergrid);
     
     
          setPreferredSize (new Dimension(400, 400));
          setBackground (Color.gray);
    }
     
       //*****************************************************************
       //  Represents a listener for button push (action) events.
       //*****************************************************************
       private class ButtonListener implements ActionListener
       {
          private int inc;
          ButtonListener(int i){
          	inc =i;
          }
     
          public void actionPerformed (ActionEvent event)
          {
           flowergrid.incr(inc);
          }
       }
     
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("flower");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
          frame.getContentPane().add(new flowerPanel());
     
          frame.pack();
          frame.setVisible(true);
       }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JGrid and JButton issues.

    flower petals will not change when i use the JButtons.
    How does the paintComponent method get information about how to change what it is drawing?

  3. The Following User Says Thank You to Norm For This Useful Post:

    zaxahmed (August 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JGrid and JButton issues.

    I can't figure out how to update that information. If you can point me in the right direction? Thank you.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JGrid and JButton issues.

    You need to understand how your program is executing. Try debugging your code by Adding some printlns to all the methods to show when they are being called and what the values of the variables are when they are called. Especially the number of petals to be displayed. Look at the printed out messages to understand where your code is executing and where it is not executing.

  6. The Following User Says Thank You to Norm For This Useful Post:

    zaxahmed (August 21st, 2011)

Similar Threads

  1. [SOLVED] Image issues
    By Toll in forum Java Theory & Questions
    Replies: 7
    Last Post: May 19th, 2011, 09:43 PM
  2. Recursion issues.
    By ender16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2011, 09:03 PM
  3. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  4. ResultSet issues
    By _lithium_ in forum JDBC & Databases
    Replies: 3
    Last Post: March 4th, 2011, 03:20 PM
  5. issues with Class
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 20th, 2010, 01:49 PM