调用JPanel.getGraphics()出现java.lang.NullPointerException

qinguan0619 qinguan0619 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大大,我想画个网格,但是
Graphics graphics = panel.getGraphics();
这条语句却报出java.lang.NullPointerException?
不知什么原因,求解,谢谢.


package test;

import java.awt.*;
import javax.swing.*;

public class TestGrid extends JFrame{

public static void main(String[] args) {
TestGrid game = new TestGrid();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
}

public TestGrid(){
super();
this.setTitle("just a test!");
this.setSize(600, 400);
this.getContentPane().setLayout(new BorderLayout());

DrawGrid(resultPanel);
this.add(resultPanel,BorderLayout.CENTER);
}


private void DrawGrid(JPanel panel) {
width = panel.getWidth();
height = panel.getHeight();

section_width = width / 20;
section_height = height / 20;

remainder_width = width - section_width * 20;
remainder_height = height - section_height * 20;

Graphics graphics = panel.getGraphics();
graphics.clearRect(0, 0, width, height);

for (int i = 0; i <= 20; i++) {
graphics.drawLine(remainder_width / 2 + section_width * i,
remainder_height / 2,
remainder_width / 2 + section_width * i,
height - remainder_height / 2);
}

for (int i = 0; i <= 20; i++) {
graphics.drawLine(remainder_width / 2,
remainder_height / 2 + section_height * i,
width - remainder_width / 2,
remainder_height / 2 + section_height * i);
}
}

private int width ;
private int height ;
private int section_width;
private int section_height;
private int remainder_width;
private int remainder_height;
private JPanel resultPanel = new JPanel();
}


...全文

相关推荐

产教融合实训基地与实践中心建设逻辑图-可编辑版

产教融合实训基地与实践中心建设逻辑图-可编辑版

龙猫与奥特曼

idea2025-free.zip

idea2025-free.zip

qq_45190508

sop基于灵敏度分析的有源配电网智能软开关优化配置升级1(Matlab代码实现)

内容概要:本文提出了一种基于灵敏度分析的有源配电网智能软开关(SOP)优化配置方法,旨在应对高比例分布式电源接入带来的电压波动与网络损耗加剧等问题。研究以IEEE33节点系统为算例,通过改进的灵敏度分析模型量化各节点对电压和功率流动的影响程度,进而构建SOP选址与定容的优化模型,实现降低网损、改善电压分布及提升系统运行灵活性的目标。全文详细阐述了灵敏度指标的数学推导、目标函数与约束条件的建模过程,并基于Matlab实现了完整的仿真求解流程,具备良好的工程实用性与学术参考价值。; 适合人群:电力系统及其自动化、电气工程等相关专业的硕士博士研究生、科研机构研究人员,以及从事智能配电网规划、运行与优化的工程技术人员。; 使用场景及目标:①研究分布式电源大规模接入背景下配电网的主动调控与优化策略;②指导智能软开关在实际配电网中的科学选址与容量配置;③为相关科研课题提供可复现的仿真案例,支撑学术论文撰写与算法验证。; 阅读建议:建议读者结合提供的Matlab代码与IEEE33节点标准模型进行动手实践,重点理解灵敏度指标的物理意义及其在优化模型中的应用逻辑,关注求解过程中的约束设置与算法收敛性,并可进一步拓展至SOP与储能联合配置、多时间尺度优化等复杂场景进行二次开发与研究。

荔枝科研社

Ubuntu安装Latex

关于Ubuntu安装Latex的详细记录和排错指南

Leers Ting

高校技术转移办公室人员在成果转化过程中,如何实现科技成果与产业需求的高效对接?.docx

高校技术转移办公室人员在成果转化过程中,如何实现科技成果与产业需求的高效对接?

BIZKEEN
评论 5
收藏 已收藏
分享
返回顶部
qinguan0619
Java SE 62,620 成员 307,251 内容