It’s more complicated then you can imagine. I am doing exactly what you saying. However i have 2 classes paddle and ball which have a draw(Graphics g) method which will draw in the class OnePlayerGame/TwoPlayerGame by getting the graphics contex of that frame. Also if you follow the instructions i gave above, you will be able to download the source code, Anyways heres the code for the one player game:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.applet.Applet;
import java.applet.AudioClip;
import javax.swing.JFrame;
import javax.swing.JPanel;
class OnePlayerGame extends Canvas implements WindowListener, Runnable{
//global variables for off-screen rendering
private Graphics dbg;
private Image dbImage = null;
// for the animation
private Thread animator;
//background bufferedImage object to hold the background image
private BufferedImage background_image;
//background AudioClip object to hold the gameengine's sound effect
private AudioClip sound_effect;
//boolian to hold wether the game is paused or not
private boolean ispaused = false;
//boolian to hold wether the game is running or not
private boolean running = false;
public final int screenWidth;
public final int screenHeight;
OnePlayerGame(int new_screenWidth, int new_screenHeight, String filename){
//create a frame
JFrame frame = new JFrame();
//create a panel
JPanel panel = (JPanel)frame.getContentPane();
//set the frames window title
frame.setTitle("Pong Master v1.0 - One Player Game");
//set the default close operation to close when the window is closed
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//get the current screens height
screenWidth = new_screenWidth;
//get the current screens height
screenHeight = new_screenHeight;
//set the game windows size and center it
frame.setBounds(0, 0, screenWidth, screenHeight);
//set the game panels size and position it
panel.setBounds(0, 0, screenWidth, screenHeight);
//load the background image
load_background_image(filename);
panel.setFocusable(true);
panel.requestFocus(); //now has focus, so recieves key events
panel.addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e)
{ processKey(e); }
});
//add the panel
panel.add(this);
//set the panel layout to null
panel.setLayout(null);
//show the frame
frame.setVisible(true);
}
Paddle player1 = new Paddle("Data/Graphics/bat1.png", 10, 10);
Paddle player2 = new Paddle("Data/Graphics/bat2.png", 20, 20);
Ball ball = new Ball("Data/Graphics/ball.png", 320, 240, 2, 2);
//*************************************************************************************
// Function add_Notify()
// wait for the panel to be added to the JFrame/Japplet before starting
//*************************************************************************************
public void add_Notify(){
super.addNotify();
startGame();
}
//*************************************************************************************
// Function draw()
// Draws the paddle to the current graphics contex
//*************************************************************************************
public void draw(Graphics g){
g.drawImage(background_image,0,0,this);
}
//*************************************************************************************
// Function gameOver_message()
// displays a gameover message
//*************************************************************************************
private void gameOver_Message(Graphics g){
//center the gameover message
//get the center x position
int x = screenWidth / 4;
int y = screenHeight / 4;
g.drawString("GameOver!", x, y);
}
//*************************************************************************************
// Function gameRender()
// renderss the game images
//*************************************************************************************
private void gameRender(){
//draw the current frame to an image buffer
if (dbImage == null){
//create the buffer
dbImage = createImage(screenWidth, screenHeight);
if(dbImage == null){
System.out.println("Could not create Screen Buffer");
return;
}
else
dbg = dbImage.getGraphics();
}
//clear the background
dbg.setColor(Color.white);
dbg.fillRect(0, 0, screenWidth, screenHeight);
// draw the game elements
// draw the background
draw(dbg);
// draw player 1's paddle
player1.draw(dbg);
// draw player 2's paddle
player2.draw(dbg);
// draw the ball
ball.draw(dbg);
}//end of gamerender
//*************************************************************************************
// Function gameUpdate()
// updates the game state
//*************************************************************************************
private void gameUpdate(){
}
//*************************************************************************************
// Function load_image()
// loads the paddle image into bufferedimage object paddle_image
//*************************************************************************************
public void load_background_image(String filename){
try{
background_image = ImageIO.read(getClass().getResource(filename));
}catch(IOException e){
System.out.println("Load Image error: Failed to Load" + filename);
}//end of catch block
}//end of try block
//*************************************************************************************
// Function paintComponent()
// Flips the Buffers
//*************************************************************************************
public void paint(Graphics g){
if(dbImage != null){
g.drawImage(dbImage, 0, 0, null);
}
}
//*************************************************************************************
// Function run()
// Runs the game
//*************************************************************************************
public void run(){
//repeatly update, render, sleep
running = true;
while(running){
gameUpdate();
gameRender();
repaint();
try {
Thread.sleep(20); //sleep a bit
}
catch(InterruptedException ex) {}
}
System.exit(0);
} //end of run
//*************************************************************************************
// Function startGame()
// initalize and start the thread
//*************************************************************************************
public void startGame(){
if (animator == null || !running) {
animator = new Thread(this);
animator.start();
}
} // end of startGame()
}
Plus i plan on converting to Active Rendering Soon.