04-19-2008, 08:53 PM | #1 |
Hoopy Frood
Join Date: Jan 2008
Location: USA
Posts: 147
|
Java help?
Hi, I'm a moron.
For my term project in my networking class, I'm writing a Java applet. However, I don't know Java. :P In general, I know how to write the program -- I know C++, and I've gotten pretty far by writing Java by using C++ and then looking up the proper syntax when errors pop up. I've also been looking at some Java applet source codes online and have managed to piece together more of it. My main problem is the repaint() function. For example, I need to make different pictures and shapes go across the applet space in different directions and yadda. Logically, I know how to do it (using a variable for its position, using a loop to add to the variable until it reaches a certain number, et cetera), but I can't seem to get the applet to refresh properly to show the animation so to speak, if you know what I mean? Essentially, if anybody can find or write a simple Java applet of a rectangle (or any other shape) moving across the applet space, that would be great and I'd love you forever. It doesn't have to move at any certain speed, any certain direction, et cetera -- just as something as simple as possible so I can piece together what the heck I should be doing. Since I'm being an idiot and not learning Java properly (two weeks left to write this thing!), examples work better than explanations, since they tend to refer to concepts and/or syntax I've never heard. But anything anybody is willing to offer, I'll appreciate! Thanks!
__________________
- Cati |
04-19-2008, 09:42 PM | #2 |
Fifty-Talents Haversham
Join Date: Mar 2006
Location: FABULOUS
Posts: 1,904
|
I can wing you a couple of digitized textbooks I have lying around. In addition, here's a snippet of code from the jdk Demo subdirectory.
Code:
public void update(Graphics g) { Dimension newSize = getSize(); if (size.equals(newSize)) { // Erase old box g.setColor(getBackground()); g.drawRect(mx, my, (size.width / 10) - 1, (size.height / 10) - 1); } else { size = newSize; g.clearRect(0, 0, size.width, size.height); } // Calculate new position mx = (int) (Math.random() * 1000) % (size.width - (size.width / 10)); my = (int) (Math.random() * 1000) % (size.height - (size.height / 10)); paint(g); } Code:
eraseThing(x,y); drawThing(x+1,y+1); If you want the two texts, PM me with your e-mail and I'll RAR them up for you. Otherwise, that's the breadth of my graphical Java knowledge. EDIT: Looking more at repaint(), I'll try and whip something up. EDIT2: This might help you. It's going to take me a while, I'm not familiar at all with painting. EDIT3: This looks like it will really help you.
__________________
<Insert witticism here; get credit; ???; profit!> Last edited by Eltargrim; 04-19-2008 at 09:54 PM. |
04-20-2008, 12:42 PM | #3 |
A Threat to the District
|
You are going to have to use a timer and events to get it to work properly. What you want to do is create a timer and have it throw an event at whatever millisecond interval you want.
Here is the code I used in a pong game I made: Code:
ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent evt) { if (paddle1 != null) { ball.updateForNextFrame(); paddle1.updateForNextFrame(); paddle2.updateForNextFrame(); } repaint(); } }; timer = new Timer(50, action); In the ball and paddle classes I wrote a method called updateForNextFrame which just does all the calculations for where the ball and paddles need to be. You can look find the full source here.
__________________
Twitter! |
04-21-2008, 08:11 PM | #4 |
Hoopy Frood
Join Date: Jan 2008
Location: USA
Posts: 147
|
Thanks so much guys, I messed around using your examples and links and I finally got it working, phew! Now that I have the base, I can get somewhere, yay!
__________________
- Cati |
|
|