import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GridPanelLauncher extends JFrame {
public class LocalGridPanel extends JPanel {
private static final int X_GRID_DISTANCE = 20;
private static final int Y_GRID_DISTANCE = 20;
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2d = (Graphics2D) g.create( );
g2d.setColor( Color.LIGHT_GRAY );
g2d.setStroke( new BasicStroke( 1 ) );
// Draw vertical grid lines
for (int x = 0; x < this.getWidth( ); x += X_GRID_DISTANCE) {
g2d.drawLine( x, 0, x, this.getHeight( ) );
}
// Draw horizontal grid lines
for (int y = 0; y < this.getHeight( ); y += Y_GRID_DISTANCE) {
g2d.drawLine( 0, y, this.getWidth( ), y );
}
g2d.dispose( );
}
}
public GridPanelLauncher() {
JPanel panel = new LocalGridPanel( );
getContentPane( ).add( panel );
}
private static void createAndShowGUI() {
JFrame mainFrame = new GridPanelLauncher( );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.setSize( 800, 610 );
mainFrame.setVisible( true );
}
public static void main( String[] args ) {
Runnable doCreateAndShowGUI = new Runnable( ) {
public void run() {
createAndShowGUI( );
}
};
SwingUtilities.invokeLater( doCreateAndShowGUI );
}
}
|