使用循环在Java中绘制线条
我需要帮助用Java画线。我已经为一个角落写了15行代码。但是我很难想出如何在4个角同时画出这15条线。有人能告诉我如何在4个角中的每一个都镜像我当前的代码吗使用循环在Java中绘制线条,java,lines,Java,Lines,我需要帮助用Java画线。我已经为一个角落写了15行代码。但是我很难想出如何在4个角同时画出这15条线。有人能告诉我如何在4个角中的每一个都镜像我当前的代码吗 import javax.swing.JFrame; public class DrawOneSetOfLines extends JPanel { public static void main(String args[]) { DrawOneSetOfLines panel = new DrawOneSetOfLines()
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 );
}
}
}
import javax.swing.JFrame;
公共类DrawOneSetOfLines扩展了JPanel
{
公共静态void main(字符串参数[])
{
DrawOneSetOfLines面板=新建DrawOneSetOfLines();
JFrame应用程序=新JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
应用程序。添加(面板);
应用程序。设置大小(250250);
application.setVisible(true);
}
公共组件(图形g)
{
超级组件(g);
int linesToDraw=15;
int width=getWidth();
int height=getHeight();
整数,y,x,dy,dx;
x=0;
y=高度;
数字=15;
dx=宽度/数量;
dy=高度/数量;
对于(int i=1;i
这将从最左边的下角开始。这只是改变这些值的一个例子。例如:
x = width; // Far right
y = 0; // Top of the component.
因此,这将从组件的右上角开始。请在询问之前尝试一下。
x = width; // Far right
y = 0; // Top of the component.