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

Thread: Drawing a shape

  1. #1
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Drawing a shape

    Hey, so my problem is that when my program draws a shape it is inside a small square, as you can see here(the arrow shows the small square):

    weg.jpg

    does anyone know how i can stop this from happening ?


    I want my whole window to be the painting JPanel, except for a row at the bottom just for my slider and labels/textboxes.

    This is the code that draws the shapes, I make an instance of it within one of my other classes.
    package calculations;
     
    import java.awt.*;
    import java.awt.geom.*;
     
    import javax.swing.JPanel;
     
    class DrawShape extends JPanel
    {
         private int x = 50, y = 10;
     
         public DrawShape(int newX, int newY)
         {
             x = newX;
             y = newY;
     
             this.setBackground(Color.GRAY);
             repaint() ;
         }
     
         @Override
                public void paintComponent(Graphics g)
                {
                    Graphics2D g2 = (Graphics2D) g;
     
                    super.paintComponent(g2);
     
                    BasicStroke pen = new BasicStroke(4F);
                    g2.setStroke(pen);
     
                    //Square
                    Rectangle rect1 = new Rectangle(x,y,50,50);
                    g2.setColor(Color.BLACK);
                    g2.draw(rect1);
     
                    //Circle
                    Ellipse2D.Float circle1 = new Ellipse2D.Float(x+100,y,50,50);
                    g2.setColor(Color.BLUE);
                    g2.draw(circle1);
     
                    int xCoords[] = {x+200,x+200,x+260};
                    int yCoords[] = {y,y+50,y+50};
     
                    //Triangle
                    Polygon poly1 = new Polygon(xCoords, yCoords,3);
                    g2.setColor(Color.RED);
                    g2.draw(poly1);
                }
     }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Drawing a shape

    I can't see what you're talking about in that picture. I took the code you posted and made it runnable. What's wrong with this?
    import java.awt.*;
    import java.awt.geom.*;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    class TestClass extends JPanel
    {
        private int x = 50, y = 10;
     
        public TestClass()
        {
            x = 50;
            y = 10;
     
            setPreferredSize( new Dimension( 380, 80 ) );
     
            setBackground(Color.GRAY);
        }
     
        @Override
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
     
            super.paintComponent(g2);
     
            BasicStroke pen = new BasicStroke(4F);
            g2.setStroke(pen);
     
            //Square
            Rectangle rect1 = new Rectangle(x,y,50,50);
            g2.setColor(Color.BLACK);
            g2.draw(rect1);
     
            //Circle
            Ellipse2D.Float circle1 = new Ellipse2D.Float(x+100,y,50,50);
            g2.setColor(Color.BLUE);
            g2.draw(circle1);
     
            int xCoords[] = {x+200,x+200,x+260};
            int yCoords[] = {y,y+50,y+50};
     
            //Triangle
            Polygon poly1 = new Polygon(xCoords, yCoords,3);
            g2.setColor(Color.RED);
            g2.draw(poly1);
        }
     
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
               public void run()
               {
                   JFrame frame = new JFrame();
     
                   frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                   frame.add( new TestClass() );
                   frame.pack();
     
                   frame.setVisible( true );
     
               } // end method run()
     
            } );
     
        } // end method main()
     
    } // end class TestClass

Similar Threads

  1. Drawing on a JFrame that has a Shape
    By tim8w in forum AWT / Java Swing
    Replies: 4
    Last Post: February 14th, 2013, 05:25 PM
  2. "Simple" Shape drawing!
    By incorsair in forum Object Oriented Programming
    Replies: 7
    Last Post: November 7th, 2012, 05:14 PM
  3. Shape error?
    By xTommy24 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:46 AM
  4. Shape Calculation Program
    By TH1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 03:54 PM
  5. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM