Original Post
Im trying to make a "Breakout" style game as my first dabble into games making, and, ill be honest, the key/mouselistener stuff is confusing me. and some of the graphics stuff. Anyhoo.. heres my code so far: Ok, my question is this: do i "implements keylistener" on the DrawIt class, and put the keylistener stuff in there? If so, where do i start the keylistener (i.e make it start listening) Lastly, where to i put repaint in all of this? (I understand i'd need a while loop while the game is running.. does this go around the the whole of the main class, or just inside the DrawIt Class?) any advice or help on any of this would be greatly appreciated! [Edited by - Esqulax on December 1, 2008 6:45:00 AM]
package app1;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class MainClass extends JFrame {
final int WIDE = 900, HIGH = 650;
int paddle_x = ((WIDE/2)-40);
//Class to create the GUI and Add stuff
public MainClass() {
this.setSize(WIDE, HIGH);
this.setTitle("Awesome");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.add(new DrawIt());
this.setVisible(true);
}
//Class to draw my paddle
public class DrawIt extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Shape s = new RoundRectangle2D.Float(paddle_x,HIGH-100,80,10,10,10);
g2.setColor(Color.BLACK);
g2.draw(s);
g2.setColor(Color.RED);
g2.fill(s);
}
}
public static void main(String[] args)
{
new MainClass();
}
}