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();
}
}
} 展开
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();
}
}
} 展开
2个回答
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询