View Single Post
  #2 (permalink)  
Old 10-18-2007, 11:10 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class CreateGridRx extends JPanel { int gridSize; final int PAD = 20; public CreateGridRx(int size) { this.gridSize = size; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); double xInc = (double)(w - 2*PAD)/gridSize; double yInc = (double)(h - 2*PAD)/gridSize; for(int j = 0; j <= gridSize; j++) { double x = PAD + j*xInc; g2.draw(new Line2D.Double(x, PAD, x, h-PAD)); } for(int j = 0; j <= gridSize; j++) { double y = PAD + j*yInc; g2.draw(new Line2D.Double(PAD, y, w-PAD, y)); } } public static void main(String[] args) { String input = JOptionPane.showInputDialog( "Enter number of rows and columns you want grid to contain\n" + "(For example entering 2 will create a 2x2 grid)\n"); int gridSquares = Integer.parseInt( input ); CreateGridRx test = new CreateGridRx(gridSquares); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Reply With Quote