-1

我需要帮助用 Java 画线。我已经为一个角落编写了 15 行的代码。但是我很难弄清楚如何同时在 4 个角的每一个中再次绘制这 15 条线。谁能告诉我如何在 4 个角落中的每个角落镜像我当前的代码?

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


} 
} 
}
4

1 回答 1

1
x = 0;
y = height;

This will start at the far left, in the bottom corner. It's simply a case of changing these values. For example:

x = width; // Far right
y = 0; // Top of the component.

Ergo, this will start in the top right of the component.

于 2013-05-03T16:01:08.983 回答