Total members 11893 |It is currently Wed Nov 06, 2024 7:01 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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();
  }

}

           

Attachment:
untitled.GIF
untitled.GIF [ 2.16 KiB | Viewed 5259 times ]


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time
Post new topic Reply to topic  [ 3 posts ] 

  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     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com