All I want to do is draw a few lines... :-(

M

Matt Melchert

Okay. I have an application where I'm attempting to draw a shape
consisting solely of horizontal and vertical lines. It's not the kind
of thing you can draw once and forget; it will need to be updated from
time to time.

How can you draw a few lines, then a little later draw a few more
without losing the first lot? I've tried a lot of things, but they
all either don't work at all or I only get the last line drawn!

Please help. I'm awash here.

MTIA,

Matt
 
V

VisionSet

Matt Melchert said:
Okay. I have an application where I'm attempting to draw a shape
consisting solely of horizontal and vertical lines. It's not the kind
of thing you can draw once and forget; it will need to be updated from
time to time.

How can you draw a few lines, then a little later draw a few more
without losing the first lot? I've tried a lot of things, but they
all either don't work at all or I only get the last line drawn!

1st thing to be aware of is that the code you write *is* responsible for
taking into account obscuring windows, minimising etc.
That is to say that the paint method of your component is responsible for
restoring your component to its required state after one of these events.
So your paint method should draw everything in one go that is req'd at that
time.
 
M

Marco Schmidt

Matt Melchert:
Okay. I have an application where I'm attempting to draw a shape
consisting solely of horizontal and vertical lines. It's not the kind
of thing you can draw once and forget; it will need to be updated from
time to time.

How can you draw a few lines, then a little later draw a few more
without losing the first lot? I've tried a lot of things, but they
all either don't work at all or I only get the last line drawn!

Your GUI application must be able to draw itself completely when
requested. So just store all line coordinates in some data structure
and access that inside of your paint method. If you have to add or
delete lines, modify that data structure and force a redraw.

Regards,
Marco
 
H

Harald Hein

Matt Melchert said:
Okay. I have an application where I'm attempting to draw a shape
consisting solely of horizontal and vertical lines.

Sound's like homework. Why do you post via Google? Does the Waikato
Institute of Technology not run an own news server?
It's not the
kind of thing you can draw once and forget; it will need to be
updated from time to time.

How can you draw a few lines, then a little later draw a few more
without losing the first lot?

By "remembering" (storing) all your line and shape data in some data
structure, and by implementing a proper paint() method in AWT or
paintComponent() in Swing. That method loops / iterates through your
data and paints the lines and shapes.
Please help. I'm awash here.

Read the Sun tutorial on 2D Graphics (part of the Swing UI tutorial
track) on the Sun Java web site.

Read the Swing architecture papers at the TSC archive on the Sun Java
web site.

Write your own code and show us the code next time you ask a question.

Do not post homework (Hint: Your teachers can google and can read
newsgroups). If they don't, we might inform them :)
 
F

Fred L. Kleinschmidt

Matt said:
Okay. I have an application where I'm attempting to draw a shape
consisting solely of horizontal and vertical lines. It's not the kind
of thing you can draw once and forget; it will need to be updated from
time to time.

How can you draw a few lines, then a little later draw a few more
without losing the first lot? I've tried a lot of things, but they
all either don't work at all or I only get the last line drawn!

Please help. I'm awash here.

MTIA,

Matt

Use douyble-buffering - create an offscreen image and do all of the
drawing in that. Then your paint method just copies that image to the
screen whenever a repaint is needed.
 
M

Matt Melchert

Fred L. Kleinschmidt said:
Use douyble-buffering - create an offscreen image and do all of the
drawing in that. Then your paint method just copies that image to the
screen whenever a repaint is needed.

Mmmmmm.........okay.

Any clues as to how to begin?

BTW, I have done this in C++, but the Java graphics environment is way different.

Thanks for any help you can give.

Matt
 
C

Chris Smith

VisionSet said:
The Java tutorial ?

Double buffering is not essential, but if you are animating it is neccessary
to produce nice results.

http://java.sun.com/docs/books/tutorial/uiswing/painting/overview.html

In this case, double buffering was suggested as a way to store past
results persistently to get correct behavior, NOT as a means of reducing
flicker. So yes, it or another solution to the problem is required.
Which I'd do depends on whether there's any meaning attached to the
lines; if so, I'd probably store the screen state in a more meaningful
way.

The URL Mike gave would be a good place to start in either case.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top