Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bresenham Line | Boundary Fill help

Options
  • 05-03-2009 5:01pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    I'm trying to create a small applet that just lets someone draw a line using bresenhams algorithim and create a box out of it and when they then click in the box the box will be filled.

    I have a good bit of code and it compiles and runs and allows the drawing of lines but only from left to right, top to bottom and top left corner to bottom right corner.

    It wont let me draw a line right to left ect...

    Also my fill method doesn't work properly either, it just leaves a dot and doesn't fill the area at all

    here is the complete code:
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    
    public class BoundryFill extends JApplet implements MouseListener
    {
    
    	private static final long serialVersionUID = 1L;
    	int tempX, tempY;
    	public BufferedImage bImg;
    	int[] draw = new int[2];
    	public Graphics g;
    	public DisplayPanel myPanel;
    	private JPanel m_controls;
    	private Label m_functions;
    	private Choice m_shapeChoice;
    	private JButton m_clear;
    	Color color = null;
    	
    	public BoundryFill()
    	{
    		m_controls = new JPanel();
    		m_clear = new JButton("Clear");
    		
    		addMouseListener(this);
    	}
    	
        public static void main(String[] args)
        {
    
        }
    
    	public void init()
    	{
    		myPanel = new DisplayPanel();
    		bImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    		setContentPane(myPanel);
    		
    		
            m_controls.add(m_clear);
    	}
    	
    	void drawLine(int x1,int y1,int x2,int y2)
    	{
    		
    
    		//Uses Bresenham
    		
    		//System.out.println("Bresenham: Start("+x1+","+y1+") End("+x2+","+y2+")");
    		int dx, dy, incr1, incr2, xIncr, yIncr, d, x, y, colour;
    
    		colour = (Color.red).getRGB();
    			
    		//Distance between x and y values
    		dx = x2 - x1;
    		dy = y2 - y1;
    		
    		//System.out.println("Distance x="+dx);
    		//System.out.println("Distance y="+dy);
    		
    		if(dx > dy)
    		{
    			//System.out.println("dx > dy");
    			
    			d = 2 * dy - dx;
    			incr1 = 2 * dy;
    			incr2 = 2 * (dy - dx);
    		 
    		      if(x1 > x2) // switch end-points
    		      {
    		    	  //SwapX(x1,x2);
    		    	  tempX = x1;
    		    	  x1=x2;
    		    	  x2=tempX;
    		    	  
    		    	  //SwapY(y1,y2);
    		    	  tempY = y1;
    		    	  y1=y2;
    		    	  y2=tempY;
    		      }
    		 
    		      if(y1 < y2) // either slope up or down
    		      {
    		    	  yIncr = 1;
    		      }
    		      else
    		      {
    		    	  yIncr = -1;
    		      }
    		 
    		      y = y1;
    		 
    		      for(x = x1;x <= x2;x++)
    		      {
    		    	  WritePixel(x,y,colour);
    		    	  if(d < 0)
    		    {
    		    		  d += incr1;
    		    }
    		    	  else
    		    	  {
    		    		  y += yIncr;
    		    		  d += incr2;
    		    	  }
    		      }
    		     }
    			else
    			{
    			
    			//System.out.println("dy > dx");
    				
    			 d = 2 * dx - dy;
    			 incr1 = 2 * dx;
    			 incr2 = 2 * (dx - dy);
    		      
    			 if(y1 > y2) // switch end-points
    		     {
    		    	  //SwapX(x1,x2);
    		    	  tempX = x1;
    		    	  x1=x2;
    		    	  x2=tempX;
    		    	  
    		    	  //SwapY(y1,y2);
    		    	  tempY = y1;
    		    	  y1=y2;
    		    	  y2=tempY;
    		      }
    		      if(x1 < x2) // either slope up or down
    		      {
    		    	  xIncr = 1;
    		      }
    		      else
    		      {
    		    	  xIncr = -1;
    		      }
    		 
    		      x = x1;
    		 
    		      for(y = y1;y <= y2;y++)
    		      {
    		         WritePixel(x,y,colour);
    		         
    		         if(d < 0)
    		         {
    		            d += incr1;
    		         }
    		         else
    		         {
    		            x += xIncr;
    		            d += incr2;
    		         }
    		      }
    		   }
    		}
    			
    	void WritePixel(int x, int y,int colour)
    	{
    		//System.out.println("WritePixel("+x+","+y+")");
            bImg.setRGB(x, y, colour);
    	}
    	
    	int ReadPixel(int x, int y)
    	{
    		//System.out.println("ReadPixel("+x+","+y+")");
    		return bImg.getRGB(x,y);
    	}
    	
    	void Fill(int x,int y,int newColour,int boundaryColour)
    	{
    		
    		//System.out.println("Fill("+x+","+y+")");
    		
    		while(x>=0 && y>=0)
    		{
    		// condition to end recursion
    		if((ReadPixel(x, y) == newColour || ReadPixel(x, y) == boundaryColour))
    		{
    			return;
    		} 
    
    		// Step 2a)
    		int startX = x;
    		int endX = startX;
    		
    		// Step 2b)
    		WritePixel(startX,y,newColour);
    
    		// go to the left most pixel in the run
    		while(ReadPixel(startX - 1, y) != newColour && ReadPixel(startX - 1, y) != boundaryColour)
    		{
    			startX--;
    			WritePixel(startX,y,newColour);
    		}
    
    		// fill in the run from left to right
    		while(ReadPixel(endX + 1, y) != newColour && ReadPixel(endX + 1, y) != boundaryColour)
    		{
    			endX++;
    			WritePixel(endX, y,newColour);
    		}
    
    		   
    		x = startX;
    		   
    		while(x < endX)
    		{
    			while((ReadPixel(x, y - 1) == newColour || ReadPixel(x, y - 1) == boundaryColour) && (x < endX))
    			{
    				x++;
    			}
    
    			// either the start of a run, or the end of    the row has been reached
    			if(x < endX)
    			{
    				// start of a run, so fill it
    				Fill(x, y - 1, newColour, boundaryColour);
    				//WritePixel(x,y-1,bImg,newColour);
    				x++;
    			}
    		}
    
    		// Step 2d)
    		x = startX;
    		   
    		while(x < endX)
    		{
    			while((ReadPixel(x, y + 1) == newColour || ReadPixel(x, y + 1) == boundaryColour) && (x < endX))
    			{
    				x++;
    			}
    
    			// either the start of a run, or the end of the row has been reached
    			if(x < endX)
    			{
    				// start of a run, so fill it
    				Fill(x, y + 1, newColour, boundaryColour);
    				//WritePixel(x, y, bImg, newColour);
    				x++;
    			}
    		}
    		}
    	}		
    
    	public class DisplayPanel extends JPanel
    	{
    		private static final long serialVersionUID = 1L;
    		
    		public DisplayPanel()
    		{
    			setLayout(new BorderLayout());
    			this.setPreferredSize(new Dimension(500,500));
    			this.add(m_controls,BorderLayout.SOUTH);
    		}
    
    		
    		protected void paintComponent(Graphics g)
    		{
    	         super.paintComponent(g);
    	         color = g.getColor();
    	        
    	         g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);
    	      }
    	}
    	
    	public void mouseClicked(MouseEvent e) 
    	{
    		System.out.println("Clicked");
    		
    		int x = e.getX();
    		int y = e.getY();
    		
    		System.out.println("("+x+","+y+")");
    		
    		Fill(x,y,(color).getRGB(),(Color.red).getRGB());
    		
    		System.out.println("Filled");
    		
    		
    		System.out.println("Painted");
    	}
    
    	public void mouseEntered(MouseEvent e) 
    	{
    		System.out.println("Entered screen");
    	}
    
    	public void mouseExited(MouseEvent e) 
    	{
    		System.out.println("Exited screen");
    	}
    
    	public void mousePressed(MouseEvent e) 
    	{
    		System.out.println("Pressed mouse");
    		
    		draw[0] = e.getX();
    		draw[1] = e.getY();		
    	}
    
    	public void mouseReleased(MouseEvent e)
    	{
    		System.out.println("Released mouse");
    		int x1,y1,x2,y2;
    		
    		x1 = draw[0];
    		y1 = draw[1];
    		x2 = e.getX();
    		y2 = e.getY();
    		
    		drawLine(x1,y1,x2,y2);
    		g = this.getGraphics();
    		myPanel.paintComponent(g);
    		
    		//Clear Array
    		draw = new int[2];
    	}
    }
    

    Any help with spotting errors would be greatly appreciated, computer graphics isn't my strong point :o


Comments

  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Got it working, can I just ask what would be the best way to simulate the line so when I click and drag the line it shows the line and I can move it but it doesn't draw the line until I release the button?


Advertisement