Java面向对象续
实现桌面画图形
创建父类
import java.awt.Color;
import java.awt.Graphics;
/**
* 图形(抽象类, 不能实例化)
*
* @author
*
*/
public abstract class Shape {
protected int x1; // 起点的横坐标
protected int y1; // 起点的纵坐标
protected int x2; // 终点的横坐标
protected int y2; // 终点的纵坐标
protected Color color; // 颜色
/**
* 绘图(抽象方法留给子类重写)
*
* @param g 画笔
*/
public abstract void draw(Graphics g);
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public void setColor(Color color) {
this.color = color;
}
}
创建线条类
import java.awt.Graphics;
/**
* 线条
* @author
*
*/
public class Line extends Shape {
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
创建矩形类
import java.awt.Graphics;
/**
* 矩形
* @author jackfrued
*
*/
public class Rectangle extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x2 - x1);
int height = Math.abs(y2 - y1);
int x = x1 < x2? x1 : x2;
int y = y1 < y2? y1 : y2;
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
创建椭圆类
import java.awt.Graphics;
/**
* 椭圆
* @author jackfrued
*
*/
public class Oval extends Shape {
@Override
public void draw(Graphics g) {
int width = Math.abs(x2 - x1);
int height = Math.abs(y2 - y1);
int x = x1 < x2? x1 : x2;
int y = y1 < y2? y1 : y2;
g.setColor(color);
g.drawOval(x, y, width, height);
}
}
创建实心正方形类
import java.awt.Color;
import java.awt.Graphics;
public class Scrawl extends Shape {
private static final int SIZE = 8;
private int cx, cy;
public Scrawl(int cx, int cy) {
this.cx = cx;
this.cy = cy;
}
@Override
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(cx - SIZE / 2, cy - SIZE / 2, SIZE, SIZE);
}
}
我的工具箱
import java.awt.Color;
/**
* 自定义工具类
* @author
*
*/
public final class MyUtil {
private MyUtil() {
}
/**
* 产生指定范围的随机整数
* @param min 最小值(闭区间)
* @param max 最大值(闭区间)
* @return 指定范围的随机整数
*/
public static int random(int min, int max) {
return (int) (Math.random() * (max - min + 1) + min);
}
/**
* 生成随机颜色
* @return Color对象
*/
public static Color randomColor() {
int r = random(0, 255);
int g = random(0, 255);
int b = random(0, 255);
return new Color(r, g, b);
}
}
用户类
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
绘图窗口
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import com.lovoinfo.shape.Line;
import com.lovoinfo.shape.Oval;
import com.lovoinfo.shape.Rectangle;
import com.lovoinfo.shape.Scrawl;
import com.lovoinfo.shape.Shape;
import com.lovoinfo.util.MyUtil;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
type = e.getActionCommand();
}
}
private BufferedImage offImage = new BufferedImage(600, 600, 1);
private JButton lineButton, ovalButton, rectButton, scrawlButton;
private String type = "Scrawl";
private List<Shape> shapeList = new ArrayList<Shape>();
private Shape shape = null;
private boolean isMouseLeftButtonDown = false;
private boolean isScrawlMode = true;
private LoginFrame loginFrame = null;
public MainFrame(LoginFrame f) {
this.loginFrame = f;
this.setTitle("绘图窗口");
this.setSize(600, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
lineButton = new JButton("Line");
ovalButton = new JButton("Oval");
rectButton = new JButton("Rect");
scrawlButton = new JButton("Scrawl");
ActionListener handler = new ButtonHandler();
lineButton.addActionListener(handler);
ovalButton.addActionListener(handler);
rectButton.addActionListener(handler);
scrawlButton.addActionListener(handler);
this.setLayout(null);
lineButton.setBounds(100, 10, 60, 30);
this.add(lineButton);
ovalButton.setBounds(170, 10, 60, 30);
this.add(ovalButton);
rectButton.setBounds(240, 10, 60, 30);
this.add(rectButton);
scrawlButton.setBounds(310, 10, 80, 30);
this.add(scrawlButton);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
loginFrame.setVisible(true);
}
});
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
isMouseLeftButtonDown = e.getButton() == MouseEvent.BUTTON1;
if(isMouseLeftButtonDown) {
isScrawlMode = false;
if(type.equals("Line")) {
shape = new Line();
}
else if(type.equals("Oval")) {
shape = new Oval();
}
else if(type.equals("Rect")){
shape = new Rectangle();
}
else {
isScrawlMode = true;
}
if(shape != null) {
int x = e.getX();
int y = e.getY();
shape.setX1(x);
shape.setY1(y);
shape.setX2(x);
shape.setY2(y);
shape.setColor(MyUtil.randomColor());
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(isMouseLeftButtonDown && shape != null) {
shapeList.add(shape);
shape = null;
isMouseLeftButtonDown = false;
}
}
});
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if(isMouseLeftButtonDown && shape != null) {
shape.setX2(x);
shape.setY2(y);
}
else if(isScrawlMode) {
Shape temp = new Scrawl(x, y);
shapeList.add(temp);
}
repaint();
}
});
}
@Override
public void paint(Graphics g) {
Graphics g3 = offImage.getGraphics();
super.paint(g3);
for(Shape temp : shapeList) {
temp.draw(g3);
}
if(shape != null) {
shape.draw(g3);
}
g.drawImage(offImage, 0, 0, null);
}
}
用户登录
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class LoginFrame extends JDialog {
private JLabel uidLabel, pwdLabel;
private JTextField uidField;
private JPasswordField pwdField;
private JButton loginButton, cancelButton;
private List<User> userList = new ArrayList<User>();
public LoginFrame() {
this.setTitle("用户登录");
this.setSize(300, 180);
this.setResizable(false);
this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(HIDE_ON_CLOSE);
initComponents();
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
private boolean checkLogin(String username, String password) {
for(User user : userList) {
if(user.getUsername().equals(username)) {
return user.getPassword().equals(password);
}
}
return false;
}
private void initComponents() {
userList.add(new User("admin", "123456"));
userList.add(new User("hellokitty", "1qaz2wsx"));
userList.add(new User("jack", "123123"));
uidLabel = new JLabel("用户名: ");
pwdLabel = new JLabel("密码: ");
uidField = new JTextField();
pwdField = new JPasswordField();
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
if(checkLogin(username, password)) {
LoginFrame.this.setVisible(false);
new MainFrame(LoginFrame.this).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误", 0);
}
}
});
cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setLayout(null);
uidLabel.setBounds(40, 20, 50, 30);
pwdLabel.setBounds(40, 60, 50, 30);
uidField.setBounds(100, 25, 150, 20);
pwdField.setBounds(100, 65, 150, 20);
loginButton.setBounds(80, 100, 60, 30);
cancelButton.setBounds(160, 100, 60, 30);
this.add(uidLabel);
this.add(pwdLabel);
this.add(uidField);
this.add(pwdField);
this.add(loginButton);
this.add(cancelButton);
}
}
多用户登录
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.lovoinfo.ui.MainFrame;
@SuppressWarnings("serial")
public class LoginFrame extends JFrame {
private JLabel uidLabel, pwdLabel;
private JTextField uidField;
private JPasswordField pwdField;
private JButton loginButton, cancelButton;
private List<User> userList= new ArrayList<User>();
public LoginFrame() {
this.setTitle("用户登录");
this.setSize(300, 180);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initComponents();
}
public boolean checkLogin(String username,String password){
for (User user :userList){
if(user.getUsername().equals(username)){
return user.getPassword().equals(password);
}
}
return false;
}
private void initComponents() {
userList.add(new User("admin","123456"));
userList.add(new User("admi","12345"));
userList.add(new User("adm","123"));
uidLabel = new JLabel("用户名: ");
pwdLabel = new JLabel("密码: ");
uidField = new JTextField();
pwdField = new JPasswordField();
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
if(checkLogin(username,password)){
new MainFrame(null).setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误","错误",0);
}
}
});
cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setLayout(null);
uidLabel.setBounds(40, 20, 50, 30);
pwdLabel.setBounds(40, 60, 50, 30);
uidField.setBounds(100, 25, 150, 20);
pwdField.setBounds(100, 65, 150, 20);
loginButton.setBounds(80, 100, 60, 30);
cancelButton.setBounds(160, 100, 60, 30);
this.add(uidLabel);
this.add(pwdLabel);
this.add(uidField);
this.add(pwdField);
this.add(loginButton);
this.add(cancelButton);
}
}
MainFrame测试
import javax.swing.UIManager;
import com.lovoinfo.ui.LoginFrame;
public class MainFrameTest {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new LoginFrame().setVisible(true);
}
}