创建一个循环以绘制垂直线 [英] Create a loop to draw vertical lines

查看:67
本文介绍了创建一个循环以绘制垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java画一个国际象棋棋盘,为了做到这一点,我从画垂直线开始.我已经完成了,但是我不想写出每一行,而是想实现一个循环.我是一个初学者,所以一些建议确实会有所帮助!先感谢您.

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

    }

}

推荐答案

将可变元素替换为循环变量,例如

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

for循环的说明

 for ( A;  B;  C)

A:第一次进入循环语句时执行此操作

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

B:检查是否为真,如果是,则执行循环

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

C:每次执行循环时,请在此之后执行.

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

因此,我们将i设置为100,执行循环并将i加100.如果i超过800,则继续循环.

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

另外

这不是绘制UI的好方法

This is not a nice way to draw to the UI

Graphics paper = panel.getGraphics();

使用面板@Override paint(Graphics g)方法并使用其中传递的图形对象进行绘制,这意味着仅在绘制面板时才进行绘制.然后,如果需要刷新,只需致电repaint().

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.

如果您要弄乱GUI,则应该将其放在EDT线程中,以免多个线程进行图形操作(因为它们可能会干扰)

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

这将其交给专用的UI更改线程队列.

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

这篇关于创建一个循环以绘制垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆