画图工具画不出来,求教!

LilyKingdom668564 LilyKingdom668564 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/************************************************************************************************************/
public class DrawMain extends JFrame{
//Main Method
public static void main(String[] args) {
J_ButtonPanel btPanel = new J_ButtonPanel();
J_PaintingGround ptGround = new J_PaintingGround(btPanel);
DrawMain app = new DrawMain(btPanel, ptGround);

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(600, 600);
app.setVisible(true);

//设置显示外观
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
e.printStackTrace();
}
}

//构造函数
public DrawMain(J_ButtonPanel btPanel, J_PaintingGround ptGround) {
//设置容器及容器的整体布局
super("画板例程");
Container cp = getContentPane();

cp.add(btPanel.getPanel(),BorderLayout.WEST);
cp.add(ptGround, BorderLayout.CENTER);
}
}
/************************************************************************************************************/
public class J_ButtonPanel extends JPanel implements ActionListener{
private int optype = 0; //操作类型变量,默认值为0
private JPanel buttonPanel = new JPanel(); //按钮版
public JPanel getPanel(){return buttonPanel;} //获取按钮版
public int getOptype(){return optype;} //获取操作类型
J_ButtonPanel() {
//新建按钮
btFree = new JButton("随手画");
btLine = new JButton("直线");
btRectangle = new JButton("矩形");
btRound = new JButton("圆");
btEllipse = new JButton("椭圆");
btFill = new JButton("填充");
btSave = new JButton("保存");
btOpen = new JButton("打开");
//添加按钮
buttonPanel.add(btOpen);
buttonPanel.add(btSave);
buttonPanel.add(btFree);
buttonPanel.add(btLine);
buttonPanel.add(btRectangle);
buttonPanel.add(btRound);
buttonPanel.add(btEllipse);
buttonPanel.add(btFill);
}

public void actionPerformed(ActionEvent event) { //鼠标监听器
Object source = event.getSource(); //获取动作,确定操作类型
if(source == btFree) optype = 0;
if(source == btLine) optype = 1;
if(source == btRectangle) optype = 2;
if(source == btRound) optype = 3;
if(source == btEllipse) optype = 4;
if(source == btFill) optype = 5;
if(source == btOpen) optype = 6;
if(source == btSave) optype = 7;
}

private JButton btFree;
private JButton btLine;
private JButton btRectangle;
private JButton btRound;
private JButton btEllipse;
private JButton btFill;
private JButton btSave;
private JButton btOpen;
}
/************************************************************************************************************/
public class J_PaintingGround extends JPanel{
public Vector<Vector<Integer>> m_vectorSet = new Vector<Vector<Integer>>(); //设置变量存放笔画

public J_PaintingGround(J_ButtonPanel buttonpanel) { //构造函数
btPanel = buttonpanel; //按钮版

if(btPanel.getOptype() == 0){
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
Vector<Integer> v = new Vector<Integer>();
v.add(btPanel.getOptype());
v.add(event.getX());
v.add(event.getY());
m_vectorSet.add(v);
}
}
);
addMouseMotionListener(new MouseMotionListener()
{
public void mouseMoved(MouseEvent event){}

public void mouseDragged(MouseEvent event)
{
int n = m_vectorSet.size()-1;
Vector<Integer> v = new Vector<Integer>();
v = m_vectorSet.get(n);
v.add(event.getX());
v.add(event.getY());
repaint();
}
}
);
}
if(btPanel.getOptype() >=1 && btPanel.getOptype() <= 4){
addMouseListener(new MouseAdapter()
{
Vector<Integer> v = new Vector<Integer>();
public void mousePressed(MouseEvent event)
{
v.add(btPanel.getOptype());
v.add(event.getX());
v.add(event.getY());
}
public void mouseReleased(MouseEvent event)
{
v.add(event.getX());
v.add(event.getY());
m_vectorSet.add(v);
repaint();
}
}
);
}
if(btPanel.getOptype() == 5){}
if(btPanel.getOptype() == 7){
try
{
FileOutputStream f = new FileOutputStream("绘画作品.txt");
DataOutputStream fout = new DataOutputStream(f);
int i,j,m;
int n = m_vectorSet.size();
Vector<Integer> vv = new Vector<Integer>();
for(i=0;i<n;i++)
{
vv = m_vectorSet.get(i);
m = vv.size()-1;
for(j=0; j<m; j++)
{
fout.writeInt(vv.get(j).intValue());
}
fout.writeInt('\n');
}
fout.close();
repaint();
}
catch(Exception e)
{
System.err.println("发生异常:" + e);
e.printStackTrace();
}
}

if(btPanel.getOptype() == 6){
try
{
int i,j,k=0;
RandomAccessFile f = new RandomAccessFile("绘画作品.txt","r");
Vector<Vector> v = new Vector<Vector>();
Vector<Integer> vv = new Vector<Integer>();

for(i=0;i<f.length()/4-1;i++)
{
if(f.readInt() == '\n'){
v.add(vv);
vv = new Vector<Integer>();
}
else {
f.seek(i*4);
vv.add(f.readInt());
}
}
this.repaint();
}
catch(Exception e)
{
System.err.println("发生异常:" + e);
e.printStackTrace();
}
}
}

protected void paintComponent(Graphics g)
{
g.setColor(Color.YELLOW);
g.clearRect(0, 0, getWidth(), getHeight());
Vector<Integer> v = new Vector<Integer>();
Point s, t;
int i, j, m;
int n = m_vectorSet.size();
for(i=0,j=0; i<n; i++)
{
v = m_vectorSet.get(i);
Integer a = v.get(0);
v.remove(0);
m = v.size()-1;
switch(a.intValue())
{
case FREE:
while(j+3<m){
s = new Point(v.get(j).intValue(),v.get(j+1).intValue());
t = new Point(v.get(j+2).intValue(),v.get(j+3).intValue());
g.drawLine(s.x, s.y, t.x, t.y);
j=j+2;
}
break;
case LINE:
s = new Point(v.get(j++).intValue(),v.get(j++).intValue());
t = new Point(v.get(j++).intValue(),v.get(j++).intValue());
g.drawLine(s.x, s.y, t.x, t.y);
break;
case ROUND:
s = new Point(v.get(j++).intValue(),v.get(j++).intValue());
t = new Point(v.get(j++).intValue(),v.get(j++).intValue());
g.drawRoundRect(s.x, s.y, t.x-s.x, t.x-s.x, t.x-s.x, t.x-s.x);
break;
case RECTANGLE:
s = new Point(v.get(j++).intValue(),v.get(j++).intValue());
t = new Point(v.get(j++).intValue(),v.get(j++).intValue());
g.drawRect(s.x, s.y, t.x-s.x, t.y-s.y);
break;
case ELLIPSE:
s = new Point(v.get(j++).intValue(),v.get(j++).intValue());
t = new Point(v.get(j++).intValue(),v.get(j++).intValue());
g.drawOval(s.x, s.y, t.x-s.x, t.y-s.y);
break;
default:break;
}
}
}

public Dimension getPreferredSize()
{
return new Dimension(600,600);
}

private int optype;
private J_ButtonPanel btPanel = new J_ButtonPanel();

public static final int FREE = 0;
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int ROUND = 3;
public static final int ELLIPSE = 4;
public static final int FILL = 5;
public static final int OPEN = 6;
public static final int SAVE = 7;
}
/************************************************************************************************************/
运行通过,但是无法在画图版上画图,按钮没反应。
...全文

相关推荐

CISP-CSE注册云安全工程师备考资源包基础教程

随着云计算技术的普及,云安全已成为网络安全领域的核心议题。CISP-CSE(注册云安全工程师)认证由中国信息安全测评中心颁发,旨在培养具备云安全专业知识和实践能力的实战型人才。本教程将结合"CISP-CSE注册云安全工程师备考资源包",系统性地为您梳理云安全的核心知识体系,并提供实战案例分析,助您高效备考。

t0_54coder

绘图模拟:在冲动环境中固定时间的示例 附matlab代码.rar

绘图模拟:在冲动环境中固定时间的示例 附matlab代码.rar

天天Matlab科研工作室

基于规则的电动汽车混合能源管理系统,采用MATLAB、Simulink、Simscape Electrical和Stateflow。.zip

1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

海神之光

完全复现虚拟同步发电机VSG自适应惯量阻尼控制仿真、根轨迹、调节系数对比(Simulink仿真、Matlab代码实现)

内容概要:本文围绕虚拟同步发电机(VSG)的自适应惯量阻尼控制展开深入研究,通过Simulink搭建详细的系统仿真模型,并结合Matlab代码实现控制算法,系统分析了根轨迹特性及不同调节系数对系统动态响应的影响。研究重点在于设计并验证一种能够根据电网频率变化自动调整惯量和阻尼参数的自适应控制策略,有效提升了VSG在面对负载突变或新能源波动时的频率稳定性与抗干扰能力,为高比例可再生能源接入下的电力系统稳定运行提供了关键技术支撑。; 适合人群:具备电力系统分析、自动控制理论及新能源并网技术基础的研究生、科研人员以及从事微电网、分布式能源系统设计与运维的工程技术人员。; 使用场景及目标:①深入理解虚拟同步发电机的工作原理及其在提升电网惯性方面的关键作用;②掌握基于Simulink的电力电子与控制系统建模与仿真方法;③学习并实践惯量和阻尼自适应控制策略的设计流程,通过根轨迹分析手段优化控制器参数,提升系统动态性能。; 阅读建议:建议读者结合所提供的Simulink仿真模型与Matlab代码进行同步操作,重点关注控制器设计逻辑与参数整定过程,通过改变调节系数观察系统响应变化,深化对稳定性判据与控制性能之间关系的理解。

冒泡芳

【雷达信号处理】数字下变频(DDC)(Matlab代码实现)

【雷达信号处理】数字下变频(DDC)(Matlab代码实现)内容概要:本文主要介绍了数字下变频(DDC)技术在雷达信号处理中的应用,并提供了基于Matlab的代码实现。DDC作为现代雷达系统中的关键环节,主要用于将高频接收信号转换为便于后续处理的低频或基带信号,从而提高信号处理效率与精度。文中详细阐述了DDC的基本原理,包括混频、低通滤波和抽取等步骤,并通过Matlab代码展示了其实现过程,帮助读者理解如何在实际工程中构建和调试DDC模块。此外,文档还提及该资源属于一个涵盖多个科研方向的技术资料集合,如路径规划、电力系统优化、机器学习、通信系统仿真等,强调借助成熟工具与方法可提升科研效率。 适合人群:具备一定信号处理基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适用于从事雷达系统设计、通信工程或相关领域研究的人员。 使用场景及目标:① 学习和掌握数字下变频(DDC)在雷达信号处理中的基本原理与实现方法;② 利用Matlab进行算法仿真与验证,辅助完成课程设计、毕业课题或科研项目中的信号处理部分;③ 借助提供的代码框架拓展至其他通信或雷达系统仿真任务中。 阅读建议:此资源以Matlab代码为核心载体,注重理论与实践结合,建议读者在学习过程中同步运行代码并调整参数,观察不同设置对下变频结果的影响,加深对混频泄漏、滤波器设计和采样率变化等问题的理解,从而真正掌握DDC的关键技术细节。

程序猿鑫
评论 5
收藏 已收藏
分享
返回顶部
LilyKingdom668564
Java SE 62,620 成员 307,251 内容