Eclipse is a compiler, go ahead and get it from http://www.eclipse.org/ I use it, it’s definitely the best free compiler out there.
Here’s my source code:
import java.awt.Graphics;
import java.awt.Color;
import java.util.ArrayList;
public class Block extends Collidable implements Drawable
{
// Variables
Color color;
Formation parent;
// Constructor
public Block(int x, int y, int w, int h, Color c, Formation p)
{
super(x,y,w,h);
color = c;
parent = p;
}
// Mutator Methods
public void draw(Graphics g)
{
// Base Block
g.setColor(color);
g.fillRect(x,y,width,height);
// Surrounding black lines
g.setColor(Color.BLACK);
g.drawLine(x,y,x+width,y);
g.drawLine(x,y,x,y+height);
g.drawLine(x+width,y,x+width,y+height);
g.drawLine(x+width,y+height,x,y+height);
// Darkest shading on the bottom right
g.setColor(color.darker().darker().darker());
g.drawLine(x+width-1,y+1,x+width-1,y+height-1);
g.drawLine(x+width-1,y+height-1,x+1,y+height-1);
// Lightest shading on the top left
g.setColor(color.brighter().brighter().brighter());
g.drawLine(x+1,y+1,x+width-1,y+1);
g.drawLine(x+1,y+1,x+1,y+height-1);
// Darker shading on the bottom right
g.setColor(color.darker().darker());
g.drawLine(x+width-2,y+3,x+width-2,y+height-2);
g.drawLine(x+width-2,y+height-2,x+3,y+height-2);
// Lighter shading on the top left
g.setColor(color.brighter().brighter());
g.drawLine(x+2,y+2,x+width-3,y+2);
g.drawLine(x+2,y+2,x+2,y+height-3);
// Dark shading on the bottom right
g.setColor(color.darker());
g.drawLine(x+width-3,y+5,x+width-3,y+height-3);
g.drawLine(x+width-3,y+height-3,x+5,y+height-3);
// Light shading on the top left
g.setColor(color.brighter());
g.drawLine(x+3,y+3,x+width-5,y+3);
g.drawLine(x+3,y+3,x+3,y+height-5);
}
public void remove()
{
parent.removeBlock(this);
}
// Accessor Methods
public Formation parent()
{
return parent;
}
public Color color()
{
return color;
}
}
import java.util.ArrayList;
public abstract class Collidable
{
protected int x;
protected int y;
protected int width;
protected int height;
public Collidable()
{
x = y = width = height = 0;
}
public Collidable(int newX, int newY, int newWidth, int newHeight)
{
x = newX;
y = newY;
width = newWidth;
height = newHeight;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int width()
{
return width;
}
public int height()
{
return height;
}
public void move(int newX, int newY)
{
x = newX;
y = newY;
}
public ArrayList getContents()
{
ArrayList returnValue = new ArrayList();
returnValue.add(this);
return returnValue;
}
}
import java.awt.Graphics;
public interface Drawable
{
public void draw(Graphics g);
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Driver extends JFrame
{
private JMenuBar menuBar;
private Timer timer = new Timer(15,null);
private ArrayList panels;
private ArrayList scoreboards;
private ArrayList buttonsPressed;
private GridBagLayout gbl;
private GridBagConstraints gbc;
private JRadioButtonMenuItem p1,p2,p3,p4;
private SoundPlayer soundPlayer;
public static void main(String[] args)
{
Driver tpo = new Driver();
tpo.setSize(600,800);
tpo.makeMenu();
tpo.setVisible(true);
tpo.runWaitLoop();
tpo.start();
}
private void runWaitLoop()
{
while (!p1.isSelected() && !p2.isSelected() && !p3.isSelected() && !p4.isSelected())
{
//keep doing nothing until they select a number of players
}
makePanels();
}
private void makeMenu()
{
menuBar = new JMenuBar();
JMenu menu = new JMenu("Choose the number of players");
menuBar.add(menu);
ButtonGroup group = new ButtonGroup();
p1 = new JRadioButtonMenuItem("One Player");
group.add(p1);
p2 = new JRadioButtonMenuItem("Two Players");
group.add(p1);
p3 = new JRadioButtonMenuItem("Three Players");
group.add(p1);
p4 = new JRadioButtonMenuItem("Four Players");
group.add(p1);
menu.add(p1);
menu.add(p2);
menu.add(p3);
menu.add(p4);
setJMenuBar(menuBar);
menuBar.addKeyListener(new PanelKeyListener());
setTitle("Party Tetris - by Eli Delventhal, Edwin Chen, and Joseph Park");
setIconImage(Toolkit.getDefaultToolkit().getImage("tetris.gif"));
}
private void makePanels()
{
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
getContentPane().setLayout(gbl);
gbc.fill = GridBagConstraints.BOTH;
int numPlayers = 0;
buttonsPressed = new ArrayList();
panels = new ArrayList();
scoreboards = new ArrayList();
if (p1.isSelected())
numPlayers = 1;
if (p2.isSelected())
numPlayers = 2;
if (p3.isSelected())
numPlayers = 3;
if (p4.isSelected())
numPlayers = 4;
for (int i = 0; i < numPlayers; i++)
{
int p = i; if (p > 1) p -= 2;
Panel panel = new Panel(this, i+1);
panels.add(panel);
addComponent(panel,p*4+1,i/2+1,2,1,10.0,10.0);
Scoreboard scoreboard = new Scoreboard(panel, timer, panel.getPlayer());
scoreboards.add(scoreboard);
addComponent(scoreboard,p*4+3,i/2+1,1,1,5.0 ,5.0);
panel.setScoreboard(scoreboard);
new Thread(panel).start();
}
if (numPlayers == 3)
{
DummyPanel filler = new DummyPanel();
addComponent(filler, 5, 2, 3, 1, 10.0,10.0);
filler.draw();
}
setSize(480 * ((numPlayers+2)/2), 670 * ((numPlayers+1)/2));
addKeyListener(new PanelKeyListener());
timer.addActionListener(new TetrisListener());
soundPlayer = new SoundPlayer();
soundPlayer.play(SoundPlayer.BKGD_SND, true);
setResizable(false);
}
private void addComponent(Component c, int x, int y, int w, int h, double wide, double tall)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = wide;
gbc.weighty = tall;
getContentPane().add(c);
gbl.setConstraints(c, gbc);
}
private class MyWindowAdapter extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
public void start()
{
for (int i = 0; i < panels.size(); i++)
{
((Panel)panels.get(i)).startGrid();
((Scoreboard)scoreboards.get(i)).newNextItem();
}
((Panel)panels.get(0)).startGrid();
timer.start();
}
public void stop()
{
timer.stop ();
}
public Player[] getPlayers()
{
Player[] players = new Player[panels.size()];
for (int i = 0; i < panels.size(); i++)
players[i] = ((Panel)panels.get(i)).getPlayer();
return players;
}
public ArrayList getPanels()
{
return panels;
}
public ArrayList buttonsPressed()
{
return buttonsPressed;
}
private class PanelKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent k)
{
if (!buttonsPressed.contains(new Integer(k.getKeyCode())))
buttonsPressed.add(new Integer(k.getKeyCode()));
}
public void keyReleased(KeyEvent k)
{
if (buttonsPressed.contains(new Integer(k.getKeyCode())))
buttonsPressed.remove(new Integer(k.getKeyCode()));
for (int i = 0; i < panels.size(); i++)
if (k.getKeyCode() == ((Panel)panels.get(i)).getPlayer().rotateButton())
((Panel)panels.get(i)).setDidRotate(false);
}
}
public boolean isFocusable()
{
return true;
}
public class TetrisListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
boolean allLost = true;
for (int i = 0; i < panels.size(); i++)
{
if (!((Panel)panels.get(i)).getPlayer().lostGame())
allLost = false;
}
if (allLost)
{
soundPlayer.stop(SoundPlayer.BKGD_SND);
timer.stop();
}
}
}
}
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
public class DummyPanel extends JPanel
{
public DummyPanel()
{
setBackground(new Color(34867 * 12));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawLogo(g);
drawBorders(g);
}
private void drawLogo(Graphics g)
{
Color color = Color.RED;
for (int i = 0; i < 8; i++)
color = color.darker();
for (int i = 0; i < 10; i++)
{
Font font = new Font("Arial", Font.BOLD, 80);
g.setFont(font);
g.setColor(color);
g.drawString("PARTY", 10 + i, 100 + i);
g.drawString("TETRIS", 100 + i, 200 + i);
color = color.brighter();
}
Font font = new Font("Arial", Font.BOLD, 30);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString("by Eli Delventhal", 10, 350);
g.drawString("Edwin Chen", 60, 400);
g.drawString("Joseph Park", 110, 450);
}
private void drawBorders(Graphics g)
{
Color color = Color.BLACK;
for (int i = 0; i < 5; i++)
{
color = color.brighter().brighter();
g.setColor(color);
g.drawRect(i,i,getWidth()-i,getHeight()-i);
}
}
public void draw()
{
repaint();
}
}