当前位置: 技术问答>java相关
如何实现可拖放移动、并改变大小(200分)
来源: 互联网 发布时间:2015-04-15
本文导语: 请问如何使用Java2D API实现可用鼠标拖动位置,并通过控制点改变大小的方框、菱形、椭圆形等。本人有一简单程序,单在运行时不太正常。请指教: import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.ge...
请问如何使用Java2D API实现可用鼠标拖动位置,并通过控制点改变大小的方框、菱形、椭圆形等。本人有一简单程序,单在运行时不太正常。请指教:
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class ShapeTest
{
public static void main(String[] args)
{
JFrame frame = new ShapeTestFrame();
frame.show();
}
}
class ShapeTestFrame extends JFrame
implements ActionListener
{
private ShapePanel panel;
private JComboBox comboBox;
public ShapeTestFrame()
{
setTitle("ShapeTest");
setSize(300, 300);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
Container contentPane = getContentPane();
panel = new ShapePanel();
comboBox = new JComboBox();
comboBox.addItem(new ArcMaker());
comboBox.addItem(new LineMaker());
comboBox.addItem(new RectangleMaker());
comboBox.addItem(new RoundRectangleMaker());
comboBox.addItem(new EllipseMaker());
comboBox.addItem(new PolygonMaker());
comboBox.addItem(new QuadCurveMaker());
comboBox.addItem(new CubicCurveMaker());
comboBox.addActionListener(this);
contentPane.add(comboBox, "North");
contentPane.add(panel, "Center");
}
public void actionPerformed(ActionEvent event)
{
ShapeMaker shapeMaker = (ShapeMaker)comboBox.getSelectedItem();
panel.setShapeMaker(shapeMaker);
}
}
class ShapePanel extends JPanel
implements MouseListener, MouseMotionListener
{
private Point2D[] points;
private static Random generator = new Random();
private static int SIZE = 6;
private int current;
private ShapeMaker shapeMaker;
public ShapePanel()
{
addMouseListener(this);
addMouseMotionListener(this);
current = -1;
}
public void setShapeMaker(ShapeMaker aShapeMaker)
{
shapeMaker = aShapeMaker;
int n = shapeMaker.getPointCount();
points = new Point2D[n];
for(int i=0; i
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class ShapeTest
{
public static void main(String[] args)
{
JFrame frame = new ShapeTestFrame();
frame.show();
}
}
class ShapeTestFrame extends JFrame
implements ActionListener
{
private ShapePanel panel;
private JComboBox comboBox;
public ShapeTestFrame()
{
setTitle("ShapeTest");
setSize(300, 300);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
Container contentPane = getContentPane();
panel = new ShapePanel();
comboBox = new JComboBox();
comboBox.addItem(new ArcMaker());
comboBox.addItem(new LineMaker());
comboBox.addItem(new RectangleMaker());
comboBox.addItem(new RoundRectangleMaker());
comboBox.addItem(new EllipseMaker());
comboBox.addItem(new PolygonMaker());
comboBox.addItem(new QuadCurveMaker());
comboBox.addItem(new CubicCurveMaker());
comboBox.addActionListener(this);
contentPane.add(comboBox, "North");
contentPane.add(panel, "Center");
}
public void actionPerformed(ActionEvent event)
{
ShapeMaker shapeMaker = (ShapeMaker)comboBox.getSelectedItem();
panel.setShapeMaker(shapeMaker);
}
}
class ShapePanel extends JPanel
implements MouseListener, MouseMotionListener
{
private Point2D[] points;
private static Random generator = new Random();
private static int SIZE = 6;
private int current;
private ShapeMaker shapeMaker;
public ShapePanel()
{
addMouseListener(this);
addMouseMotionListener(this);
current = -1;
}
public void setShapeMaker(ShapeMaker aShapeMaker)
{
shapeMaker = aShapeMaker;
int n = shapeMaker.getPointCount();
points = new Point2D[n];
for(int i=0; i