高分!!!JAVA 鼠绘程序如何一边拖鼠标一边可以看到自己当前画的图形? 请问我的代码应当怎么改?

[复制链接]
查看11 | 回复4 | 2011-5-18 11:23:01 | 显示全部楼层 |阅读模式
我当前画的图形是currentShape(MyShape类型),一旦鼠标释放就表示画好了保存在Shapes数组里。我现在绘制前面已画好的图形没有问题,但是当前画的图形只有鼠标释放才能显示出来……
MyShape,我现在想用它的paintComponent画当前图形但好像此法不行,有什么其他办法吗?
publicclassMyShapeextendsJPanel{
intmyShapeType;//0:直线,1:椭圆,2:矩形
booleanmyFilled;
intx1,y1,x2,y2;
ColormyColor;
publicMyShape()//省略
publicMyShape(ColormyColor,intmyShapeType,booleanmyFilled,intx1,inty1,intx2,inty2)//省略
publicvoidPaint(intx2,inty2)//因为画直线第三四个参数是坐标而椭圆矩形是宽度高度所以这里根据形状修改下第三四个参数
{

switch(myShapeType)
{
case0:
this.x2=x2;
this.y2=y2;
break;
case1:
case2:
this.x2=x2-x1;
this.y2=y2-y1;
break;
}

}
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
g.setColor(myColor);
if(!myFilled)

switch(myShapeType)
{
case0:
g.drawLine(x1,y1,x2,y2);
break;
case1:
g.drawOval(x1,y1,x2,y2);
break;
case2:
g.drawRect(x1,y1,x2,y2);
break;
}
else
switch(myShapeType)
{
case0:
g.drawLine(x1,y1,x2,y2);
break;
case1:
g.fillOval(x1,y1,x2,y2);
break;
case2:
g.fillRect(x1,y1,x2,y2);
break;
}

}

}
这是主绘图面板的鼠标事件:
publicclassDrawPanelextendsJPanel{
……//这个类的代码有省略
publicvoidmousePressed(MouseEventevent)
{
currentShape=newMyShape(currentColor,shapeType,filledShape,event.getX(),event.getY(),event.getX(),event.getY());

}
publicvoidmouseDragged(MouseEventevent)
{

currentShape.Paint(event.getX(),event.getY());//根据当前形状修改第三、四个参数x2,y2
currentShape.repaint();//!!!画当前的图形,为什么画不出???
repaint();//画以前的图形
}
publicvoidmouseReleased(MouseEventevent)
{
shapes[shapeCount]=newMyShape();
shapes[shapeCount]=currentShape;
shapeCount;
repaint();
}
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
for(inti=0;ishapeCount;i)
{
g.setColor(shapes.myColor);
if(!shapes.myFilled)

switch(shapes.myShapeType)
{
case0:
g.drawLine(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
case1:
g.drawOval(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
case2:
g.drawRect(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
}
else
switch(shapes.myShapeType)
{
case0:
g.drawLine(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
case1:
g.fillOval(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
case2:
g.fillRect(shapes.x1,shapes.y1,shapes.x2,shapes.y2);
break;
}

}
}
}
回复

使用道具 举报

千问 | 2011-5-18 11:23:01 | 显示全部楼层
在DrawPanel中添加一个属性:BooleanisDragged=false;
用于判断当前鼠标鼠标是否拖动,并添加相应的set方法:
publicvoidsetIsDragged(Booleandrag)
{
        this.isDragged=drag;
}

然后再paintComponent(Graphicsg)中另外添加一段代码
if(isDragged)
{
        currentShape.paintShape(g);//此shape为鼠标拖动状态下需要绘制的图形
}
然后就是修改鼠标拖动事件部分的代码
publicvoidmouseDragged(MouseEventarg0)
{
currentShape=newMyShape();//需要拖动时绘制的图形
this.setISDragged(true);//把是否拖动属性设置为true
this.repaint();//调用repaint方法
}
此后要在publicvoidmouseReleased(MouseEventevent)事件处理方法中
另外添加第一句代码
this.setIsDragged(false);//把是否拖动的判断重新设置为false

大致就是需要处理这么几步,具体的代码实现就看你的组织了
回复

使用道具 举报

千问 | 2011-5-18 11:23:01 | 显示全部楼层
试过,不行
回复

使用道具 举报

千问 | 2011-5-18 11:23:01 | 显示全部楼层
你是要做一个画图程序吗?可以参考Windows自带的画图工具的那个效果,严格的按照面向对象的原则来设计,Shape类做成接口,不要依赖其他任何类,具体图形类继承Shape,要画什么图形,首先做出选择,点击画直线的按钮之后就表示要画直线,具体图形类千万不能继承swing组件,这样违反了模型与视图分离的原则。尽量提高可维护性、灵活性、可扩展性。
设计良好的结构,绝不会出现太多的switch或if分支语句,建议你学习一下设计模式。
然后,鼠标重绘图形将不再是问题。
回复

使用道具 举报

千问 | 2011-5-18 11:23:01 | 显示全部楼层
哦,我不是要模仿Windows自带的画图工具,是完成作业……大体框架是题目要求的……我就是想知道怎么在鼠标拖拉时也可以显示出正在画的图形:P
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行