Forum Stats

  • 3,839,868 Users
  • 2,262,546 Discussions
  • 7,901,087 Comments

Discussions

Filling a shape

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Oct 24, 2015 2:21PM in New To Java

Hi,

I have a shape which is like a polygon but some of its connectors are curve. I cant apply fillPoly, fillRect  & fillOval. Some body please guide me how to fill such objects.

Zulfi.

«1

Answers

  • Unknown
    edited Oct 5, 2015 12:13PM
    I have a shape which is like a polygon but some of its connectors are curve. I cant apply fillPoly, fillRect  & fillOval. Some body please guide me how to fill such objects.
    

    DUPLICATE THREAD!

    This is your fourth thread on the same basic theme of how to use Graphics in Java. I already answered your question in one of your other threads.

    You need to LEARN how to use graphics before you start trying to use them.

    Go through The Java Tutorials trails at that link I provided and actually TRY the examples. It shows you, with working code, how to draw graphic objects and fill them.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 5, 2015 1:52PM

    Hi,

    I have made many programs in graphics using JAVA now. However I did not find any command for filling non-polygonal closed shapes. You told me about fill(shapes) command. But i cant find how to use fill shapes on customerize images. Please tell me some command or the link in tutorial for this command with respect to non-polygonal shapes other than built-in shapes like ellipse & ovals.

    Zulfi.

  • Unknown
    edited Oct 5, 2015 6:08PM
    I have made many programs in graphics using JAVA now. However I did not find any command for filling non-polygonal closed shapes. You told me about fill(shapes) command. But i cant find how to use fill shapes on customerize images. Please tell me some command or the link in tutorial for this command with respect to non-polygonal shapes other than built-in shapes like ellipse & ovals.
    
    
    

    It's all right there in The Java Tutorials link I gave you many hours ago. I can't read it for you; you have to do that yourself

    https://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html

    Stroking and Filling Graphics Primitives

    You already know how to create different geometric primitives and more complicated shapes. This lesson teaches how to add some color and fancy outlines to your graphics and represents filling and stroking:
    
    

    A 'fill' is a 'fill' is a 'fill' - it 'fills' until it hits the boundary of whatever object you are filling.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 6, 2015 5:48AM

    Hi,

    Are you talking about this code:

    // fill and stroke GeneralPath

            int x3Points[] = {x, x+rectWidth, x, x+rectWidth};

            int y3Points[] = {y, y+rectHeight, y+rectHeight, y};

            GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,

                                                        x3Points.length);

            filledPolygon.moveTo(x3Points[0], y3Points[0]);

            for ( int index = 1; index < x3Points.length; index++ ) {

                filledPolygon.lineTo(x3Points[index], y3Points[index]);

            };

            filledPolygon.closePath();

            g2.setPaint(red);

            g2.fill(filledPolygon);

    It has got only 3 points and there is no curve edge. I have lines and curve edges which result in the closed shape. Please explain me how can i use it for a close shape having curves and straight lines as edges.

    Zulfi.

  • morgalr
    morgalr Member Posts: 457
    edited Oct 6, 2015 7:01PM

    So I will make the assumption that you have this shape and it is an aggregate/intersection of Polygons and Curves/Circles. If that is the case, you are quite right. Java, as far as I have found, does not support with a fill any shape that is not derived from their Shape class. So you have a problem. The way I would go about solving the problem is to look at the individual objects that go into making the shape. So let's just for grins, say you have a Star that is intersected by a Circle at some point. I must ask:

    Do you have the Java Class source that produces your aggregate shape?

    If you do, can you change the source to produce the shape using overlaying objects supported by Shape?

    If you can, then just write a fill method incorporating those objects' fill support in Graphics:

    ie: it's is made by a polygon and a circle, so using the same method of drawing the object outlines, use fills instead.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 7, 2015 2:09PM

    Hi,

    Thanks for your response. I have tried.


    >it 'fills' until it hits the boundary of whatever object you are filling.

    Its not like flood fill. It left the portion when it touched the arc starting coordinate (i guess).

    This is my code:

    import java.applet.Applet;

    import java.awt.*;

    import javax.swing.*;

      public class MountainFill extends Applet{

        public  void paint(Graphics g) {  

           mountain(g);

           fillMountain(g);

       }

    public void mountain(Graphics g) {

     

    Color brownColor= new Color(150, 75, 0);

      g.setColor(brownColor);

      

       g.drawLine(0,250,0,160);

       g.drawLine(0,160,30,200);

       g.drawLine(30,200,80,120);

       g.drawArc(77,114,25,25,30,150);

       g.drawLine(99,119,150,180);

       g.drawLine(150,180,220,100);

       g.drawLine(248,99,320,200);

       g.drawArc(217,89,35,45,30,150);

       g.drawLine(320,200,370,125);

       g.drawArc(365,117,28,38,30,150);

       g.drawLine(389,124,430,180);

       g.drawLine(430,180,505,90);

       g.drawArc(500,80,35,38,30,150);

       g.drawLine(533,89,605,200);

       g.drawLine(605,200,700,170);

       g.drawLine(700,170,700,250);

       g.drawLine(700,250,0,250);

       }

       public void fillMountain(Graphics g) {

          // super.paintComponent( g );

        Graphics2D g2d = (Graphics2D)g.create();

        Polygon triangle = new Polygon();

        triangle.addPoint(0, 250);

        triangle.addPoint(0, 160);

        triangle.addPoint(30, 200);

        triangle.addPoint(80, 120);

        triangle.addPoint(77, 114);

        triangle.addPoint(99, 119);

        triangle.addPoint(150, 180);

    triangle.addPoint(220, 100);

    triangle.addPoint(217, 89);

    triangle.addPoint(320, 200);

    triangle.addPoint(365, 117);

    triangle.addPoint(389, 124);

    triangle.addPoint(430, 180);

    triangle.addPoint(505, 90);

    triangle.addPoint(500, 80);

    triangle.addPoint(533, 89);

    triangle.addPoint(605, 200);

    triangle.addPoint(700, 170);

    triangle.addPoint(700, 250);

    mountainfillprob.png

      

        g2d.setColor( Color.RED );

        g2d.fill( triangle );

    }

      }

    Please run the program if you cant see the problem in the image. Please guide me how to fill the image completely in Java.

    Zulfi.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 8, 2015 7:37AM

    Hi,

    >If you do, can you change the source to produce the shape using overlaying objects supported by Shape?

    Kindly look at my drawing & tell me what overlaying objects you are talking about.


    Zulfi.

  • morgalr
    morgalr Member Posts: 457
    edited Oct 8, 2015 6:57PM

    Your entire image is made from lines and arcs, so basically you have intersecting trapezoids with portions of spheres on top.

    If you are doing this to produce a background for a game, then you need to become familiar with offscreen rendering too, or even better render a BufferedImage and draw it onto the Graphics object of your container.

  • morgalr
    morgalr Member Posts: 457
    edited Oct 8, 2015 8:21PM

    Here's a basic example:

    package jsupportedshapesfill;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Polygon; import javax.swing.JFrame;
    import javax.swing.JPanel; public class JSupportedShapesFill{
      public JSupportedShapesFill(){
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyJPanel p = new MyJPanel();
        p.setPreferredSize(new Dimension(300, 200));
        f.add(p);
        f.pack();
        f.setVisible(true);
        p.repaint();
      }
      public static void main(String[] args){
        new JSupportedShapesFill();
      }
      class MyJPanel extends JPanel{
        public MyJPanel(){ 
        }
        public void mountain(Graphics g, int x, int y, int width, int height, int dpoint, Color c){
          g.setColor(c);
          Polygon p = new Polygon();
          p.addPoint(x, y);
          p.addPoint(x+(width-dpoint)/2, y-(height+dpoint/8));
          p.addPoint(dpoint+x+(width-dpoint)/2, y-(height+dpoint/8));
          p.addPoint(x+width, y);
          g.fillPolygon(p);
          g.fillArc(x+(width-dpoint)/2, y-(height+height/16), dpoint, height/8, 0, 180);
        }
        @Override
        public void paintComponent(Graphics g){
          super.paintComponent(g);
          this.mountain(g, 0, 200, 100, 150, 20, Color.GREEN);
          this.mountain(g, 50, 200, 100, 100, 30, Color.RED);
          this.mountain(g, 130, 200, 200, 160, 50, Color.GREEN);
        }
      }
    }
This discussion has been closed.