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

Java Applet图形绘制求助:中心矩形内线条生成异常

解决Java Applet中心矩形内线条绘制问题

问题原因分析

  • 初始坐标未对应中心矩形的实际边缘位置,导致线条起始点错误
  • 循环中混淆了宽度步长(w2div)和高度步长(h2div)的用途,使线条偏移方向错误
  • 仅实现了单个方向的线条,未覆盖中心矩形的四个边,无法达到和外部矩形一致的效果

修正后的完整代码

// Lab04vst.java
// 

import java.awt.*;
import java.applet.*;

public class Lab04vsthome extends Applet
{
    public void paint(Graphics g)
    {
        int width = 980;
        int height = 630;
        int x1 = 10;
        int y1 = 640;
        int x2 = 990;
        int y2 = 640;
        g.drawRect(10,10,width,height);

        int hdiv = height/50;
        int wdiv = width/50;

        //bottom right
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y2 -= hdiv;
            x1 += wdiv;
        }   

        x1 = 10;
        y1 = 640;
        x2 = 990;
        y2 = 640;

        //bottom left
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y1 -= hdiv;
            x2 -= wdiv;
        }   

        x1 = 10;
        y1 = 10;
        x2 = 990;
        y2 = 10;

        //top right
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y2 += hdiv;
            x1 += wdiv;
        }   

        x1 = 10;
        y1 = 10;
        x2 = 990;
        y2 = 10;

        //top left
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y1 += hdiv;
            x2 -= wdiv;
        }
        
        // 中心矩形部分(修正后的代码)
        int width2 = 500;
        int height2 = 312;
        int centerRectX = 250;
        int centerRectY = 170;
        g.drawRect(centerRectX, centerRectY, width2, height2);
        
        int w2div = width2 / 50;
        int h2div = height2 / 50;

        // 中心矩形 - bottom right
        int x3 = centerRectX;
        int y3 = centerRectY + height2; // 矩形底部y坐标
        int x4 = centerRectX + width2;
        int y4 = centerRectY + height2;
        for (int k = 0; k <= 50; k++)
        {
            g.drawLine(x3, y3, x4, y4);
            y4 -= h2div; // 顶部点向上移动(减少y值)
            x3 += w2div; // 左侧点向右移动(增加x值)
        }

        // 中心矩形 - bottom left
        x3 = centerRectX;
        y3 = centerRectY + height2;
        x4 = centerRectX + width2;
        y4 = centerRectY + height2;
        for (int k = 0; k <= 50; k++)
        {
            g.drawLine(x3, y3, x4, y4);
            y3 -= h2div; // 左侧点向上移动
            x4 -= w2div; // 右侧点向左移动
        }

        // 中心矩形 - top right
        x3 = centerRectX;
        y3 = centerRectY;
        x4 = centerRectX + width2;
        y4 = centerRectY;
        for (int k = 0; k <= 50; k++)
        {
            g.drawLine(x3, y3, x4, y4);
            y4 += h2div; // 右侧点向下移动
            x3 += w2div; // 左侧点向右移动
        }

        // 中心矩形 - top left
        x3 = centerRectX;
        y3 = centerRectY;
        x4 = centerRectX + width2;
        y4 = centerRectY;
        for (int k = 0; k <= 50; k++)
        {
            g.drawLine(x3, y3, x4, y4);
            y3 += h2div; // 左侧点向下移动
            x4 -= w2div; // 右侧点向左移动
        }
    }
}

关键修改说明

  1. 坐标校准:通过centerRectY + height2获取中心矩形的底部y坐标,确保线条从矩形的实际边缘开始绘制
  2. 步长修正:循环中使用h2div控制y轴方向的移动,w2div控制x轴方向的移动,和外部矩形的逻辑保持一致
  3. 补全四个方向:按照外部矩形的实现逻辑,补充了中心矩形四个角的线条循环,实现对称的视觉效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 15:41:16