Java 绘制流程图工具问题

编程大胖 2011-12-02 06:27:37
本人Java新手,第一次做相对大一点的程序。

我想画出文本域和画直线,并且可以改变他们的大小位置~我也都实现了,唯一的问题是当我改变线的位置的时候,会把路径中的所有线都画出来~
如果用clearRect()方法的话,文本域就没了~
但是我发现如果移动文本域经过那些线时,线就没了,只剩下我想要的那一个~
不知道有没有什么好方法可以解决?
...全文
303 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inhibitory 2011-12-02
  • 打赏
  • 举报
回复
1. 既然是使用Swing (JPanel)来绘制,那么就要尽量的去使用Swing提供的双缓冲功能,在paintComponent函数中去绘制,而不是使用paint方法,因为使用paint方法绘制还要自己写update方法,而paintComponent不需要
2. 如果绘制的线和文本太多,可以先考虑把所有绘制工作先绘制到一个BufferedImage中,然后再绘制到panel上,这样会快很多,因为每调用一次JPanel的Graphics的draw或者fill方法,占用的资源都比BufferedImage的Graphics的draw和fill方法要多,需要调用更多系统内部的资源

3. clearRect很不好控制,使用这个方法需要自己处理图元相交,与这个rect相交的所有图元都需要重新绘制,Swing默认没有提供这方面的库,得自己实现或者每用一次都要和所有的图元判断一下,如Qt就提供了QGraphicsView和QGraphicsScene框架,使用Binary Space Partition进行碰撞检测,不需要检测所有对象。

4. text绘制的时候线不在了,可能是区域被覆盖了,具体代码没细看

所以楼主最好先去看一下JPanel的paintComponent绘图例子,然后再继续下一步
编程大胖 2011-12-02
  • 打赏
  • 举报
回复
请问能不能说的稍微详细点。。没太理解啊。。
龙四 2011-12-02
  • 打赏
  • 举报
回复
可以对JGraph二次开发
编程大胖 2011-12-02
  • 打赏
  • 举报
回复
小弟代码,新手写的没什么结构性~~求指点
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.*;
import javax.swing.*;

public class MyGUI {
public static void main (String[] args){
FlowChart f = new FlowChart();
}
}

class FlowChart extends JComponent implements MouseListener,MouseMotionListener{
final ArrayList<String> rects = new ArrayList<String>();
final ArrayList<String> lines = new ArrayList<String>();
Point[] textpoints = new Point[50];
Point[] linepoints = new Point[50];
int[] width = new int[50];
int[] height = new int[50];

Graphics g;


int x=10, y=10;
int clickx, clicky;
int movex, movey;
int pressx,pressy;
int draggx,draggy;
int releasex=50,releasey=50;
int item, choice;

JFrame frame = new JFrame();

JPanel menu = new JPanel (new GridLayout(1,4));
JPanel p = new JPanel();
JPanel gp = new JPanel();


JTextArea[] TA = new JTextArea[50];
JTextArea T = new JTextArea(8,40);

JButton Savebutton = new JButton("Save");
JButton Loadbutton = new JButton("Load");
JButton TextArea = new JButton("Text");
JButton Line = new JButton("Connecting Lion");


public FlowChart(){
for(int i =0; i<50;i++)
{
width[i] = 50 ;
height[i] = 50 ;

TA[i] = new JTextArea(8,40);
textpoints[i] = new Point(50,100*(i+1));

linepoints[i] = new Point(500,50*(i+1));
}

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setTitle("Flow Chart Tool!");
frame.setBackground(Color.WHITE);


frame.setLayout(new BorderLayout());

menu.add(Savebutton);
menu.add(Loadbutton);
menu.add(TextArea);
menu.add(Line);
frame.add(menu,BorderLayout.NORTH);

frame.add(p,BorderLayout.CENTER);

frame.setVisible(true);

Savebutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.print("1");
}

});

Loadbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.print("2");
}

});

TextArea.addActionListener(new ActionListener()
{
int x = 0;
public void actionPerformed(ActionEvent e)
{

rects.add("Text"+ x);
x++;
DrawPanel();
}

});

Line.addActionListener(new ActionListener()
{
int y = 0;
public void actionPerformed(ActionEvent e)
{
lines.add("Line"+y);
lines.add("Line"+(y+1));
y=y+2;
DrawPanel();
}

});

p.addMouseListener(this);
p.addMouseMotionListener(this);

p.setLayout(null);
}

public void DrawPanel(){




for(int i=0;i<rects.size();i++)
{
p.add(TA[i]);
TA[i].setBounds(textpoints[i].x, textpoints[i].y, width[i], height[i]);
}

g = p.getGraphics();
Graphics2D g2=(Graphics2D)g;

for(int j=0;j<lines.size();j=j+2)
{
drawAL(linepoints[j].x,linepoints[j].y,linepoints[j+1].x,linepoints[j+1].y,g2);
}
p.add(T);
T.setBounds(0, 0, 700, 700);
p.remove(T);

p.revalidate();

}

/* public void paint(Graphics g)
{
g = p.getGraphics();
Graphics2D g2=(Graphics2D)g;

g.clearRect(0,0,700,700);


for(int j=0;j<lines.size();j=j+2)
{
Line2D.Double line=new Line2D.Double(linepoints[j].x,linepoints[j].y,linepoints[j+1].x,linepoints[j+1].y);
g2.draw(line);
}

}*/
public void mouseClicked(MouseEvent e) {
clickx = e.getX();
clicky = e.getY();
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
pressx = e.getX();
pressy = e.getY();

for(int i =0;i<rects.size();i++)
{
if(Math.abs(textpoints[i].x-pressx)<10 && Math.abs(textpoints[i].y-pressy)<10)//点击文本域左上角移动位置
{
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
item = i;
choice = 1;
}
if(Math.abs(textpoints[i].x+width[i]-pressx)<10 && Math.abs(textpoints[i].y+height[i]-pressy)<10)//点击文本域右下角改变大小
{
p.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
item = i;
choice = 2;
}

for(int j =0;j<lines.size();j=j+2)
{
if(Math.abs(linepoints[j].x-pressx)<5 && Math.abs(linepoints[j].y-pressy)<5)//连接线的位置
{
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
item = j;
choice = 3;
}
if(Math.abs(linepoints[j+1].x-pressx)<5 && Math.abs(linepoints[j+1].y-pressy)<5)//连接线的大小
{
p.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
item = j+1;
choice = 4;
}
}

}
}

public void mouseReleased(MouseEvent e) {
releasex = e.getX();
releasey = e.getY();

p.setCursor(Cursor.getDefaultCursor());
choice = 0;

}

public void mouseDragged(MouseEvent e) {
draggx = e.getX();
draggy = e.getY();
if(choice == 1)
{
textpoints[item].x = draggx;
textpoints[item].y = draggy;
DrawPanel();
}
else if(choice == 2)
{
width[item] = draggx-textpoints[item].x;
height[item] = draggy-textpoints[item].y;
DrawPanel();
}
else if(choice == 3)
{
linepoints[item+1].x = draggx-linepoints[item].x+linepoints[item+1].x;
linepoints[item+1].y = draggy-linepoints[item].y+linepoints[item+1].y;
linepoints[item].x = draggx;
linepoints[item].y = draggy;
DrawPanel();
}
else if(choice == 4)
{
linepoints[item].x = draggx;
linepoints[item].y = draggy;
DrawPanel();
}

}

public void mouseMoved(MouseEvent e) {
movex = e.getX();
movey = e.getY();


}

public static void drawAL(int sx, int sy, int ex, int ey,Graphics2D g2)

{

double H = 6 ; // 箭头高度
double L = 6 ; // 底边的一半
int x3 = 0 ;
int y3 = 0 ;
int x4 = 0 ;
int y4 = 0 ;
double awrad = Math.atan(L / H); // 箭头角度
double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度
double [] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true , arraow_len);
double [] arrXY_2 = rotateVec(ex - sx, ey - sy, - awrad, true , arraow_len);
double x_3 = ex - arrXY_1[ 0 ]; // (x3,y3)是第一端点
double y_3 = ey - arrXY_1[ 1 ];
double x_4 = ex - arrXY_2[ 0 ]; // (x4,y4)是第二端点
double y_4 = ey - arrXY_2[ 1 ];

Double X3 = new Double(x_3);
x3 = X3.intValue();
Double Y3 = new Double(y_3);
y3 = Y3.intValue();
Double X4 = new Double(x_4);
x4 = X4.intValue();
Double Y4 = new Double(y_4);
y4 = Y4.intValue();
// g.setColor(SWT.COLOR_WHITE);
// 画线
g2.drawLine(sx, sy, ex, ey);
// 画箭头的一半
g2.drawLine(ex, ey, x3, y3);
// 画箭头的另一半
g2.drawLine(ex, ey, x4, y4);

}





//计算
public static double [] rotateVec( int px, int py, double ang, boolean isChLen,
double newLen) {

double mathstr[] = new double [ 2 ];
// 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
double vx = px * Math.cos(ang) - py * Math.sin(ang);
double vy = px * Math.sin(ang) + py * Math.cos(ang);
if (isChLen) {
double d = Math.sqrt(vx * vx + vy * vy);
vx = vx / d * newLen;
vy = vy / d * newLen;
mathstr[ 0 ] = vx;
mathstr[ 1 ] = vy;
}
return mathstr;
}


}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧