簡體   English   中英

在 JPanel 的整個高度上繪制線條

[英]Drawing lines the full height of JPanel

除了最后一件事,我的代碼大部分都可以工作。 當繪制的線條數 > 20 時,線條不會被繪制以占用面板的全部空間

** 看看我截圖的右下角。 go 行在面板的整個高度下方。 我需要它到 go 高度的全長。 **

在此處輸入圖像描述

這是我的 DrawPanelTest 代碼


// Creating JPanel to display DrawPanel
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DrawPanelTest
{
   public static void main(String[] args)
   {
      int nValue = 0; // declare variable to store user input. Default value 0.
      Boolean flag = true; // initalize flag to true for argument value of while-loop

      // while-loop to prompt user input
      while (flag) {
         // prompt user for the value of N
         nValue = Integer.parseInt(JOptionPane.showInputDialog("Enter number of lines between 2 and 100."));

        // user input validation. Valid input is nValue [2, 100]
        if (nValue < 2 || nValue > 100) {
           nValue = Integer.parseInt(JOptionPane.showInputDialog("Enter number of lines between 2 and 100."));
        } else {
           flag = false; // if user input is correct, while-loop will end
        } // end if-else
      } // end while-loop
     
      // displays the value of N to make sure it really is correct; This works
      // String message = String.format("The value of n is: %d ", nValue);
      // JOptionPane.showMessageDialog(null, message);

      // create a panel that contains our drawing
      DrawPanel drawPanel = new DrawPanel(nValue);
      // create a new frame to hold the panel
      JFrame application = new JFrame();

      // set the frame to exit when closed
      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      application.add(drawPanel); // add panel to the frame
      application.setSize(600, 600); // set the size of the frame
      application.setVisible(true); // make the frame visible
   }
 } // end class DrawPanelTest

這是我的代碼 DrawPanel

 // Using drawLine() to connect the corners of a panel
import java.awt.Graphics;
import javax.swing.JPanel;


public class DrawPanel extends JPanel 
{
  int numLines;

  // constructor initializes DrawPanel and initializes
  // numLines with the argument value of n
  public DrawPanel(int n)
  {
    numLines = n;
  }

  // draws a X from the corners of the panel
  public void paintComponent( Graphics g)
  {
    // call paintComponent to ensure the panel displays correctly
    super.paintComponent(g);

    int width = getWidth(); // total width
    int height = getHeight(); // total height
    int x1 = 0; // starting x-coordinate
    int y1 = height / 2; // starting y-coordinate
    int x2 = width; // initial value for end x-coordinate
    int spaceValue = 0; // represents the space between the lines
    int stepValue = height/numLines; // represents the increment value starting from top right corner

    for (int i = 0; i <= numLines; i++) {
      if (numLines == 2) {
        g.drawLine(x1, y1, x2, 0);
        g.drawLine(x1, y1, x2, height);
        break;
      } // end numLines == 2

      else if (numLines >= 3) {
        g.drawLine(x1, y1, x2, spaceValue);
        spaceValue += stepValue;
      } // end else if
    } // end for-loop
  } // end paintComponent
} // end class DrawPanel

我認為我的問題出在 DrawPanel 第 41 行。我認為我沒有正確計算線條之間的空間,因此它們最終占據了面板的整個高度。

在此先感謝您的幫助。

您對“stepValue”的計算有兩個問題:

  1. stepValue 太小,所以最后一行永遠不會出現在底部
  2. stepValue 因 integer 數學而被截斷,因此每次將截斷值添加到“spaceValue”都會產生稍微不太准確的值。

假設您有一個高度為 600 且行數為 3 的面板。

你的計算是:

int stepValue = 600 / 3 = 200

我認為這是錯誤的,因為您會繪制 3 條“spaceValue”為 0、200、400 的線,因此永遠不會繪制最后一條線(在 600 處)。

實際上我認為計算應該是:

double stepValue = (double)height / (numLine - 1);

這使:

double stepValue = 600 / (3 - 1) = 300.

這將給出“spaceValue”為 0、300、600 的線,這將是頂部、中間和底部的線。

所以你的繪畫循環就變成了:

for (int i = 0; i < numLines; i++) 
{
    g.drawLine(x1, y1, x2, spaceValue);
    spaceValue += stepValue;
}

我認為您遇到的(其中一個)問題是“整數除法”問題。

int stepValue = height / numLines;

正在截斷結果。 例如,如果height400且行數為6 ,則stepValue將為66而不是67 (這將允許 66.6666 向上舍入)

現在,您可以自己“四舍五入”該值,但我更願意利用可用的 API 為我做這些事情。

Line2D.Double支持雙精度參數,非常適合這項工作

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (lineCount > 1) {
        double gap = getHeight() / (double)(lineCount - 1);
        for (int i = 0; i < lineCount; i++) {
            Line2D line = new Line2D.Double(0, getHeight() / 2, getWidth(), i * gap);
            g2d.draw(line);
        }
    } else {
        g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
    }
    g2d.dispose();
}

可運行的例子...

在此處輸入圖像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                JPanel contentPane = new JPanel(new BorderLayout());
                contentPane.setBorder(new EmptyBorder(32, 32, 32, 32));
                frame.setContentPane(contentPane);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int lineCount = 1;

        public TestPane() {
            setBorder(new LineBorder(Color.RED));
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    lineCount++;
                    repaint();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            FontMetrics fm = g2d.getFontMetrics();
            g2d.drawString(Integer.toString(lineCount), 10, fm.getAscent());
            if (lineCount > 1) {
                double gap = getHeight() / (double) (lineCount - 1);
                for (int i = 0; i < lineCount; i++) {
                    Line2D line = new Line2D.Double(0, getHeight() / 2, getWidth(), i * gap);
                    g2d.draw(line);
                }
            } else {
                g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
            }
            g2d.dispose();
        }

    }
}

在for循環中,改變

for (int i = 0; i < numLines; i++) {

for (int i = 0; i <= numLines; i++) {

迭代組件的整個高度。 i必須等於 numLines 才能達到統一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM