下面这个程序是可以执行的,但是我不知道,这个程序是抓去鼠标点击点然后存为
Ellipse2D,我不知道如何将这个点输入到arraylist中
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
import java.math.*;public class Btriple
{
public static void main (String[] args)
{
MouseFrame frame=new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
*a frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("vorinoi");
setSize(500,600);
//add panel to frame
MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH =300;
public static final int HEIGHT=200;
}
/**
*a panel wiht mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares=new ArrayList();
current=null;
JButton yellowButton = new JButton("画图");
ColorAction yellowAction = new ColorAction(Color.white);
// associate actions with buttons yellowButton.addActionListener(yellowAction);
// add buttons to panel add(yellowButton);
addMouseListener(new MouseHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//draw all squares
g.setColor(Color.blue);
for (int i=0;i<squares.size();i++)
{
g2.draw((Ellipse2D)squares.get(i));
g2.fill((Ellipse2D)squares.get(i));
currentcopy=new Pointer();
// currentcopy.x=event.getPoint().x;
// currentcopy.y=event.getPoint().y;
// currentcopy.flag=true;
// squarescopy.add(currentcopy);
}
}
/**
*finds the first square containing a point.
*@param p a point
*@return the index of the first square the contains p
*/
public Ellipse2D find(Point2D p)
{
for (int i=0;i<squares.size();i++)
{
Ellipse2D r=(Ellipse2D)squares.get(i);
if (r.contains(p))return r;
}
return null;
}
/**
*adds a square to the collection.
*@param p the center of the square
*/
public void add(Point2D p)
{//currentcopy=new Pointer();
double x=p.getX();
//currentcopy.x=p.getX();
double y=p.getY();
// currentcopy.y=p.getY();
// currentcopy.flag=true;
//squarescopy.add(currentcopy);
current=new Ellipse2D.Double(
x-SIDELENGTH/2,
y-SIDELENGTH/2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
}
//end add
/**
*revove s sfquare from the collection/
*@param s the square to remove
*/
private static final int SIDELENGTH=3;
private ArrayList squares;
private Ellipse2D current;
private ArrayList squarescopy;
private Pointer currentcopy;
//the square containing the mouste cursor
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
//add a new square if the cursor isn"t inside a square
current=find(event.getPoint());
if (current==null)
add(event.getPoint());
}
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
} public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
} private Color backgroundColor;
}
}
class Pointer
{
double x;
double y;
boolean flag;
}
Ellipse2D,我不知道如何将这个点输入到arraylist中
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
import java.math.*;public class Btriple
{
public static void main (String[] args)
{
MouseFrame frame=new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
*a frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("vorinoi");
setSize(500,600);
//add panel to frame
MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH =300;
public static final int HEIGHT=200;
}
/**
*a panel wiht mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares=new ArrayList();
current=null;
JButton yellowButton = new JButton("画图");
ColorAction yellowAction = new ColorAction(Color.white);
// associate actions with buttons yellowButton.addActionListener(yellowAction);
// add buttons to panel add(yellowButton);
addMouseListener(new MouseHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//draw all squares
g.setColor(Color.blue);
for (int i=0;i<squares.size();i++)
{
g2.draw((Ellipse2D)squares.get(i));
g2.fill((Ellipse2D)squares.get(i));
currentcopy=new Pointer();
// currentcopy.x=event.getPoint().x;
// currentcopy.y=event.getPoint().y;
// currentcopy.flag=true;
// squarescopy.add(currentcopy);
}
}
/**
*finds the first square containing a point.
*@param p a point
*@return the index of the first square the contains p
*/
public Ellipse2D find(Point2D p)
{
for (int i=0;i<squares.size();i++)
{
Ellipse2D r=(Ellipse2D)squares.get(i);
if (r.contains(p))return r;
}
return null;
}
/**
*adds a square to the collection.
*@param p the center of the square
*/
public void add(Point2D p)
{//currentcopy=new Pointer();
double x=p.getX();
//currentcopy.x=p.getX();
double y=p.getY();
// currentcopy.y=p.getY();
// currentcopy.flag=true;
//squarescopy.add(currentcopy);
current=new Ellipse2D.Double(
x-SIDELENGTH/2,
y-SIDELENGTH/2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
}
//end add
/**
*revove s sfquare from the collection/
*@param s the square to remove
*/
private static final int SIDELENGTH=3;
private ArrayList squares;
private Ellipse2D current;
private ArrayList squarescopy;
private Pointer currentcopy;
//the square containing the mouste cursor
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
//add a new square if the cursor isn"t inside a square
current=find(event.getPoint());
if (current==null)
add(event.getPoint());
}
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
} public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
} private Color backgroundColor;
}
}
class Pointer
{
double x;
double y;
boolean flag;
}
class Pointer
{
double x;
double y;
boolean flag;
}
中添加构造函数不久可以了吗?
pointer(double x,double y, boolean flag){
this.s=x;
this.y=y;
this.flag=flag;
}
arrlist.add(new pointer(x,y,flag)so i think then will be ok!
我试试。
{
double x;
double y;
boolean flag;
}
将它存入到arraylist中,怎么办啊
费了好多周折,谢谢xxd2000(疯子)