JavaSwing中画多条线的问题

在面板上画线条,我用list来记录坐标,但是画第二条线时会和之前画的线首尾自动以直线相连,请教怎么解决?publicclassCeshi2extendsJFrame{pu... 在面板上画线条,我用list来记录坐标,但是画第二条线时会和之前画的线首尾自动以直线相连,请教怎么解决?

public class Ceshi2 extends JFrame {
public Ceshi2()
{
Container c = getContentPane();
c.add(new PanelOn());
setTitle("aaa");
setSize(500,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
new Ceshi2();
}
}
class PanelOn extends JPanel
{
JPanel jp1 = new JPanel();
int x,y,xo,yo;
Polygon pg;
boolean bool = false;
private ArrayList<Integer> listX = new ArrayList<Integer>();
private ArrayList<Integer> listY = new ArrayList<Integer>();
public PanelOn()
{
setLayout(new BorderLayout());
add(jp1, BorderLayout.SOUTH);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotion());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
int ox[] = new int[listX.size()];
int oy[] = new int[listX.size()];
for (int i = 0; i < listX.size(); i++)
{
ox[i] = listX.get(i);
oy[i] = listY.get(i);
}
g2d.drawPolyline(ox, oy, listX.size());
if(bool){
g2d.drawPolyline(pg.xpoints, pg.ypoints, pg.npoints);
}
}
class MyMouseListener extends MouseAdapter
{
public void mousePressed(MouseEvent m)
{
x = m.getX();
y = m.getY();
pg = new Polygon();
listX.add(x);
listY.add(y);
pg.addPoint(x, y);
bool = true;
repaint();
}
}
class MyMouseMotion extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent m)
{
x = m.getX();
y = m.getY();
listX.add(x);//每次都记到list里面
listY.add(y);
pg.addPoint(x, y);
repaint();
}
}
}
展开
 我来答
神群
2015-01-04
知道答主
回答量:23
采纳率:0%
帮助的人:12.3万
展开全部
package com.test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Ceshi2 extends JFrame {

public Ceshi2()

{

Container c = getContentPane();

c.add(new PanelOn());

setTitle("aaa");

setSize(500, 400);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String args[]) {

new Ceshi2();

}

}

class PanelOn extends JPanel

{

JPanel jp1 = new JPanel();

int x, y, xo, yo;

Polygon pg;

boolean bool = false;

private ArrayList<ArrayList> drawListX = new ArrayList<ArrayList>();

private ArrayList<ArrayList> drawListY = new ArrayList<ArrayList>();

private ArrayList<Integer> listX = new ArrayList<Integer>();

private ArrayList<Integer> listY = new ArrayList<Integer>();

public PanelOn()

{

setLayout(new BorderLayout());

add(jp1, BorderLayout.SOUTH);

addMouseListener(new MyMouseListener());

addMouseMotionListener(new MyMouseMotion());

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

if(drawTime != 0){

for(int t = 0; t < drawListX.size(); t++){

int ox[] = new int[drawListX.get(t).size()];

int oy[] = new int[drawListX.get(t).size()];

for (int i = 0; i < drawListX.get(t).size(); i++)

{

ox[i] = (Integer)drawListX.get(t).get(i);

oy[i] = (Integer)drawListY.get(t).get(i);

}

g2d.drawPolyline(ox, oy, drawListX.get(t).size());

if (bool) {

g2d.drawPolyline(pg.xpoints, pg.ypoints, pg.npoints);

}
}
}
int ox[] = new int[listX.size()];

int oy[] = new int[listX.size()];

for (int i = 0; i < listX.size(); i++)

{

ox[i] = listX.get(i);

oy[i] = listY.get(i);

}

g2d.drawPolyline(ox, oy, listX.size());

if (bool) {

g2d.drawPolyline(pg.xpoints, pg.ypoints, pg.npoints);

}
}

int drawTime = -1;
class MyMouseListener extends MouseAdapter

{

public void mousePressed(MouseEvent m)

{

drawTime ++;

drawListX.add(listX);

drawListY.add(listY);

listX = new ArrayList<Integer>();

listY = new ArrayList<Integer>();

x = m.getX();

y = m.getY();

pg = new Polygon();

listX.add(x);

listY.add(y);

pg.addPoint(x, y);

bool = true;

repaint();

}

}

class MyMouseMotion extends MouseMotionAdapter

{

public void mouseDragged(MouseEvent m)

{

x = m.getX();

y = m.getY();

System.out.println("x:" + x + ",y:" + y);

listX.add(x);// 每次都记到list里面

listY.add(y);

pg.addPoint(x, y);

repaint();

}

}

}

已解决,你自己看看,懒没加注释,如果不能明白你找我。Q:395690696
bvg_Winnir
2015-01-04 · TA获得超过153个赞
知道小有建树答主
回答量:288
采纳率:50%
帮助的人:256万
展开全部
每次画之前把上一条清空了·再获得当前的鼠标坐标。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式