Java画图板
我做的画图板很简单,就是把工具和颜色选择放到了同一个JFrame里面了
我只实现了画线功能(确切的说是用连续的点拼成的线)
对鼠标事件和各种事件不太懂。
现在只想实现基本的画直线,矩形和椭圆以及橡皮擦和重绘功能
下面是我的代码
可以指点一下,或者直接跟我说任何实现就好,就是鼠标按下去和起来的坐标如何获得
//主类
public class DrawingPad extends JFrame {
//界面设置
private JPanel colorPanel;
private JPanel toolPanel;
private DrawSpace drawPanel;
private JButton buttons[];
private JButton colorButton;
private Color penColor=Color.black;
//画图用的有关变量
private int pointCount=0;
private Point[] points=new Point[10000];
//跟花直线,矩形有关的东西
Point dots[] = new Point[1000];
int dot=0;
int ys=0;
int dx=0;
int flag=0;
int startx,starty,endx,endy,height,width,chanx,chany;
//构造函数
public DrawingPad ()
{//工具板,颜色板以及画板的添加和设置
setLayout(null);
toolPanel=new JPanel();
toolPanel.setBounds(0, 0, 100, 450);
add(toolPanel);
buttons=new JButton[5];
buttons[0]=new JButton("Line");
toolPanel.add(buttons[0]);
buttons[1]=new JButton("Rect");
toolPanel.add(buttons[1]);
buttons[2]=new JButton("Oval");
toolPanel.add(buttons[2]);
ButtonHandler handler = new ButtonHandler();
buttons[3]=new JButton("Eraser");
toolPanel.add(buttons[3]);
buttons[3].addActionListener( handler );
buttons[4]=new JButton("Repaint");
//想实现重绘功能,但失败
buttons[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == buttons[4])
repaint();
}
});
toolPanel.add(buttons[4]);
colorPanel=new JPanel();
colorPanel.setBounds(0, 450, 600, 120);
add(colorPanel);
//颜色选择板
colorPanel.setLayout( new GridLayout());
colorButton=new JButton("Click to Choose the Colr of pen");
colorButton.setFont(new Font("隶书", Font.BOLD,25));
// colorButton.setBorder(BorderFactory.createRaisedBevelBorder());
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
penColor = JColorChooser.showDialog(DrawingPad .this,
"choose a color ", penColor);
if (penColor == null)
penColor = Color.black;
}
});
colorPanel.add(colorButton);
drawPanel=new DrawSpace ();
drawPanel.setBounds(100, 0, 500, 450);
drawPanel.setBackground(Color.white);
add(drawPanel);
createMenuBar();
//创建ArrayList
}//button事件想通过点button实现画矩形的功能,有待实现
private class ButtonHandler implements ActionListener
{
// handle button event
public void actionPerformed( ActionEvent event )
{
// clearRect
} // end method actionPerformed
} // end private inner class ButtonHandler
public void mouseDragged(MouseEvent e)
{
}
//创建菜单
public void createMenuBar()
{
//创建一个JMenuBar放置菜单
JMenuBar menuBar=new JMenuBar();
String[] menuArr={"文件(F)","查看(V)","颜色(C)","帮助(H)"};
String[][] menuItemArr={
{"新建(N)","打开(O)","保存(S)","-","退出(E)"},
{"工具箱(T)","颜料盒(C)"},
{"编辑颜色"},
{"帮助主题","关于"}
};
for(int i=0;i<menuArr.length;i++)
{
JMenu menu=new JMenu(menuArr[i]);
for(int j=0;j<menuItemArr[i].length;j++)
{
if(menuItemArr[i][j].equals("-"))
{
//menu.addSeprator();
}
else
{
JMenuItem menuItem=new JMenuItem(menuItemArr[i][j]);
//menuItem.addActionListener(menuListener);
menu.add(menuItem);
}
}
menuBar.add(menu);
}
this.setJMenuBar(menuBar);
}
//画图板的重写
public class DrawSpace extends JPanel
{//可以实现画连续的点
public DrawSpace()
{
// handle frame mouse motion event
addMouseMotionListener(
new MouseMotionAdapter() // anonymous inner class
{
// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length )
{
points[ pointCount ] = event.getPoint(); // find point
++pointCount; // increment number of points in array
repaint(); // repaint JFrame
} // end if
} // end method mouseDragged
} // end anonymous inner class
); // end call to addMouseMotionListener
} // end PaintPanel constructor
// draw ovals in a 4-by-4 bounding box at specified locations on window
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // clears drawing area
// draw all points in array
for ( int i = 0; i < pointCount; i++ )
{g.fillOval( points[ i ].x, points[ i ].y, 4, 4 );
g.setColor(penColor);}
} // end method paintComponent
}
}