Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Fill in a shape

Sun Oct 26, 2008 3:56 am

Hi,

I'm trying to fill in a shape with color made with lines and arcs. The arcs curve inward toward the center of the solid, so fillPolygon and fillArc are not options. Can anyone point me in the right direction?

Thanks,
-Joe



Re: Fill in a shape

Sun Oct 26, 2008 10:14 am

Is this code help you ..

Code:
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FillPolyPanel extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int radius = 40;
    int centerX = 50;
    int centerY = 100;
    int angle = 30;

    int dx = (int) (radius * Math.cos(angle * Math.PI / 180));
    int dy = (int) (radius * Math.sin(angle * Math.PI / 180));

    g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, angle, 360 - 2 * angle);

    Polygon p = new Polygon();
    centerX = 150;
    for (int i = 0; i < 5; i++)
      p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)),
          (int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5)));

    g.fillPolygon(p);

    p = new Polygon();
    centerX = 250;
    for (int i = 0; i < 360; i++) {
      double t = i / 360.0;
      p.addPoint((int) (centerX + radius * t * Math.cos(8 * t * Math.PI)),
          (int) (centerY + radius * t * Math.sin(8 * t * Math.PI)));
    }
    g.fillPolygon(p);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("FillPoly");
    frame.setSize(300, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(new FillPolyPanel());

    frame.show();
  }

}

           

untitled.GIF
untitled.GIF (2.16 KiB) Viewed 5267 times


Re: Fill in a shape

Mon Oct 27, 2008 2:56 am

Hi msi_333!

I'm only a novice at Java, but your code shows me the power of the language. Before I saw your post, I wimped out and did it the easy way...
Code:



   /**draw truck*/
   //draw tires
   g.fillOval(76,393,48,48);
   g.fillOval(238,397,44,48);
   
   //draw windshield
   g.drawArc(200,250,75,100,0,90);
   g.drawLine(275,325,274,300);
   g.drawLine(200,250,245,251);
   
   //draw truck body
   g.setColor(myGreen);
   int xs[]={40,40,200,200,225,225,320,325,
            285,265,255,240,120,105,95,80};
   int ys[]={400,325,325,250,250,325,325,
            400,400,390,390,400,400,390,390,400};
   int pts=xs.length;
   g.fillPolygon(xs,ys,pts);   
   
   



It's just a sort of introductory project for my JAVA 130 class. I just drew little lines instead of an arc.

Now, I just have to figure out how to setBackground() to another color.

Thank you for kind help. :)

-Joe

Post a reply
  Related Posts  to : Fill in a shape
 Draw Fill Rectangle     -  
 Fill DataGrid In Connected Mode     -  
 How to fill a ComboBox with data from a database in jsp     -  
 fill selection options from list     -  
 fill all Squares of game board - 2d Java Game Example     -