gpt4 book ai didi

java - 在Java中制作一条数轴来显示一个点,getWidth()和getHeight()没有正确校准位置

转载 作者:行者123 更新时间:2023-12-02 02:35:01 28 4
gpt4 key购买 nike

所以我想创建一个数轴类,我可以用它来显示沿单个轴的单个点,但我希望它能够响应当前所在容器的大小,并相对于该容器更改其大小。不幸的是,我无法正确使用 getWidth() 和 getHeight() 来获取我想要的数轴。这是我到目前为止编写的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

public class NumberLine extends JPanel {
private int value;
private Color green1 = new Color(32, 77, 2);

@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int maxXValue = getWidth();
int maxYValue = getHeight();
Line2D.Float xline = new Line2D.Float((float) maxXValue/6, (float) maxYValue/2, (float) maxXValue * (5/6), (float) maxYValue/2);
Line2D.Float yline = new Line2D.Float( (float) maxXValue/ 2, (float) maxYValue * (9/20), (float) maxXValue/2, (float) maxYValue *(11/20));
g2.draw(xline);
g2.draw(yline);
Ellipse2D.Float cir = new Ellipse2D.Float((float) (maxXValue/10 + (8 * value/1000) * (maxXValue)), (float) (maxYValue/2), 10F, 10F );
g2.setColor(green1);
g2.fill(cir);
}


public NumberLine(int val0) {
value = val0;
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(150,100);
NumberLine num = new NumberLine(5);
frame.setContentPane(num);
frame.setVisible(true);

}
}

理想情况下,我想要这样的事情,如果我要做的话

NumberLine num = new NumberLine(5);

我会得到看起来像这样的东西:

-*------------------

相反,我得到:

Output from the code I have written

最佳答案

我认为你的问题是基本几何问题之一。如果您尝试将圆置于直线的中心,则需要从其位置减去其宽度和高度的一半。就是这样:

Ellipse2D.Float cir = new Ellipse2D.Float(
(float) (maxXValue / 10 + (8 * value / 1000) * (maxXValue)) - 5,
(float) (maxYValue / 2) - 5, 10F, 10F);

此外,您正在进行 int 除法,这会在您不需要的地方返回 0 值。改变

Line2D.Float yline = new Line2D.Float((float) maxXValue / 2, (float) maxYValue * (9 / 20),
(float) maxXValue / 2, (float) maxYValue * (11 / 20));

Line2D.Float yline = new Line2D.Float((float) maxXValue / 2, (float) maxYValue * (9f / 20f),
(float) maxXValue / 2f, (float) maxYValue * (11f / 20f));

不相关的问题:

  • 不要忘记调用 super 组件的paintComponent方法:

    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g); // !! don't forget this!
  • 并避免在程序中使用“魔法”数字,因为它们会使调试变得困难。

  • 使用 RenderingHints 平滑您的 Graphics2D 绘图:

    // rendering hints to smooth out your drawing
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
  • 在 EDT 上启动 Swing GUI:

    SwingUtilities.invokeLater(() -> {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

例如,类似:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import javax.swing.JPanel;

public @SuppressWarnings("serial")
class NumberLine3 extends JPanel {
private static final double X_GAP = 1.0 / 20.0;
private static final double MAJOR_TIC_HT = 0.4;
private static final int PREF_W = 600;
private static final int PREF_H = 50;
private static final Stroke MAIN_STROKE = new BasicStroke(5f);
private static final Stroke MAJOR_TIC_STOKE = new BasicStroke(3f);
private static final int CIRCLE_WIDTH = 20;
private static final Color VALUE_COLOR = new Color(32, 230, 2);
private int maxX;
private int majorTickCount;
private int minorTicksPerMajor;
private double value;

public NumberLine3(int maxX, int majorTickCount, int minorTicksPerMajor, double value) {
this.maxX = maxX;
this.majorTickCount = majorTickCount;
this.minorTicksPerMajor = minorTicksPerMajor;
this.value = value;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

// rendering hints to smooth out your drawing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Graphics2D g2b = (Graphics2D) g2.create(); // so we can change stroke without problems
g2b.setStroke(MAIN_STROKE);
int x1 = (int) xValueToScreen(-maxX);
int y1 = getHeight() / 2;
int x2 = (int) xValueToScreen(maxX);
int y2 = y1;
g2b.drawLine(x1, y1, x2, y2);
g2b.setStroke(MAJOR_TIC_STOKE);
for (int i = 0; i <= 2 * majorTickCount; i++) {
double xVal = ((double) i * maxX) / majorTickCount - maxX;
x1 = (int) xValueToScreen(xVal);
x2 = x1;
double dY1 = getHeight() * (1 - MAJOR_TIC_HT) / 2.0;
if (i == majorTickCount) {
dY1 = 0.5 * dY1;
}
double dY2 = getHeight() - dY1;
g2b.drawLine(x1, (int) dY1, x2, (int) dY2);
}
g2b.dispose();

g2.setColor(VALUE_COLOR);
x1 = (int) (xValueToScreen(value) - CIRCLE_WIDTH / 2.0);
y1 = (int) (getHeight() - CIRCLE_WIDTH) / 2;
g2.fillOval(x1, y1, CIRCLE_WIDTH, CIRCLE_WIDTH);
}

private double xValueToScreen(double xValue) {
double gap = getWidth() * X_GAP;
double scale = (double) (getWidth() - 2 * gap) / (2 * maxX);
return (xValue + maxX) * scale + gap;
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

public double getValue() {
return value;
}

public void setValue(double value) {
this.value = value;
repaint();
}

public int getMaxX() {
return maxX;
}

public int getMajorTickCount() {
return majorTickCount;
}

public int getMinorTicksPerMajor() {
return minorTicksPerMajor;
}
}

可以通过以下方式进行测试:

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class NumberLine3Test extends JPanel {
private static final int MAX_X = 40;
private static final int MAJOR_TICS = 4;
private static final int MINOR_TICS = 5;
private double value = 0.0;
private NumberLine3 numberLine3 = new NumberLine3(MAX_X, MAJOR_TICS, MINOR_TICS, value);
private JSlider slider = new JSlider(-MAX_X, MAX_X, 0);

public NumberLine3Test() {
slider.setPaintTicks(true);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.addChangeListener(ce -> {
value = slider.getValue();
numberLine3.setValue(value);
});

JPanel sliderPanel = new JPanel();
sliderPanel.add(slider);
int ebGap = 40;
sliderPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));

setLayout(new BorderLayout());
add(numberLine3, BorderLayout.PAGE_START);
add(sliderPanel);
}

private static void createAndShowGui() {
NumberLine3Test mainPanel = new NumberLine3Test();

JFrame frame = new JFrame("NumberLine3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 在Java中制作一条数轴来显示一个点,getWidth()和getHeight()没有正确校准位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504021/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com