-1

Im trying to draw a Chess board in java, and in order to do this I am starting with drawing vertical lines. I have it done, but instead of writing every line out I want to implement a loop. I am a beginner, so some advice would really be helpful! Thank you in advance.

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

public class ChessBoard extends JFrame implements ActionListener
{
    private JButton button;
    private JPanel panel;

    public static void main(String[] args)
    {
        ChessBoard demo = new ChessBoard();
        demo.setSize(400,300);
        demo.createGUI();
        demo.setVisible(true);
    }

    private void createGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(800,800));
        panel.setBackground(Color.white);
        window.add(panel);

        button = new JButton("start");
        window.add(button);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event)
    {
        int xLeft;
        int yTop;
        Graphics paper = panel.getGraphics();
        paper.setColor(Color.black);
        paper.fillRect(0,0,800,800);
        paper.setColor(Color.white);
        xLeft = 0;
        paper.drawLine(100, 0, 100, 800); 
        paper.drawLine(200, 0, 200, 800); 
        paper.drawLine(300, 0, 300, 800); 
        paper.drawLine(400, 0, 400, 800); 
        paper.drawLine(500, 0, 500, 800); 
        paper.drawLine(600, 0, 600, 800); 
        paper.drawLine(700, 0, 700, 800); 
        paper.drawLine(800, 0, 800, 800); 

    }

}
eltabo
  • 3,749
  • 1
  • 21
  • 33
vs97
  • 5,765
  • 3
  • 28
  • 41
  • `for(int i = 100; i <= 800; i+=100) { paper.drawLine(i,0,i,800); }`. – Maroun Feb 03 '14 at 10:38
  • *"trying to draw a Chess board"* Sounds much more difficult than simply laying it out as seen in [Making a robust, resizable Chess GUI](http://stackoverflow.com/q/21142686/418556). That linked GUI uses buttons for the chess board squares, thereby adding keyboard accessibility, easy determination of which square was clicked, focus indication and an easy way to display the chess pieces themselves.. – Andrew Thompson Feb 03 '14 at 10:45
  • 1
    As already mentioned in my answer: You should **NOT** call `panel.getGraphics();` and paint into this Graphics object in your actionPerformed method. This is not the proper way of painting in Swing! Please have a look at http://docs.oracle.com/javase/tutorial/uiswing/painting/ – Marco13 Feb 03 '14 at 11:03

3 Answers3

1

Replace the varying element with a loop variable, e.g.

//you could probably also replace both instances of 800 here with a 'max' variable and the 100s with 'squareSize'
for (int i = 100; i<=800; i+=100)
{
  paper.drawLine(i, 0, i, 800); 
}

Explanation of the for loop

 for ( A;  B;  C)

A: Do this when you first get to the loop statement

B: Check this is true, if it is, execute the loop

C: Every time the loop executes, execute this afterwards.

So, we are setting i to 100, executing the loop and adding 100 to i. If i goes over 800, continue past the loop.


Additionally

This is not a nice way to draw to the UI

Graphics paper = panel.getGraphics();

Have your panel @Override the paint(Graphics g) method and use the graphics object passed in there to do your drawing this means drawing is only done when the panel is drawn. Then just call repaint() if you need to refresh it.

As well as that if you are messing with the GUI you should probably put it inside the EDT thread so as to avoid multiple threads doing graphic operations (as they can interfere)

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        //UI operations here
    }
} );

This hands it off to a dedicated UI change thread queue.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
1
for (int x=100; x<=800; x+=100)
{
    paper.drawLine(x, 0, x, 800); 
}

But you should NOT call panel.getGraphics() - this will sooner or later lead to rendering errors. There are many resources on the web that show how to do this properly, e.g. http://docs.oracle.com/javase/tutorial/uiswing/painting/

Marco13
  • 53,703
  • 9
  • 80
  • 159
0
int a = 100;   
for (int i = 0; i < 8; i++) {
    paper.drawLine(a, 0, a, 800);
    a += 100;
}
Martin
  • 239
  • 1
  • 15