Anexei a este e-mail dois exemplos com Graphics.
Ignorem as várias classes colocadas num único arquivo, tentem organizar aí
no Eclipse :D
Podem ser úteis pra vocês... Senão fica como curiosidade.

-- 
Vitor Carneiro Maia
www.dcc.ufrj.br/~vitormaia <http://www.dcc.ufrj.br/%7Evitormaia>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Comp 
2 - Geral" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/comp2-geral?hl=en
-~----------~----~----~----~------~----~------~--~---

/**
 * Vitor Maia
 * http://www.dcc.ufrj.br/~vitormaia
 * @version 30-10-2008
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

//--------------------------------------------------------
class Constants {
	
	public static final int appSize = 300;
	public static final int ballSize = 30;
	public static final int ballSpeed = 2;
	
}
//--------------------------------------------------------
public class BallMain {
	
	public static void main(String[] args) {
		
		BallFrame ball = new BallFrame();
		ball.setVisible(true);
		
	}
	
}
//--------------------------------------------------------
class BallFrame extends JFrame implements KeyListener {

	
	static final long serialVersionUID = 42L;
	BallPanel panel = new BallPanel();
	
	public BallFrame() {
		
		super("Bola");
		this.add(panel);
		this.setResizable(false);
		this.setBounds(300,200,Constants.appSize + 8,Constants.appSize + 38);
		this.addKeyListener(this);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
	
	public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}
	public void keyPressed(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			
			if(panel.ball.getX() < Constants.appSize - Constants.ballSize){
				panel.ball.setX(panel.ball.getX() + Constants.ballSpeed);
				panel.repaint();
			}
			
		}
		
		if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			
			if(panel.ball.getX() > 0){
				panel.ball.setX(panel.ball.getX() - Constants.ballSpeed);
				panel.repaint();
			}
			
		}
		
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			
			if(panel.ball.getY() < Constants.appSize - Constants.ballSize){
				panel.ball.setY(panel.ball.getY() + Constants.ballSpeed);
				panel.repaint();
			}
			
		}
		
		if(e.getKeyCode() == KeyEvent.VK_UP) {
			
			if(panel.ball.getY() > 0){
				panel.ball.setY(panel.ball.getY() - Constants.ballSpeed);
				panel.repaint();
			}
			
		}
		
	}
	
}
//--------------------------------------------------------


//--------------------------------------------------------
class BallPanel extends JPanel {
	
	static final long serialVersionUID = 42L;
	Ball ball = new Ball();
	
	public BallPanel() {
		this.setBackground(new Color(150,150,150));
	}
	
	public void paint(Graphics g) {
		
		super.paint(g);
		g.fillOval(ball.getX(), ball.getY(), ball.getSize(),ball.getSize());
		
	}
	
}
//--------------------------------------------------------

//--------------------------------------------------------
class Ball {
	
	private int positionX;
	private int positionY;
	private int size;
	
	public Ball() {
		positionX = Constants.appSize/2 - Constants.ballSize/2;
		positionY = Constants.appSize/2 - Constants.ballSize/2;
		size = Constants.ballSize;
	}
	
	public int getX() {
		return positionX;
	}
	
	public int getY() {
		return positionY;
	}
	
	public int getSize() {
		return size;
	}
	
	public void setX(int positionX) {
		this.positionX = positionX;
	}
	
	public void setY(int positionY) {
		this.positionY = positionY;
	}
	
	public void setSize(int size) {
		this.size = size;
	}
	
}
//--------------------------------------------------------

Attachment: yoshi.rar
Description: application/rar

Responder a