Prasenjit Hiwale
Prasenjit Hiwale

Reputation: 33

How can I get the whole polygon colored from this code?

Following is code of computer graphics concept Scanline algorithm but instead of polygon the other plane is getting colored how can I fix it?

import javax.swing.*;
import java.awt.*;

public class CG4 extends JFrame {
    int m1 = -1,m2 = 0,m3 = 2;
    int ymax=400,ymin=100;
    int ytemp=ymin+1;

    public void Call(){

        CG4 cg4 = new CG4();
        cg4.setTitle("ScanLine");
        cg4.setSize(1000,1200);
        cg4.setVisible(true);
        cg4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void paint(Graphics g){
        Calculate(g);
    }

    public void Calculate(Graphics g){

        g.drawLine(250,100,200,200);
        g.drawLine(200,200,200,300);
        g.drawLine(200,300,250,400);
        g.drawLine(250,400,300,300);
        g.drawLine(300,300,300,200);
        g.drawLine(300,200,250,100);


        while(ytemp<ymax){
            int x1,x2;

            x1=(ytemp-400)/m1;
            x2=(ytemp+600)/m3;
            g.setColor(Color.green);
            g.drawLine(x1+1, ytemp, x2-1, ytemp);
            ytemp++;

            try{
                Thread.sleep(10);
            }
            catch(InterruptedException ex){
                Thread.currentThread().interrupt();
            }
        }
    }
}

I expect colored polygon instead of extra plane. It is getting colored but in triangle format that I don't want so, please anyone can help in fixing this code.

I think error may be in while loop.

Upvotes: 1

Views: 59

Answers (0)

Related Questions