定义:存储一系列相同类型数据的集合
一维数组:
定义格式:
类型[] 变量名 = new 类型[10];
类型[] 变量名 = new 类型[]{值,值,.....};
类型[] 变量名 = {值,值,.....};
访问数组:变量名[指定索引下标]
获取数组大小:变量名.length;
二维数组
类型[][] 变量名 = new 类型[3][5];
类型[][] 变量名 = new 类型[][]{{值,值,.....},{值,值,.....},{值,值,.....}};
类型[] 变量名 = {{值,值,.....},{值,值,.....},{值,值,.....}};
访问:变量名[行][列]
获取数组总行数:变量名.length;
获取数组总列数:变量名[指定行].length;
2.布局管理器
LayoutManager 布局管理器的父类(接口),所有布局类都是此类的实现类
边框布局
3.颜色选择
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class DrawBoardFrame extends JFrame {
//全局化颜色
public Color color;
public static void main(String[] args) {
DrawBoardFrame dbf = new DrawBoardFrame();
dbf.initFrame();
}
public void initFrame() {
// 实现的逻辑
// 构造窗体对象
// 设置窗体默认关闭方式
this.setDefaultCloseOperation(3);
// 设置大小
this.setSize(600, 600);
// 设置标题
this.setTitle("画图板工具");
// 设置窗体布局
this.setLayout(new BorderLayout());
// JPanel对象 面板,容器组件
// 容器组件:组件上可以添加组件 JFrame JPanel JScrollPanel
// 非容器组件:组件上不能添加组件 JButton JRadioButton JLabel JTextField
JPanel jpLeft = new JPanel();
JPanel jpDown = new JPanel();
JPanel jpCenter = new JPanel();
// 设置大小
Dimension dimen = new Dimension(80, 70);
jpLeft.setPreferredSize(dimen);
jpDown.setPreferredSize(dimen);
// jpCenter.setPreferredSize(dimen);
// 设置背景颜色
jpLeft.setBackground(Color.green);
jpDown.setBackground(Color.blue);
jpCenter.setBackground(Color.white);
// 添加到窗体指定位置
this.add(jpLeft, BorderLayout.WEST);
this.add(jpDown, BorderLayout.SOUTH);
this.add(jpCenter, BorderLayout.CENTER);
/***************************************** 功能单选按钮**********************************/
//定义变量数组
String[] command = {"wujiao","create","eraser","penqi",
"getColor","bigger","pencil","brush",
"pengtong","word","line","quxian","rect",
"duobian","oval","ovalRect"};
// 按钮组类对象,实现单选
ButtonGroup bg = new ButtonGroup();
// 循环创建对象
for (int i = 0; i < command.length; i++) {
JRadioButton jrb = new JRadioButton();
jrb.setActionCommand(command[i]);
jpLeft.add(jrb);
bg.add(jrb);
//创建图片对象
ImageIcon icon1 = new ImageIcon("images/draw"+i+".jpg");
ImageIcon icon2 = new ImageIcon("images/draw"+i+"-1.jpg");
ImageIcon icon3 = new ImageIcon("images/draw"+i+"-2.jpg");
ImageIcon icon4 = new ImageIcon("images/draw"+i+"-3.jpg");
//设置按钮图片
jrb.setIcon(icon1);
jrb.setRolloverIcon(icon2);
jrb.setPressedIcon(icon3);
jrb.setSelectedIcon(icon4);
}
/*************************************颜色选择按钮********************************************/
JButton jbColor1 = new JButton();
JButton jbColor2 = new JButton();
jbColor1.setBackground(Color.red);
jbColor2.setBackground(Color.black);
Dimension dimen1 = new Dimension(30,30);
jbColor1.setPreferredSize(dimen1);
jbColor2.setPreferredSize(dimen1);
jpDown.add(jbColor1);
jpDown.add(jbColor2);
//给颜色选择按钮添加监听器 匿名内部类
ActionListener action = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//获取事件源
JButton jb = (JButton)e.getSource();
//获取按钮本身背景色
color = jb.getBackground();
}
};
jbColor1.addActionListener(action);
jbColor2.addActionListener(action);
// 设置窗体可见
this.setVisible(true);
// 获取画笔
Graphics g = jpCenter.getGraphics();
// 给窗体添加鼠标监听器
DrawListener ml = new DrawListener(g, bg,this);
jpCenter.addMouseListener(ml);
}
}
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
public class DrawListener implements MouseListener {
public Graphics g;
public ButtonGroup bg;
public int x1, y1, x2, y2;
public DrawBoardFrame dbf;
public DrawListener(Graphics g,ButtonGroup bg,DrawBoardFrame dbf) {
this.g = g;
this.bg = bg;
this.dbf = dbf;
}
// 鼠标按下自动执行
public void mousePressed(MouseEvent e) {
// 保存当前按下点的坐标
x1 = e.getX();
y1 = e.getY();
//设置画笔的颜色
g.setColor(dbf.color);
}
// 鼠标释放自动执行
public void mouseReleased(MouseEvent e) {
// 保存当当前释放点的坐标
x2 = e.getX();
y2 = e.getY();
// 确定按钮组中,被选中的按钮对象是哪个?
// 获取按钮组中被选中的按钮对象
ButtonModel bm = bg.getSelection();
String command = bm.getActionCommand();
// 在按下点和释放点之间绘制图形
if("line".equals(command)){
g.drawLine(x1, y1, x2, y2);
}else if("rect".equals(command)){
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(y2 - y1));
}else if("oval".equals(command)){
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(y2 - y1));
}
}
// 鼠标点击自动执行
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}