Multiple questions - Java beans
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
Code :
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
Code :
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
Code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
circleBeanComponent1.repaint();
}
Re: Multiple questions - Java beans
You've got program logic in your paint or paintComponent method, and that is something that shouldn't be done. These methods should do nothing but paint and should not set the state of the class since you do not have full control over when or even if they are called, and they also may be called by forces other than your code, such as when the OS tells the JVM that a section of window is "dirty" and needs to be repainted.
I suggest that you take all code that sets any property out of these methods and into the class proper where it belongs. Have your JButton's ActionListener call the method you've created and then repaint() or have that method call repaint().
Likewise you should not be setting preferred sizes inside of paintComponent or creating new objects. Again it should be for painting only.
Finally, I don't see where you're calling the super paintComponent method. You should do this at the start of your paintComponent method.
Re: Multiple questions - Java beans
Why does your CircleBean extend a JComponent? Where is your logic for detecting a press and changing the color?
Re: Multiple questions - Java beans
Ok this is my new circleComponent. I think it is working to some extent. When i drag it onto my frame the colour changes. But still when i press the JButton it does not change.
Code :
package my.GraphicsBean;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class CircleBeanComponent extends JComponent
{
CircleBean circle;
private Color aColor = Color.GREEN;
Graphics g;
Graphics2D g2 = (Graphics2D) g;
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle = new CircleBean(aColor);
circle.setPreferredSize(new Dimension(200,200));
circle.paintComponent(g2);
setCircleColor();
}
public void setCircleColor()
{
if(aColor.equals(Color.GREEN)){
aColor = Color.RED;
} else if(aColor.equals(Color.RED)) {
aColor = Color.ORANGE;
} else if(aColor.equals(Color.ORANGE)) {
aColor = Color.GREEN;
}
}
}
CircleBean shouldn't of extended JComponent, that was a mistake.
Code :
package my.GraphicsBean;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class CircleBean
{
private Color color;
public CircleBean(Color aColor)
{
color = aColor;
}
public void paintComponent(Graphics2D g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
Ellipse2D.Double circle = new Ellipse2D.Double(0,0,200,200);
g2.fill(circle);
}
}
And this is the (auto-generated) code to add the actionListener to the JButton
Code :
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}
my addition is below, same as before
Code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
circleBeanComponent1.repaint();
}
Re: Multiple questions - Java beans
Have you stepped through this with a debugger, or at least added some print statements, to figure out what's going on? I don't think things are happening in the order you expect them to...
Re: Multiple questions - Java beans
Problem is sorted now. I was changing the colour but not repainting the component. I have added the extra line and it is fine now.
Thanks for the help!