Results 1 to 2 of 2
  1. #1
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default i am trying to drew a line using *

    i am trying to drewan line using "*" in the console . but i get only 2 "*". i think something is wrong with the line algorithm . maybe there is an easier way to do it.

    Java Code:
    class MyGraphicsApp {
    	MyGraphicsApp() {mg = new MyGraphics(80, 25);}
    
    	void paint(MyGraphics g) {
    		g.drawLine(5, 12, 27, 2);
    		g.drawLine(2, 2, 30, 30);
    	}
    
    	void repaint() {
    		paint(mg);
    		mg.display();
    	}
    
    	public static void main(String [] args) {
    		MyGraphicsApp myGraphicsApp = new MyGraphicsApp();
    		myGraphicsApp.repaint();
    	}
    
    	MyGraphics mg;
    }


    Java Code:
    class MyGraphics {
    int x, y;
    	private int width, height;
    	MyGraphics(int wid, int hit) {
    
    		fb= new FrameBuffer(wid,hit);
    		width = fb.getWidth();
    		height = fb.getHeight();
    	}
    	MyGraphics() {
    		fb = new FrameBuffer();
    		width = fb.getWidth();
    		height = fb.getHeight();
    	}
    
    void drawLine(int x1,int y1,int x2,int y2)
    {
      width= x2 - x ;
        height = y2 - y ;
        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
        if (width<0) dx1 = -1 ; else if (width>0) dx1 = 1 ;
        if (height<0) dy1 = -1 ; else if (height>0) dy1 = 1 ;
        if (width<0) dx2 = -1 ; else if (width>0) dx2 = 1 ;
        int longest = Math.abs(width) ;
        int shortest = Math.abs(height) ;
        if (!(longest>shortest)) {
            longest = Math.abs(height) ;
            shortest = Math.abs(width) ;
            if (height<0) dy2 = -1 ; else if (height>0) dy2 = 1 ;
            dx2 = 0 ;
        }
        int numerator = longest >> 1 ;
        for (int i=0;i<=longest;i++) {
    		fb.setPixel(x1, y1);
    
            numerator += shortest ;
            if (!(numerator<longest)) {
                numerator -= longest ;
                x += dx1 ;
                y += dy1 ;
            } else {
                x += dx2 ;
                y += dy2 ;
            }
        }
        return;
    }
    	void display() {
    		fb.display();
    		return;
    		} // simply calls the frame buffer's display method
    
    	FrameBuffer fb;
    }
    
    
    
    /*void drawLine(int x1,int y1,int x2,int y2)
    {
      width= x2 - x ;
        height = y2 - y ;
        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
        if (width<0) dx1 = -1 ; else if (width>0) dx1 = 1 ;
        if (height<0) dy1 = -1 ; else if (height>0) dy1 = 1 ;
        if (width<0) dx2 = -1 ; else if (width>0) dx2 = 1 ;
        int longest = Math.abs(width) ;
        int shortest = Math.abs(height) ;
        if (!(longest>shortest)) {
            longest = Math.abs(height) ;
            shortest = Math.abs(width) ;
            if (height<0) dy2 = -1 ; else if (height>0) dy2 = 1 ;
            dx2 = 0 ;
        }
        int numerator = longest >> 1 ;
        for (int i=0;i<=longest;i++) {
    		fb.setPixel(y, x);
    
            numerator += shortest ;
            if (!(numerator<longest)) {
                numerator -= longest ;
                x += dx1 ;
                y += dy1 ;
            } else {
                x += dx2 ;
                y += dy2 ;
            }
        }
        return;
    }*/


    Java Code:
    public class FrameBuffer {
    	FrameBuffer (int wid, int hit)
        {
          width = wid;
           height = hit;
            pixels = new boolean[height][width];
    
            for(int i = 0; i < height; i++){ // sets all positions in the array to false
                for(int j = 0; j < width; j++){
                    pixels[i][j] = false;
                }
            }
        }	// Creates a frame buffer of specified resolution
    	FrameBuffer () {
            height = 35;
            width = 60;
              pixels = new boolean[height][width];
    
            for(int i = 0; i < height; i++){ // sets all positions in the array to false
                for(int j = 0; j < width; j++) {
                    pixels[i][j] = false;
                }
            }
        }	 // Creates a default-sized frame buffer
    
     void setPixel(int x, int y, boolean b){
    		pixels[y][x] = b;
    	} //set pixels x,y to b
      void setPixel(int x, int y) {//position set as true
    
    		pixels[y][x] = true;
    	}	//set pixels x,y to true
    
        int getWidth() {
        	return width;
        	}
        int getHeight() {
        	return height;
        	}
    
        void clear() {
    
    		for(int i = 0; i < height; i++)  {    //resets all positions to false
    			for(int j = 0; j < width; j++){
    
    					pixels[i][j] = false;
    				}
    		}
        } // clears the frame buffer
    
        void display() {
    
        System.out.print("+");      // left hand upper corner position on frame
    
         for( int i = 0; i < width; i++){ // loops through the first line on frame
                System.out.print("-");
            }
         System.out.println("+"); // right hand upper corner on frame
    
         for(int i = 0; i < height; i++) {    // loops through the entire frame till the last line
                System.out.print("|");
            int j;
         for(j = 0; j < width; j++){ // loops though each row falling out at the last position
            if(pixels[i][j] == true) { // prints and asterisk if true
              System.out.print("*");
             }
            else{
            System.out.print(" ");// otherwise prints a space
            }
          }
      System.out.println("|"); // before it increments i it prints out a "|"
    
        }
    System.out.print("+");  // left lower corner position in the frame
    
        for(int i = 0; i < width; i++){// loops through the last line of the frame
                System.out.print("-");
            }
    
    		System.out.println("+");   //right lower corner of the frame
        }
    
        private boolean pixels[][];
        private int width, height;
        public static void main(String [] args) {
    
        	FrameBuffer fb = new FrameBuffer();
            int x = 0, y = 0;
    
            fb.display();
            while (x < fb.getWidth()) {
                while (x < fb.getWidth() && y < fb.getHeight()){
                    fb.setPixel(x++, y++);
    
                }
    
             y--;
    
               while (x < fb.getWidth() && y >= 0){
                    fb.setPixel(x++, y--);
    
               }
                if (y < 0)y = 0;
            }
    
            fb.display();
            fb.clear();
    
          fb.display();
        }
    }
    Last edited by Norm; 11-16-2011 at 04:46 PM. Reason: Moved post out of Applet section

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: i am trying to drew a line using *

    i think something is wrong with the line algorithm
    Have you tried debugging the code by printing out the values of variables that control how many chars are shown?

    What do you see when you execute your code? What is wrong with what it prints on the console?
    Last edited by Norm; 11-16-2011 at 04:53 PM.

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •