//**************************************************************************
// PP 6.11
//
// Design and implement a program that draws 20 horizontal, evenly spaced
// parallel lines of random length.
//**************************************************************************
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class PP6_11
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Lines");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
LinesPanel panel = new LinesPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class LinesPanel extends JPanel
{
private final int WIDTH = 400,HEIGHT = 300, LENGTH = WIDTH / 2;
private final int SPACE = HEIGHT / 20, NUM_LINES = 20;
private Random generator;
我已经完成了作业,效果很好。编译并运行时,代码绘制了 20 行,因为我使用了“SPACE”变量。我想知道是否有任何方法可以通过使用“NUM_LINES”变量来告诉程序我想要它绘制多少条线。
//-----------------------------------------------------------------------
// Sets up the drawing panel.
//-----------------------------------------------------------------------
public LinesPanel()
{
generator = new Random();
setBackground (Color.black);
setPreferredSize (new Dimension (WIDTH, HEIGHT));
}
//-----------------------------------------------------------------------
// Paints evenly spaced Horizontal lines of random length.
// lines that are half the width are highlighted with a re color.
//-----------------------------------------------------------------------
public void paintComponent (Graphics page)
{
每次我尝试在 for 循环中使用 NUM_LINES = 20 和 SPACE = 20 变量时,它只绘制几行。这是我之前使用的 for 循环“for(int i = 0; i <= NUM_LINES; i += SPACE)”
for (int i = 0; i <= HEIGHT; i += SPACE)
{
int y = generator.nextInt(WIDTH) + 1;
if (y <= LENGTH)
{
page.setColor(Color.red);
page.drawLine(0,i,y,i);
}
else
{
page.setColor(Color.blue);
page.drawLine (0,i,y,i);
}
}
}
}
有没有办法确定我画了多少条线并均匀地间隔它,或者我这样做的方式是最好的方法吗?
最佳答案
简单的答案是将 for 循环更改为:
for (int i = 0; i < HEIGHT; i += (HEIGHT - 1) / (NUM_LINES - 1))
请注意,我已经更改了比较以排除等于(因为高度实际上在您的窗口之外),并且我们动态计算线之间的距离。
从高度中减去可以纠正我们的线位于从零开始的坐标系中的事实。 NUM_LINES 的减法根据第一条线绘制为零的事实进行调整。请注意,您的解决方案并未考虑最后一个因素,但恰好“有效”,因为您在屏幕外绘制了最后一行。如果您将 HEIGHT 更改为 401,您实际上会看到 21 行。
这是一个更好的实现,它考虑了许多因素,例如重绘高度和行间距的奇数倍,以及实际的面板尺寸。它还将线条垂直居中,以更加美观。
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class PP6_11
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Lines");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
LinesPanel panel = new LinesPanel(20, 0.5);
panel.setPreferredSize (new Dimension (400, 300));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class LinesPanel extends JPanel
{
private final int numLines;
private final double colorSplit;
private final Random generator;
//-----------------------------------------------------------------------
// Sets up the drawing panel.
//-----------------------------------------------------------------------
public LinesPanel(int numLines, double colorSplit)
{
this.numLines = numLines;
this.colorSplit = colorSplit;
this.generator = new Random();
setBackground (Color.black);
}
//-----------------------------------------------------------------------
// Paints evenly spaced Horizontal lines of random length.
// lines that are half the width are highlighted with a re color.
//-----------------------------------------------------------------------
public void paintComponent (Graphics page)
{
// Clear the off-screen bitmap and set the background.
super.paintComponent(page);
final int pageWidth = getWidth();
final int pageHeight = getHeight();
// Calculate the line spacing and center vertically.
final int lineSpace = (pageHeight - 1) / (numLines - 1);
final int margin = (pageHeight - (lineSpace * (numLines - 1))) / 2;
final int splitWidth = (int)(pageWidth * colorSplit);
for (int y = margin; y < pageHeight; y += lineSpace)
{
int width = generator.nextInt(pageWidth) + 1;
page.setColor(width <= splitWidth ? Color.red : Color.blue);
page.drawLine(0, y, width, y);
}
}
}
关于java - 有没有办法使用 for 循环确定要在页面上绘制的确切行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508250/