使用循环在Java中绘制线条
我需要帮助用Java画线。我已经为一个角落写了15行代码。但我很难想出如何在4个角同时画出这15条线。有人能告诉我如何在四个角落中的每一个镜像我当前的代码吗
import javax.swing.JFrame;
public class DrawOneSetOfLines extends JPanel
{
public static void main(String args[])
{
DrawOneSetOfLines panel = new DrawOneSetOfLines();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(250, 250);
application.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int linesToDraw = 15;
int width = getWidth();
int height = getHeight();
int number, y, x, dy, dx;
x = 0;
y = height;
number = 15;
dx = width / number;
dy = height / number;
for( int i = 1; i < number; i++ )
{
x += dx;
y -= dy;
g.drawLine( 0, 0, y, x );
}
}
}
# 1 楼答案
这将从最左边的下角开始。这只是改变这些值的一个例子。例如:
因此,这将从组件的右上角开始