You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing结合数组绘制两条直线仅显示一条的问题排查

问题原因分析

你的问题核心在于每次调用DrawLine方法时都重新创建了坐标数组,导致之前存储的第一条直线数据被清空覆盖:

  • 第一次调用DrawLine时,创建四个新数组并存入第一条直线坐标,PosCount变为1;
  • 第二次调用DrawLine时,又重新初始化四个空数组,之前的第一条直线数据完全丢失,仅在新数组的索引1位置存入第二条直线坐标;
  • 最后Panel绘制时,循环到PosCount=2,但新数组的索引0位置是默认值0,第一条直线变成了(0,0)到(0,0)的不可见线段,只有第二条直线能正常显示。

另外,修改坐标数据后没有主动触发Panel重绘,即使数据正确,界面也不会及时更新。

修复后的代码

主类VB修改

package main;

import javax.swing.*;

public class VB {
    
    public static int[] POS_x1;
    public static int[] POS_y1;
    public static int[] POS_x2;
    public static int[] POS_y2;
    
    public static int PosCount;
    private static final int MAX_LINES = 256;
    
    // 静态初始化块,仅创建一次数组
    static {
        POS_x1 = new int[MAX_LINES];
        POS_y1 = new int[MAX_LINES];
        POS_x2 = new int[MAX_LINES];
        POS_y2 = new int[MAX_LINES];
        PosCount = 0;
    }
    
    VB(){

    }
    
    public static void NewWindow(String Title, String LogoPath, int x, int y, int Width, int Height){
        
        Panel panel = new Panel();
        
        ImageIcon image = new ImageIcon(LogoPath); 
        
        JFrame frame = new JFrame();
        frame.setIconImage(image.getImage());
        frame.setTitle(Title);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setBounds(x, y, Width, Height);
        frame.setResizable(false);
        frame.setVisible(true); 
    }
    
    public static void DrawLine(int x1, int y1, int x2, int y2) {
        // 防止数组越界
        if(PosCount >= MAX_LINES){
            System.out.println("已达到最大直线数量");
            return;
        }
        // 直接往已有数组添加数据,不再重新创建数组
        POS_x1[PosCount] = x1;
        POS_y1[PosCount] = y1;
        POS_x2[PosCount] = x2;
        POS_y2[PosCount] = y2;
        PosCount++;
        System.out.println(PosCount);
        // 触发界面重绘,更新所有直线
        SwingUtilities.getWindowAncestor(new Panel()).repaint();
    }
    
    public static void main(String[] args) {
        VB.NewWindow("window", "res/img/saullularphone.PNG", 100, 100, 500, 500);
        VB.DrawLine(0, 0, 100, 50);
        VB.DrawLine(0, 50, 100, 50);
    }
}

Panel类优化

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class Panel extends JPanel {
    
    Panel(){
        // 窗口显示时会自动触发paintComponent,构造方法中无需额外调用repaint
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.black); // 颜色设置移到循环外,减少重复操作
        for (int pos = 0; pos < VB.PosCount; pos++) {
            g2.drawLine(VB.POS_x1[pos], VB.POS_y1[pos], VB.POS_x2[pos], VB.POS_y2[pos]);
        }
    }
}
关键修复点
  • 数组初始化移至静态块:确保数组仅被创建一次,不会清空历史数据;
  • 添加容量检查:避免数组越界异常;
  • 主动触发重绘:修改数据后调用repaint()让Panel重新绘制所有直线;
  • 优化绘制逻辑:将颜色设置移到循环外,减少不必要的重复操作。

内容的提问来源于stack exchange,提问作者MaxChip 101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 04:45:46