Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2007, 10:16 PM
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
Basic Graphic
Does anyone know where I could learn how to make a basic graphic with animations? I don't really understand the coordinates of the animations.

Last edited by jkswebsite : 11-24-2007 at 11:18 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-25-2007, 12:04 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnimationVariables extends JPanel implements Runnable { int x = 100; int y = 100; int s = 100; int dx = 3; int dy = 2; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.red); g2.fillRect(x, y, s, s); } public void run() { boolean runMore = true; while(runMore) { try { Thread.sleep(50); } catch(InterruptedException e) { runMore = false; } checkBoundries(); x += dx; y += dy; repaint(); } } private void checkBoundries() { if(x + dx < 0 || x + s + dx > getWidth()) dx *= -1; if(y + dy < 0 || y + s + dy > getHeight()) dy *= -1; } private void start() { while(!isVisible()) { try { Thread.sleep(25); } catch(InterruptedException e) { break; } } Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { AnimationVariables test = new AnimationVariables(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); test.start(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2007, 12:29 AM
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
awesome man thanks! okay now I want to make it a little more complicated with some user interaction. I know the basic code for the user moving the arrow keys left to right. So how can I make that template more "fun," or into a "game?"

thank you SO much for all your help, you are a life saver!

this is what I have in another java file for moving text from left to right with the arrow keys:

Quote:
// handles KeyEvents
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)// left arrow
xdir = -5;// move left
if (e.getKeyCode() == KeyEvent.VK_RIGHT)// right arrow
xdir = 1;// move right

if (e.getKeyChar() == ' ') {// space bar
beep.play();
xdir = 0;// stop moving
}
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

}
how would I import that to the red animation?
__________________
Jonathan - AIM: JKK088

Last edited by jkswebsite : 11-26-2007 at 12:42 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2007, 01:09 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
If you want to use keyboard input I would recommend keybinding which you can find out about here How to Use Key Bindings. You bind them to the JFrames JRootPane and (you may need to) add a call to setFocusable on your JPanel/graphic component.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2007, 01:53 AM
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
hmmm okay, I don't really understand the site or what you're saying

so I could just move the key events code into the red one?

Or I have a better idea.

On another file, I have a yellow oval, and I want to have text moving (left to right) saying move the eyes (attached under the text) to the center,

and while the user moves the eyes left or right, eventually to the center,

the eyes will change color and maybe a "YOU WIN!" could come up, if possible. I just want a simple "game" with some user interaction
__________________
Jonathan - AIM: JKK088
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-26-2007, 02:02 AM
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
or to keep the red box from hitting the walls, and if it does, maybe it turns black

is that possible?

with user interaction though
__________________
Jonathan - AIM: JKK088
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-26-2007, 03:19 AM
Member
 
Join Date: Nov 2007
Location: Illinois
Posts: 12
jkswebsite has a little shameless behaviour in the past
Send a message via AIM to jkswebsite
I GOT IT! I made an overlap game, I am going to go in early so I can make them turn white when they overlap, thanks for all your help!
__________________
Jonathan - AIM: JKK088
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change font/ font color etc in a graphic object using JCombobox? JavaInLove AWT / Swing 0 02-07-2008 12:28 PM
Graphic settings being overridden? sjchase New To Java 0 01-17-2008 12:27 AM
How do insert a Graphic carl New To Java 1 08-01-2007 06:30 AM
help with basic example fred New To Java 1 07-20-2007 06:45 PM
Swing Graphic interface Daniel AWT / Swing 2 06-28-2007 05:39 PM


All times are GMT +3. The time now is 08:39 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org