I had 2 .java files and I decided it was too much stuff in two file .java so I made more class but now nothing is showing up :\
myFrame.java
import javax.swing.*; import java.awt.event.*; public class myFrame extends JPanel implements ActionListener,KeyListener { private static final long serialVersionUID = 1L; Timer t = new Timer(5, this); public myFrame() { t.start(); addKeyListener(this); setVisible(true); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }
arkanoidGame.java
import javax.swing.JFrame; public class arkanoidGame { public static void main(String[] args) { JFrame frame = new JFrame(); myFrame s = new myFrame(); Ball b = new Ball(); pad p = new pad(); frame.add(p); frame.add(b); frame.add(s); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,600); frame.setVisible(true); } }
ball.java
import javax.swing.*; import java.awt.*; public class Ball extends JPanel implements Runnable{ private static final long serialVersionUID = 1L; int ballX = 60; int ballY = 60; int ballWH = 25; public void init() { setBackground(Color.BLUE); } public void start() { // define a new thread Thread th = new Thread (this); // start this thread th.start (); } public void stop() { } public void destroy() { } public void paint (Graphics g) { super.paint(g); g.setColor(Color.blue); g.fillOval(ballX-ballWH,ballY-ballWH,2*ballWH,2*ballWH); } public void run() { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // run a long while (true) this means in our case "always" while (true) { ballX++; ballY++; repaint(); try { // Stop thread for 20 milliseconds Thread.sleep (20); } catch (InterruptedException ex) { // do nothing } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } }
pad.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.Rectangle2D; import javax.swing.JPanel; import javax.swing.Timer; public class pad extends JPanel implements KeyListener, ActionListener { Timer t = new Timer(5, this); double rectX = 15; double rectY = 540; int rectWidth = 80; int rectHeight = 20; double rectVeloX = 0.0; double rectVeloY = 0.0; private static final long serialVersionUID = 1L; public void paint(Graphics draw2D){ super.paint(draw2D); Graphics2D draw = (Graphics2D) draw2D; draw.setColor(Color.red); draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight)); draw.getClipBounds(); } public void actionPerformed (ActionEvent e) { repaint(); rectX += rectVeloX; rectY += rectVeloY; } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT ) { if ((rectX+rectWidth) >= 800) { rectVeloX = 0; rectX = 800-rectWidth; } else { rectVeloX = 1.5; rectVeloY = 0; } } if (key == KeyEvent.VK_LEFT ) { if (rectX <= 0) { rectVeloX = 0; rectX = 0; } else { rectVeloX = -1.5; rectVeloY = 0; } } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { rectVeloX = 0; rectVeloY = 0; } if (key == KeyEvent.VK_LEFT) { rectVeloX = 0; rectVeloY = 0; } } public void keyTyped(KeyEvent e) {} }