Results 1 to 4 of 4
  1. #1
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default graphic line problem

    ok so here's the deal, so i am making a program that draws lines in JPanel but every time i run it i draw multiple lines and maximize the window. when i do that the lines dissapear and there is only one line left. I'll show you the code.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class RubberLinesPanel extends JPanel
    {
    	private final int WIDTH=500, HEIGHT=500;
    	private Point point1= null, point2 = null;
    	private ArrayList pointList;
    	private ArrayList<Integer> PointsX =new ArrayList<Integer>();
    	private ArrayList<Integer> PointsY =new ArrayList<Integer>();
    	
    	
    
        public RubberLinesPanel() 
        {
        	pointList = new ArrayList();
        	LineListener listener = new LineListener();
        	addMouseListener (listener);
        	addMouseMotionListener (listener);
        	
        	setBackground (Color.blue);
        	setPreferredSize (new Dimension(WIDTH, HEIGHT));
        }
        //========================================================================
        public void paintComponent (Graphics page)
        {
        	Iterator pointIterator= pointList.iterator();
        //	super.paintComponent (page);
       	
        	while (pointIterator.hasNext())
       		{
        	page.setColor (Color.red);
        	Point drawPoint= (Point) pointIterator.next();
        	if (point1 != null && point2 !=null)
        		page.drawLine(point1.x,point1.y, point2.x, point2.y);
        	}	
        }
        //=======================================================================
        private class LineListener implements MouseListener,MouseMotionListener
        {
        	public void mousePressed (MouseEvent event)
        	{
        		pointList.add (event.getPoint());
        		point1 = event.getPoint();
        		
        	}	
        	public void mouseDragged (MouseEvent event)
        	{
        		pointList.add (event.getPoint());
        		point2 = event.getPoint();
     
        		repaint();
        	}	
        	public void mouseClicked (MouseEvent event) {}	
        	public void mouseReleased (MouseEvent event) {}
        	public void mouseEntered (MouseEvent event) {}
        	public void mouseExited (MouseEvent event) {}
        	public void mouseMoved (MouseEvent event) {}	
      	}	
        
        
    }



    Java Code:
    import javax.swing.*;
    public class RubberLines 
    {
    	public static void main(String args[]) 
        {
        	JFrame RubberLinesFrame = new JFrame ("Rubber");
        	RubberLinesFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        	
        	RubberLinesFrame.getContentPane().add (new RubberLinesPanel());
        	
        	RubberLinesFrame.pack();
        	RubberLinesFrame.show();
        }
        
        
    }
    please help if you can...

    thank you f_the_cook

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're not drawing the "drawPoint":

    Java Code:
        while (pointIterator.hasNext())
        {
          page.setColor(Color.red);
          Point drawPoint = (Point) pointIterator.next();
          //if (point1 != null && point2 != null)
            //page.drawLine(point1.x, point1.y, point2.x, point2.y);
          if (point1 != null && drawPoint != null)
          {
            page.drawLine(point1.x, point1.y, drawPoint.x, drawPoint.y);
          }
        }
    Or perhaps what you really want to do is this:
    Java Code:
      public void paintComponent(Graphics page)
      {
        Iterator pointIterator = pointList.iterator();
        super.paintComponent (page);
        
        Point prevPoint = null;
        while (pointIterator.hasNext())
        {
          page.setColor(Color.red);
          Point drawPoint = (Point) pointIterator.next();
          //if (point1 != null && point2 != null)
            //page.drawLine(point1.x, point1.y, point2.x, point2.y);
    //      if (point1 != null && drawPoint != null)
    //      {
    //        page.drawLine(point1.x, point1.y, drawPoint.x, drawPoint.y);
    //      }
          if (prevPoint != null && drawPoint != null)
          {
            page.drawLine(prevPoint.x, prevPoint.y, drawPoint.x, drawPoint.y);
          }
          prevPoint = drawPoint;
        }
      }
    Last edited by Fubarable; 11-25-2008 at 08:12 PM.

  3. #3
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    thank you so much! i just have one last question. Does anyone know how i would do an erase function of some sort.

    thank you for your help.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    erase is done by drawing over the area and repainting.(well animating is but you have to erase to animate)

Similar Threads

  1. Graphic library for XML Schema
    By earns in forum XML
    Replies: 0
    Last Post: 10-26-2008, 05:49 PM
  2. Graphic library
    By xeannart in forum Advanced Java
    Replies: 6
    Last Post: 07-14-2008, 03:36 AM
  3. [SOLVED] Text Area new line problem
    By Jay-1.1 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 05:18 AM
  4. Basic Graphic
    By jkswebsite in forum Java 2D
    Replies: 6
    Last Post: 11-26-2007, 03:19 AM
  5. How do insert a Graphic
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 06:30 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
  •