Basically, i've been told to initialise a grid into the "canvas" section of this program i've made, I need to make a fine grid that stretches across the whole canvas horizontally and vertically, 10 x 10 pixels apart example of the code i'm editing to do this below...
class Canvas extends JPanel
{
// Called every time there is a change in the canvas contents.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
draw(g);
g.setColor(new Color(0.8F, 0.8F, 0.8F));
g.drawLine(10, 800, 10, 10);
g.drawLine(20,800, 20, 10);
g.drawLine(30, 800, 30, 10);
g.drawLine(40,800, 40, 10);
g.drawLine(50, 800, 50, 10);
g.drawLine(60,800, 60, 10);
g.drawLine(70, 800, 70, 10);
g.drawLine(80,800, 80, 10);
g.drawLine(90, 800, 90, 10);
g.drawLine(100,800, 100, 10);
g.drawLine(110, 800, 110, 10);
g.drawLine(120,800, 120, 10);
g.drawLine(130, 800, 130, 10);
g.drawLine(140,800, 140, 10);
}
} // end inner class Canvas
^This is what i've been doing it like, adding a line one by one, but is there any way i can set it to fill the entire canvas with horizontal and vertical lines that are spaced 10 pixels apart, without having to write the whole thing out line by line, i assumed you would have to write an array, but i've got no idea how i can use it to keep adding lines until it finds the total height and width and stops adding them.
Any help would be appreciated.
//All the code in this particular program is below so you can test for yourself what it does. The lines i have put in already is a crude part of the end result :P.
For easier figuring out, the canvas is 800 width by 640 height.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
public class DrawingApplication extends JFrame
{
// GUI Component dimentsions.
private final int CANVAS_INITIAL_WIDTH = 800;
private final int CANVAS_INITIAL_HEIGHT = 640;
private final int CONTROL_PANEL_WIDTH = 200;
private final int MESSAGE_AREA_HEIGHT = 100;
class CanvasMouseMotionListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
canvas.addMouseMotionListener(new CanvasMouseMotionListener());
;
}
public void mouseDragged(MouseEvent event)
{
canvas.addMouseMotionListener(new CanvasMouseMotionListener());
;
}
}
// Drawing area class (inner class).
class Canvas extends JPanel
{
// Called every time there is a change in the canvas contents.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
draw(g);
g.setColor(new Color(0.8F, 0.8F, 0.8F));
g.drawLine(10, 800, 10, 10);
g.drawLine(20,800, 20, 10);
g.drawLine(30, 800, 30, 10);
g.drawLine(40,800, 40, 10);
g.drawLine(50, 800, 50, 10);
g.drawLine(60,800, 60, 10);
g.drawLine(70, 800, 70, 10);
g.drawLine(80,800, 80, 10);
g.drawLine(90, 800, 90, 10);
g.drawLine(100,800, 100, 10);
g.drawLine(110, 800, 110, 10);
g.drawLine(120,800, 120, 10);
g.drawLine(130, 800, 130, 10);
g.drawLine(140,800, 140, 10);
}
} // end inner class Canvas
private Canvas canvas;
private JPanel controlPanel;
private JLabel coordinatesLabel;
private JRadioButton lineRadioButton, ovalRadioButton, rectangleRadioButton, freehandRadioButton;
private JSlider freehandSizeSlider;
private JCheckBox fineCheckBox, coarseCheckBox;
private JButton colourButton, clearButton, animateButton;
private JTextArea messageArea;
private JMenuBar menuBar;
/*****************************************************************
*
* Constructor method starts here
* ... and goes on for quite a few lines of code
*/
public DrawingApplication()
{
setTitle("Drawing Application (da1)");
setLayout(new BorderLayout()); // Layout manager for the frame.
// Canvas
canvas = new Canvas();
canvas.setBorder(new TitledBorder(new EtchedBorder(), "Canvas"));
canvas.setPreferredSize(new Dimension(CANVAS_INITIAL_WIDTH, CANVAS_INITIAL_HEIGHT));
// next line changes the cursor's rendering whenever the mouse drifts onto the canvas
canvas.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
add(canvas, BorderLayout.CENTER);
// Menu bar
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem fileSaveMenuItem = new JMenuItem("Save");
fileMenu.add(fileSaveMenuItem);
JMenuItem fileLoadMenuItem = new JMenuItem("Load");
fileMenu.add(fileLoadMenuItem);
fileMenu.addSeparator();
JMenuItem fileExitMenuItem = new JMenuItem("Exit");
fileMenu.add(fileExitMenuItem);
menuBar.add(fileMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem helpAboutMenuItem = new JMenuItem("About");
helpMenu.add(helpAboutMenuItem);
menuBar.add(helpMenu);
add(menuBar, BorderLayout.PAGE_START);
// Control Panel
controlPanel = new JPanel();
controlPanel.setBorder(new TitledBorder(new EtchedBorder(), "Control Panel"));
controlPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH, CANVAS_INITIAL_HEIGHT));
// the following two lines put the control panel in a scroll pane (nicer?).
JScrollPane controlPanelScrollPane = new JScrollPane(controlPanel);
controlPanelScrollPane.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH + 30, CANVAS_INITIAL_HEIGHT));
add(controlPanelScrollPane, BorderLayout.LINE_START);
// Control Panel contents are specified in the next section:
// mouse coords panel;
// shape tools panel;
// trace-slider panel;
// grid panel;
// colour choice panel;
// "clear" n "animate" buttons
// Mouse Coordinates panel
JPanel coordinatesPanel = new JPanel();
coordinatesPanel.setBorder(new TitledBorder(new EtchedBorder(), "Drawing Position"));
coordinatesPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 60));
coordinatesLabel = new JLabel ();
coordinatesPanel.add(coordinatesLabel);
controlPanel.add(coordinatesPanel);
coordinatesLabel.setText("Co-ordinates");
// Drawing tools panel
JPanel drawingToolsPanel = new JPanel();
drawingToolsPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 140));
drawingToolsPanel.setLayout(new GridLayout(0, 1));
drawingToolsPanel.setBorder(new TitledBorder(new EtchedBorder(), "Drawing Tools"));
controlPanel.add(drawingToolsPanel);
// Freehand trace size slider
JPanel freehandSliderPanel = new JPanel();
freehandSliderPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 90));
drawingToolsPanel.setLayout(new GridLayout(0, 1));
freehandSliderPanel.setBorder(new TitledBorder(new EtchedBorder(), "Freehand Size"));
controlPanel.add(freehandSliderPanel);
// Grid Panel
JPanel gridPanel = new JPanel();
gridPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 80));
gridPanel.setLayout(new GridLayout(0, 1));
gridPanel.setBorder(new TitledBorder(new EtchedBorder(), "Grid"));
controlPanel.add(gridPanel);
// Colour Panel
JPanel colourPanel = new JPanel();
colourPanel.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 90));
colourPanel.setBorder(new TitledBorder(new EtchedBorder(), "Colour"));
colourButton = new JButton();
colourButton.setPreferredSize(new Dimension(50, 50));
colourPanel.add(colourButton);
controlPanel.add(colourPanel);
// Clear button
clearButton = new JButton("Clear Canvas");
clearButton.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 50));
controlPanel.add(clearButton);
// Animate button
animateButton = new JButton("Animate");
animateButton.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH - 20, 50));
controlPanel.add(animateButton);
// that completes the control panel section
// Message area
messageArea = new JTextArea();
messageArea.setEditable(false);
messageArea.setBackground(canvas.getBackground());
JScrollPane textAreaScrollPane = new JScrollPane(messageArea);
textAreaScrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Message Area"));
textAreaScrollPane.setPreferredSize(new Dimension(CONTROL_PANEL_WIDTH + CANVAS_INITIAL_WIDTH, MESSAGE_AREA_HEIGHT));
add(textAreaScrollPane, BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
} // end of the DrawingApplication constructor method
// Called by the canvas' paintComponent method
void draw(Graphics g)
{
;
} // end draw method
public static void main(String args[])
{
DrawingApplication drawingApplicationInstance = new DrawingApplication();
} // end main method
} // end of DrawingApplication class