codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  i need help with a timer class to move line segments  alainap21 at 22:06 on Friday, February 10, 2006
 

im trying to design an application that displays an animation of a horizontal line segment moving across the screen from left to right, eventually passing across a vertical line.

im a little confused on which variables i should set up. ..right now my variables are..
private Timer timer;
private final int DELAY=20;
private int x,y,moveX,moveY;
//and this is where im confused....do i need variables for the horizontal and vertical lines? and do i need variables x1,y1,x2,y2 to draw my these line segments?

  Re: i need help with a timer class to move line segments  crwood at 01:06 on Saturday, February 11, 2006
 

It depands on how you want to do this. If the vertical line is fixed then you can leave it inside the paintComponent method.

protected void paintComponent(Graphics g)
...
g2.draw(new Line2D.Double(w/2, 0, w/2, h));

If you want to make it an instance variable you could.

Line2D vertLine;
...
public Constructor()
vertLine = new Line2D.Double(x1, y1, x2, y2);
...
protected void paintComponent(Graphics g)
...
g2.draw(vertLine);

For the horizontal line: if you use a Line2D then it will keep track of its endpoints (P1 and P2 and/or x1, x2, y1, y2, see api). You can access these, add the increment and use in the Line2D "setLine" method. If you would rather use the Graphics method drawLine then you will need to declare the x1, y1, x2, y2 variables as instance variables (class scope). And the amount of horizontal travel, say "xInc" is required. You could make this an instance variable of the JPanel class that draws the animation or, if you use an inner/nested or outer class for the timers ActionListener you could declare the increment as an instance variable within it.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  setSize and setTitle
•  web publishing problem
•  C++ and Passwording Files
•  Disabling/enabling the textbox of EditCommandColumn based on value
•  Re: Character Arrays
•  Subject: regarding making a setup using Package and Deployment Wizard
•  java script
•  Re: how to extract applet class ?
•  Wrong number of arguments or invalid property assignment


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


© Copyright codetoad.com 2001-2006