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 6 of 6

Thread: Multiple questions - Java beans

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Multiple questions - Java beans

    Why does your CircleBean extend a JComponent? Where is your logic for detecting a press and changing the color?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.
    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
    jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            }

    my addition is below, same as before
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            circleBeanComponent1.repaint();
        }

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Slot Machine game in Java. using Net Beans.
    By leo24 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2012, 10:32 PM
  2. Some help on some multiple questions
    By djl1990 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 6th, 2012, 01:00 PM
  3. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  4. Net-beans-java
    By Tanmaysinha in forum Java IDEs
    Replies: 2
    Last Post: November 6th, 2011, 05:45 AM
  5. [SOLVED] Is this possible? (regarding beans and JSP)
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 31st, 2011, 12:58 AM