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