The Warring States of NPF  

Go Back   The Warring States of NPF > Dead threads
User Name
Password
FAQ Members List Calendar Today's Posts Join Chat

 
View First Unread View First Unread   Click to unhide all tags.Click to hide all tags.  
Thread Tools Display Modes
Unread 04-19-2008, 08:53 PM   #1
Cati
Hoopy Frood
 
Cati's Avatar
 
Join Date: Jan 2008
Location: USA
Posts: 147
Cati will become famous soon enough. Eventually. Maybe.
Default 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
Cati is offline Add to Cati's Reputation  
Unread 04-19-2008, 09:42 PM   #2
Eltargrim
Fifty-Talents Haversham
 
Eltargrim's Avatar
 
Join Date: Mar 2006
Location: FABULOUS
Posts: 1,904
Eltargrim is a ray of sunshine lighting up your life. Eltargrim is a ray of sunshine lighting up your life. Eltargrim is a ray of sunshine lighting up your life. Eltargrim is a ray of sunshine lighting up your life.
Default

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);
    }
I'm afraid I don't have an example offhand, but basically what you have to do is increment the new rectangle while erasing the old one.

Code:
eraseThing(x,y);
drawThing(x+1,y+1);
Toss the proper method equivalents inside a for loop and you should be all right. I'm afraid I haven't done a whole lot of graphical stuff, but at least one of the aforementioned texts has a decent graphics and applet section.

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.
Eltargrim is offline Add to Eltargrim's Reputation  
Unread 04-20-2008, 12:42 PM   #3
Vault Of Thrones
A Threat to the District
 
Vault Of Thrones's Avatar
 
Join Date: Sep 2007
Location: /dev/null
Posts: 444
Vault Of Thrones will become famous soon enough. Eventually. Maybe.
Send a message via ICQ to Vault Of Thrones Send a message via AIM to Vault Of Thrones Send a message via MSN to Vault Of Thrones Send a message via Yahoo to Vault Of Thrones Send a message via Skype™ to Vault Of Thrones
Default

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);
This is an anonymous inner class that responds to an action. Every 50 milliseconds the timer throws an action which the ActionListener handles.
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!
Vault Of Thrones is offline Add to Vault Of Thrones's Reputation  
Unread 04-21-2008, 08:11 PM   #4
Cati
Hoopy Frood
 
Cati's Avatar
 
Join Date: Jan 2008
Location: USA
Posts: 147
Cati will become famous soon enough. Eventually. Maybe.
Default

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
Cati is offline Add to Cati's Reputation  
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:37 PM.
The server time is now 11:37:22 PM.


Powered by: vBulletin Version 3.8.5
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.