StatsComponent.java

net.minecraft.server.gui.StatsComponent

信息

  • 全限定名:net.minecraft.server.gui.StatsComponent
  • 类型:public class
  • 包:net.minecraft.server.gui
  • 源码路径:src/main/java/net/minecraft/server/gui/StatsComponent.java
  • 起始行号:L15
  • 继承:JComponent
  • 职责:

    TODO

字段/常量

  • DECIMAL_FORMAT

    • 类型: DecimalFormat
    • 修饰符: private static final
    • 源码定位: L16
    • 说明:

      TODO

  • values

    • 类型: int[]
    • 修饰符: private final
    • 源码定位: L17
    • 说明:

      TODO

  • vp

    • 类型: int
    • 修饰符: private
    • 源码定位: L18
    • 说明:

      TODO

  • msgs

    • 类型: String[]
    • 修饰符: private final
    • 源码定位: L19
    • 说明:

      TODO

  • server

    • 类型: MinecraftServer
    • 修饰符: private final
    • 源码定位: L20
    • 说明:

      TODO

  • timer

    • 类型: Timer
    • 修饰符: private final
    • 源码定位: L21
    • 说明:

      TODO

内部类/嵌套类型

构造器

public StatsComponent(MinecraftServer server) @ L23

  • 构造器名:StatsComponent
  • 源码定位:L23
  • 修饰符:public

参数:

  • server: MinecraftServer

说明:

TODO

方法

下面的方法块按源码顺序生成。

private void tick() @ L33

  • 方法名:tick
  • 源码定位:L33
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public void paint(Graphics g) @ L45

  • 方法名:paint
  • 源码定位:L45
  • 返回类型:void
  • 修饰符:public

参数:

  • g: Graphics

说明:

TODO

public void close() @ L66

  • 方法名:close
  • 源码定位:L66
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public class StatsComponent extends JComponent {
    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("########0.000", DecimalFormatSymbols.getInstance(Locale.ROOT));
    private final int[] values = new int[256];
    private int vp;
    private final @Nullable String[] msgs = new String[11];
    private final MinecraftServer server;
    private final Timer timer;
 
    public StatsComponent(MinecraftServer server) {
        this.server = server;
        this.setPreferredSize(new Dimension(456, 246));
        this.setMinimumSize(new Dimension(456, 246));
        this.setMaximumSize(new Dimension(456, 246));
        this.timer = new Timer(500, event -> this.tick());
        this.timer.start();
        this.setBackground(Color.BLACK);
    }
 
    private void tick() {
        long usedRam = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        this.msgs[0] = "Memory use: "
            + usedRam / 1024L / 1024L
            + " mb ("
            + Runtime.getRuntime().freeMemory() * 100L / Runtime.getRuntime().maxMemory()
            + "% free)";
        this.msgs[1] = "Avg tick: " + DECIMAL_FORMAT.format((double)this.server.getAverageTickTimeNanos() / TimeUtil.NANOSECONDS_PER_MILLISECOND) + " ms";
        this.values[this.vp++ & 0xFF] = (int)(usedRam * 100L / Runtime.getRuntime().maxMemory());
        this.repaint();
    }
 
    @Override
    public void paint(Graphics g) {
        g.setColor(new Color(16777215));
        g.fillRect(0, 0, 456, 246);
 
        for (int x = 0; x < 256; x++) {
            int v = this.values[x + this.vp & 0xFF];
            g.setColor(new Color(v + 28 << 16));
            g.fillRect(x, 100 - v, 1, v);
        }
 
        g.setColor(Color.BLACK);
 
        for (int i = 0; i < this.msgs.length; i++) {
            String msg = this.msgs[i];
            if (msg != null) {
                g.drawString(msg, 32, 116 + i * 16);
            }
        }
    }
 
    public void close() {
        this.timer.stop();
    }
}

引用的其他类