Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>..draw 2 lines </p> </blockquote> <p>That seems like the crux of the matter in this question. </p> <p>Keep a collection of lines in an expandable list (e.g. <code>ArrayList</code>) when clicking/dragging, add a new line to the list and call <code>repaint()</code>. In <code>paintComponent(Graphics)</code>, iterate the collection and draw each line.</p> <p>BTW - I am guessing you have not minimized and restored your window while testing this. Your lines (beautiful or ugly) would disappear!</p> <hr> <blockquote> <p>..they disappeared. What's the reason?</p> </blockquote> <p>The methods <code>paint()</code> and <code>paintComponent()</code> are called whenever the GUI needs to redraw. They might be invoked after another window appears in front of the app., then it is brought back to the front. Another time is after being restored from minimized. </p> <p>The options to retain the lines include:</p> <ul> <li>Store the locations of the line(s) and redraw all of them whenever asked (as described above). This can work for most purposes. Even if there are hundreds of lines, the GUI will redraw them in 'the blink of an eye'.</li> <li>Draw each line to a <code>BufferedImage</code> and put the image in (an <code>ImageIcon</code> in) a <code>JLabel</code>. This approach works well if the drawing area is of a fixed size &amp; nothing is ever removed, and can accommodate ..millions of lines, arcs, semi-transparent areas, smaller images, text.. Using an image as a rendering surface, you would no longer need the <code>ArrayList</code>, since all you do is add a new line to the image, and repaint the label to see the new line and all previous lines.</li> </ul> <hr> <blockquote> <p>..the line is not the straight line. </p> </blockquote> <p>That is because of the 'rendering hints' used when drawing the line. A screen made of aligned rows of pixels can only make vertical or horizontal lines perfectly. To give the 'illusion' of a straight &amp; continuous line at any other angle, requires a technique known as <a href="http://en.wikipedia.org/wiki/Dither" rel="nofollow">dithering</a>. Read the opening sections of <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html" rel="nofollow"><code>Graphics2D</code></a> for more explanation and description of the <code>RenderingHints</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload