I have a few questions, i am sure they all have simple answers but i just cant seem to get them working right now.
First problem. I have a circle class, i then have a CircleBeanComponent that i add to my JFrame using the netbeans GUI builder (i would prefer not to use the builder but i have to for this).
When a JButton is clicked the circleComponent should change colour, at the moment the circle displays but when pressed the colour does not change
Circle class
package my.GraphicsBean; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; public class CircleBean extends JComponent { private String color = "green"; public CircleBean(String aColor) { color = aColor; } public void paintComponent(Graphics2D g) { Graphics2D g2 = (Graphics2D) g; if(color.equals("green")) { g2.setColor(Color.RED); color = "red"; } if(color.equals("red")) { g2.setColor(Color.ORANGE); color = "orange"; } if(color.equals("orange")) { g2.setColor(Color.GREEN); color = "green"; } Ellipse2D.Double circle = new Ellipse2D.Double(0,0,200,200); g2.fill(circle); } public String getColor() { return color; } public void setColor(String nextColor) { color = nextColor; } }
CircleBeanComponent
package my.GraphicsBean; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class CircleBeanComponent extends JComponent { CircleBean circle; private String color = "green"; Graphics g; Graphics2D g2 = (Graphics2D) g; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; circle = new CircleBean(color); circle.setPreferredSize(new Dimension(200,200)); circle.paintComponent(g2); color = circle.getColor(); setCircleColor(); circle.setColor(color); } public void setCircleColor() { if(color.equals("green")){ color = "red"; } else if(color.equals("red")) { color = "orange"; } else if(color.equals("orange")) { color = "green"; } } }
The JButton code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { circleBeanComponent1.repaint(); }