1

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.

  • you are creating a `CG4` inside itself (`public void Call... cg4 = new CG4...`) - not sure, but that is very suspect (I didn't fully understand your problem) – user85421 Apr 29 '19 at 17:22
  • @CarlosHeuberger I am calling that function from another main class here this code excutes successfully in case of triangle and I think that problem is in while loop of calculate() but i'm not able to understand that. – Prasenjit Hiwale Apr 29 '19 at 17:38
  • but there is only one loop (kind of) in that code drawing lines between two *border lines* (in mathematical sense), no where is it using the coordinates of the polygon - this can not work. It will need different segments depending on Y (*range* 100-200, 200-300, 300-400) Also you should be aware that `paint` can be called more than once and it is not a good idea to have a `sleep` in it ((supposing you don't *want* to use the `fill(Shape)` and `draw(Shape)` methods that do fill and draw a `Polygon`)) – user85421 Apr 29 '19 at 19:59
  • I do not see anything that even slightly resembles filling a triangle/convex polygon (your scan line algorithm) ... for starters you should have encapsulated it into some function with points defining the shape as parameters ... I do not see anything but hard-coded lines ... then make the functionality ... so first make the left/right buffers feed them and render for debug and only once working as should fill the inside with scanlines ... see [how to rasterize rotated rectangle (in 2d by setpixel)](https://stackoverflow.com/a/19078088/2521214) – Spektre Apr 30 '19 at 07:30

0 Answers0