对绘图系统进行修整理
添加登录器
package com.lovoinfo.test;
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);
}
}
package com.lovoinfo.ui;
/**
/*登录器与主窗口分段隐藏
*/
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);
}
}
用户类
package com.lovoinfo.ui;
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;
}
}
主窗口
package com.lovoinfo.ui;
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);
}
}
案例提升:创建等边三角形
package com.lovoinfo.shape;
import java.awt.Color;
import java.awt.FlowLayout;
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.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class DrawingFrame extends JFrame {
private JButton cirButton = new JButton("Circle");
private JButton rectButton = new JButton("Rect");
private JButton eqtrButton = new JButton("equtran");
private JButton scrawlButton = new JButton("scrawl");
private BufferedImage bottomImage = new BufferedImage(800, 600, 1);
private List<Shape> shapeList = new ArrayList<Shape>();
private boolean isMouseLeftButtonDown = false;
private boolean isScrawlMode;
private Shape shape = null;
private String type = "";
private class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
type = e.getActionCommand();
}
}
public DrawingFrame(){
this.setTitle("绘图器");
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
ActionListener handler = new ButtonHandler();
this.add(cirButton);
this.add(rectButton);
this.add(eqtrButton);
this.add(scrawlButton);
cirButton.addActionListener(handler);
rectButton.addActionListener(handler);
eqtrButton.addActionListener(handler);
scrawlButton.addActionListener(handler);
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
isMouseLeftButtonDown = e.getButton() == MouseEvent.BUTTON1;
isScrawlMode = false;
if (isMouseLeftButtonDown) {
if (type.equals("Circle")) {
int radius = (int) (Math.random()*600);
shape = new Circle(radius);
}
else if (type.equals("Rect")) {
int width = (int) (Math.random()*600);
int height = (int) (Math.random()*600);
shape = new Rect(width, height);
}
else if (type.equals("equtran")) {
int radius = (int) (Math.random()*600);
shape = new Eqtrangle(radius);
}else{
isScrawlMode = true;
}
if (shape != null) {
shape.setX(e.getX());
shape.setY(e.getY());
Color color = MyUtil.randomColor();
shape.setColor(color);
repaint();
}
}
}
@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.setM(x);
shape.setN(y);
}
else if (isScrawlMode) {
Shape temp = new Scrawl(x, y);
shapeList.add(temp);
}
repaint();
}
});
}
@Override
public void paint(Graphics g) {
Graphics g2 = bottomImage.getGraphics();
super.paint(g2);
for(Shape temp : shapeList){
temp.draw(g2);
}
if (shape !=null) {
shape.draw(g2);
g2.drawString(String.format("周长:%.2f",
shape.perimeter()), shape.getX(), shape.getY());
g2.drawString(String.format("面积:%.2f",shape.area()),shape.getX(), shape.getY()+15);
}
g.drawImage(bottomImage, 0,0, null);
}
public static void main(String[] args){
new DrawingFrame().setVisible(true);
}
}
等边三角形类
package com.lovoinfo.shape;
import java.awt.Graphics;
public class Eqtrangle extends Shape {
private int radius;
private int x1,y1,x2,y2,x3,y3;
public Eqtrangle(int radius) {
this.radius = radius;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
x1 = x;
y1 = y - radius;
x2 = (int) (x - radius*(Math.sqrt(3)/2));
y2 = y + radius/2;
x3 = (int) (x + radius*(Math.sqrt(3)/2));
y3 = y + radius/2;
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y2, x3, y3);
g.drawLine(x1, y1, x3, y3);
}
@Override
public double perimeter() {
return 3*radius*Math.sqrt(3);
}
@Override
public double area() {
return 3*radius*radius*Math.sqrt(3)/4;
}
}