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

Thread: JButton with 2D Graphics help

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default [SOLVED] JButton with 2D Graphics help

    Hello, I'm having some issues getting a JButton to show on a frame where I have created some 2D graphics objects. Objective is to have the button change the smile displayed to a frown when the button is pressed. Any help would be much appreciated.

    I have tried adding the button both in the constructor and in the paint method. I have also tried repositioning it in both methods.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
     
    public class JSmileyFace2 extends JFrame implements ActionListener
    {
     
        private JButton mouth = new JButton("Smile");
     
        public JSmileyFace2()
        {
            super("Smile");
            setVisible(true);
            setSize(300,320);
            setBackground(Color.WHITE);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            add(mouth);
            mouth.addActionListener(this);
        }
     
        public void paint(Graphics gr)
        {
            super.paint(gr);
     
            Graphics2D gr2d = (Graphics2D)gr;
            BasicStroke brush = new BasicStroke(5.0f, BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_ROUND);
            gr2d.setStroke(brush);
     
            //Draws outer smiley
            gr2d.setPaint(Color.YELLOW);
            gr2d.fill(new Ellipse2D.Float(30F, 80F, 200F, 200F));
            gr2d.setPaint(Color.BLACK);
            gr2d.draw(new Ellipse2D.Float(30F, 80F, 200F, 200F));
     
            //draws eyes
            gr2d.fill(new Ellipse2D.Float(90F, 110F, 20F, 60F));
            gr2d.fill(new Ellipse2D.Float(150F, 110F, 20F, 60F));
     
            //draws mouth based on button text
            if(mouth.getText().equals("Smile")) //draws smile
                gr2d.draw(new Arc2D.Float(70F, 140F, 120F, 120F, 180F, 180F, Arc2D.OPEN));
            else //draws frown
                gr2d.draw(new Arc2D.Float(70F, 180F, 120F, 120F, 180F, -180F, Arc2D.OPEN));
            mouth.setLocation(20,20);
        }
     
        public void actionPerformed(ActionEvent e)
        {
            if(mouth.getText().equals("Smile"))
                mouth.setText("Frown");
            else
                mouth.setText("Smile");
            repaint();
        }
     
        public static void main(String[] args)
        {
            JSmileyFace2 frame = new JSmileyFace2();
        }
    }
    Last edited by mystikaljester; November 13th, 2010 at 06:25 PM. Reason: Problem resolved


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JButton with 2D Graphics help

    try setting the button to be visible.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JButton with 2D Graphics help

    mouth.setVisiible(true);

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JButton with 2D Graphics help

    You never set the text of the JButton mouth to anything.

    At least not in the constructor.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JButton with 2D Graphics help

    You never set the text of the button.

    Is your starting one "Smile" or "Frown"?

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

    mystikaljester (November 13th, 2010)

  7. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: JButton with 2D Graphics help

    Text for the smile is set in the line just before the constructor and outside of any methods.
    private JButton mouth = new JButton("Smile");
    That should set the starting text as "Smile"

    And now I have discovered that I am a moron. My problem is not in my code, but a problem with NetBeans. This was originally started as just a smiley face, then the request for the smile/frown button was added. I created a second file for the button request, but created it in the same project. Every time I was running the code, it was running the original file with no button due to it being set as the main class for the project. Thanks for the help all. Sorry for wasting your time.

  8. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: JButton with 2D Graphics help

    Also of note for anyone else doing this as a homework assignment out of the book "Java Programming" (Fifth Edition) by Joyce Farrell (Page 757, problem 10):

    The above code works perfectly as soon as a layout manager is added. I used
    setLayout(new FlowLayout());
    and added it to the JSmileyFace2 constructor.

  9. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JButton with 2D Graphics help

    Custom painting in Swing is done by overriding paintComponent(...) in a JComponent / JPanel, not paint(...) in a top level window. Read the API for JComponent#paint(...) for better understanding.

    Recommended reading:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  2. wrong graphics
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 09:33 AM
  3. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  4. Graphics mayhem
    By javapenguin in forum AWT / Java Swing
    Replies: 2
    Last Post: August 9th, 2010, 05:21 PM
  5. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM