Wednesday, April 6, 2011

Bricks (Java GUI)


I created a simple java program called "Bricks."

When you execute it, you can see a big window that has 100 grids in it. You can select one of them by mouse-clicking. When you double-click it, a small window called "Editor" pops up.

The Editor window, like the big one, has 100 grids in it. You can change their colors by clicking each grid.
As the Editor window changes, the selected portion of the big window changes also.



This is the diagram of classes. MainFrame is the big window, and MainGrid is a grid in it.
EditorFrame is the window that pops up when you double-click a MainGrid, and EditorGrid is a grid inside EditorFrame.


MainGrid class holds EditorGrid objects, so whenever the EditorFrame object modifies the EditorGrid objects, MainGrid class knows what is changed.

All of the event handlers are in MainFrame and EditorFrame classes. "Frame" classes manage their "Grid" objects.



(Source Code)


Bricks.class


public class Bricks
{
public static void main(String[] args)
{
MainFrame mainFrame = new MainFrame();
}
}



MainFrame.class


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MainFrame extends JFrame
{
private MainGrid[][] mainGrid;
private MouseHandler mouseHandler;
private MouseMotionHandler mouseMotionHandler;
private Timer timer;

public MainFrame()
{
init();
setLayout(new GridLayout(mainGrid.length, mainGrid[0].length));
setSize(600,600);
setTitle("Bricks Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void init()
{
mouseHandler = new MouseHandler();
mouseMotionHandler = new MouseMotionHandler();
addMouseListener(mouseHandler);
addMouseMotionListener(mouseMotionHandler);

mainGrid = new MainGrid[10][10];
for(int i = 0; i < mainGrid.length; i++)
{
for(int j = 0; j < mainGrid[0].length; j++)
{
mainGrid[i][j] = new MainGrid(i,j);
mainGrid[i][j].addMouseListener(mouseHandler);
mainGrid[i][j].addMouseMotionListener(mouseMotionHandler);
add(mainGrid[i][j]);
}
}

timer = new Timer(50,new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
for(int i = 0; i < mainGrid.length; i++)
{
for(int j = 0; j < mainGrid[0].length; j++)
{
mainGrid[i][j].repaint(); // Re-paint all the grids every 50 milliseconds.
}
}
}
});
timer.start();
}

//--------------------------------------------------------------------------------------------------Inner classes

public class MouseHandler extends MouseAdapter // MouseListener class
{
public void mousePressed(MouseEvent event)
{
if(event.getSource() instanceof MainGrid)
{
if(((MainGrid)event.getSource()).isSelected()) //De-select the grid if it was already selected
{
((MainGrid)event.getSource()).setSelected(false);
}
else //If it was not selected,
{
for(int i = 0; i < mainGrid.length; i++) //De-select all the other selected grids.
{
for(int j = 0; j < mainGrid[0].length; j++)
{
if(mainGrid[i][j].isSelected())
{
mainGrid[i][j].setSelected(false);
}
}
}
((MainGrid)event.getSource()).setSelected(true); //Select the pressed grid.
}
}
}

public void mouseClicked(MouseEvent event)
{
if(event.getSource() instanceof MainGrid && event.getClickCount() == 2)
{
((MainGrid)event.getSource()).setSelected(true);
((MainGrid)event.getSource()).createEditorFrame();
}
if(event.getSource() instanceof MainGrid && event.getClickCount() > 2)
{
((MainGrid)event.getSource()).setSelected(true);
}
}
}

public class MouseMotionHandler extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent event)
{
if(event.getSource() instanceof MainGrid)
{
for(int i = 0; i < mainGrid.length; i++) //De-select all the other selected grids.
{
for(int j = 0; j < mainGrid[0].length; j++)
{
if(((MainGrid)event.getSource()).isSelected())
mainGrid[i][j].setSelected(false);
}
}
((MainGrid)event.getSource()).setSelected(true); //Select the pressed grid.
}
}
}
}



MainGrid.class (and) MainGrid$EditorFrame.class


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MainGrid extends JPanel
{
private EditorGrid[][] editorGrid;
private EditorFrame editorFrame;
private final int X;
private final int Y;
private boolean selected;
private MouseHandler mouseHandler;

public MainGrid(int y, int x)
{
Y = y;
X = x;
selected = false;
mouseHandler = new MouseHandler();

editorGrid = new EditorGrid[10][10];
for(int i = 0; i < editorGrid.length; i++)
{
for(int j = 0; j < editorGrid[0].length; j++)
{
editorGrid[i][j] = new EditorGrid();
}
}
}


//--------------------------------------------------------------------------------------------------Public Methods

public void paintComponent(Graphics g)
{
// Paint the grid based on editorGrid's status
super.paintComponent(g);

g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());

for(int i = 0; i < editorGrid.length; i++)
{
for(int j = 0; j < editorGrid[0].length; j++)
{
g.setColor(editorGrid[i][j].getBackground());
g.fillRect(((j+1)*5)-4,((i+1)*5)-4, 5, 5);
}
}

g.setColor(Color.DARK_GRAY);
if(selected != false)
g.setColor(Color.YELLOW);
g.drawRect(0,0, getWidth()-7, getHeight()-5);
}

public void createEditorFrame()
{
editorFrame = new EditorFrame();
}

public int getX()
{
return X;
}

public int getY()
{
return Y;
}

public boolean isSelected()
{
return selected;
}

public void setSelected(boolean sel)
{
selected = sel;
repaint();
}


//--------------------------------------------------------------------------------------------------Inner classes

public class EditorFrame extends JFrame
{
public EditorFrame()
{
init();
setSize(300,300);
setTitle("Editor :: Row " + Y + ", Column " + X);
setLayout(new GridLayout(editorGrid.length, editorGrid[0].length));
setVisible(true);
}

public void init()
{
addMouseListener(mouseHandler);

for(int i = 0; i < editorGrid.length; i++)
{
for(int j = 0; j < editorGrid[0].length; j++)
{
editorGrid[i][j].addMouseListener(mouseHandler);
add(editorGrid[i][j]);
}
}
}
}

public class MouseHandler extends MouseAdapter // MouseListener class
{
public void mousePressed(MouseEvent event)
{
if(event.getSource() instanceof EditorGrid && ((EditorGrid)event.getSource()).getBackground() == Color.BLACK)
{
((EditorGrid)event.getSource()).setBackground(Color.GRAY);
MainGrid.this.repaint();
}
else if(event.getSource() instanceof EditorGrid && ((EditorGrid)event.getSource()).getBackground() == Color.GRAY)
{
((EditorGrid)event.getSource()).setBackground(Color.BLACK);
MainGrid.this.repaint();
}
}
}
}



EditorGrid.class


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class EditorGrid extends JPanel
{
private Color background;

public EditorGrid()
{
background = Color.BLACK;
}


//--------------------------------------------------------------------------------------------------Frequently used Methods

public void paintComponent(Graphics g)
{
//Paint the grid based on background.
super.paintComponent(g);

super.setBackground(background);

g.setColor(Color.DARK_GRAY);
g.drawRect(2, 2, getWidth()-2, getHeight()-2);
}

public void setBackground(Color bg)
{
background = bg;
repaint();
}

public Color getBackground()
{
return background;
}
}

No comments:

Post a Comment