Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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); 

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...