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

Thread: Drawing on a JFrame that has a Shape

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Drawing on a JFrame that has a Shape

    This post is cross posted here:
    Drawing on a JFrame that has a Shape (Swing / AWT / SWT forum at JavaRanch)

    Hi,
    I have created a JFrame for my application that has a custom shape. In this case a simple circle. How can I draw my own edge around the edge of the shape? I have tried adding the code to both paint() and paintComponent() and have noticed that in the debugger, it never gets to either paint routine. Any ideas as to how to do this?


        public static void main(String[] args)   
        {   
            myFrame = new JFrame("MyFrame");   
     
            myFrame .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
            myFrame .setBounds(400,200,700,700);   
            myFrame .setUndecorated(true);   
            myFrame .setShape( new Ellipse2D.Double(0,0,700,700));   
            myFrame .addMouseListener(new MyProgram());   
     
            Container myContainer=myFrame.getContentPane();   
            myContainer.setBackground(new Color(33,99,140));   
            myContainer.setLayout(null);   
            myContainer.setFocusable(true);   
     
            createButtons();   
            createPopupMenu();   
     
            myContainer.add(btnMenu);   
            myContainer.add(pmMenu);   
     
            ComponentMover cm = new ComponentMover();   
            cm.registerComponent(myFrame);   
     
            myFrame.setVisible(true);   
        }   
     
        public void paint(Graphics g)   
        {   
            g.setColor(Color.black);   
            g.drawOval(350, 350, 349, 349);   
            g.setColor(Color.gray);   
            g.drawOval(350, 350, 348, 348);   
     
        }   
        public void paintComponent(Graphics g)   
        {   
            g.setColor(Color.black);   
            g.drawOval(350, 350, 349, 349);   
            g.setColor(Color.gray);   
            g.drawOval(350, 350, 348, 348);   
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing on a JFrame that has a Shape

    never gets to either paint routine.
    What class is the paint methods code in? How does that class related to the JFrame object that is created in the main() method and set visible?

    Did you try adding the @Override as suggested in the other site.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing on a JFrame that has a Shape

    Norm,
    @Override does not work in the main, only from an extended class (at least that's what the compiler says anyway)

    So you are a moderator on both Forums?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing on a JFrame that has a Shape

    @Override does not work in the main
    @Override is used in classes before methods, not inside of a method like main().

    I'm not a mod at the ranch.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing on a JFrame that has a Shape

    Norm,
    I understand what you were trying to do in your example. Thanks. Here's what I got based on your suggestions:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    public class RoundFrame   
    {   
        public static void main(String[] args)   
        {   
            SwingUtilities.invokeLater
            (   
                new Runnable()   
                {   
                    @Override  
                    public void run()   
                    {   
                        new RoundFrame("Going Round in Circles");   
                    }   
                }   
    		);   
        }   
     
        private RoundFrame(String title)   
        {   
            final JFrame frame = new JFrame(title);   
            frame.setUndecorated(true);   
            frame.setBounds(new Rectangle(400,200,700,700));   
            frame.setShape(new Ellipse2D.Double(0,0,700,700)); 
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
            frame.setLayout(null);
            JButton closeButton = new JButton("Close");   
            closeButton.setMnemonic('c');   
            closeButton.setBounds(450,550,80,25);
            closeButton.addActionListener(new ActionListener()   
            {   
                @Override  
                public void actionPerformed(ActionEvent arg0)   
                {   
                    frame.dispose();   
                }   
            });   
            frame.add(closeButton);   
            RoundPanel panel = new RoundPanel(350,3);   
            panel.setBackground(new Color(33,99,140));
            frame.add(panel);   
            frame.setVisible(true);   
        }   
     
        private class RoundPanel extends JPanel   
        {
        	private int rpRadius = 1;
        	private int rpBorder = 1;
     
            private RoundPanel(int radius)   
            {   
                super();
                rpRadius = radius;
                this.setBounds(0,0,rpRadius*2, rpRadius*2);
            }   
     
            private RoundPanel(int radius, int border)   
            {   
                super();
                rpRadius = radius;
                rpBorder = border;
                this.setBounds(0,0,rpRadius*2, rpRadius*2);
            }   
     
            public int getRadius()
            {
            	return rpRadius;
            }
     
            public int getDiameter()
            {
            	return(rpRadius * 2);
            }
     
            public int getBorderThickness()
            {
            	return rpBorder;
            }
     
            @Override protected void paintComponent(Graphics g)   
            {   
                super.paintComponent(g);   
     
        		g.setColor(Color.black);
                for ( int i = 0; i < getBorderThickness(); i++)
                {
            		g.drawOval(i, i, getDiameter()-(i*2), getDiameter()-(i*2));
                }
        		g.setColor(Color.gray);
                for ( int i = getBorderThickness(); i < 2 * getBorderThickness(); i++)
                {
            		g.drawOval(i, i, getDiameter()-(i*2), getDiameter()-(i*2));
                }
     
            }   
        }   
    }

Similar Threads

  1. "Simple" Shape drawing!
    By incorsair in forum Object Oriented Programming
    Replies: 7
    Last Post: November 7th, 2012, 05:14 PM
  2. Replies: 1
    Last Post: April 30th, 2012, 08:16 AM
  3. Drawing to a JFrame From Other Classes
    By Destro in forum AWT / Java Swing
    Replies: 7
    Last Post: April 20th, 2012, 06:18 PM
  4. help with JFrame, drawing, and JButtons
    By Khoatic in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 12:34 AM
  5. [SOLVED] Drawing on JFrame
    By kbarrett1989 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2010, 03:41 AM