Posts which you will also like.

Thursday, January 13, 2011

A java program to print a shape using wired mesh

This program uses swing and Graphics class.All coding is straight forward and can be easily understood.You may need to create main method to use this.

import javax.swing.JPanel;
import java.awt.Graphics;
public class Drawpanel4 extends JPanel
{
    public void paintComponent(Graphics g)
    {
        int width=getWidth();
        int height=getHeight();
        int stepx=width/15;
        int stepy=height/15;
        super.paintComponent(g);
        int x=0,y=0,i=0,j=0,k=0,l=0;
        while(i<15)
        {   
            g.drawLine(0,y,x,height);
            y+=stepy;
            x+=stepx;
            i++;
       
        }
        x=0;y=height;   
        while(j<15)
        {
            g.drawLine(0,y,x,0);
            y-=stepy;
            x+=stepx;
            j++;
        }
        x=width;y=0;
        while(k<15)
        {
            g.drawLine(width,y,x,height);
            y+=stepy;
            x-=stepx;
            k++;
        }
        y=height;x=width;
        while(l<15)
        {
            g.drawLine(width,y,x,0);
            x-=stepx;
            y-=stepy;
            l++;
        }
    }
}



OUTPUT:


No comments:

Post a Comment

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool