import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Test extends JFrame implements ActionListener, MouseListener {
String hinh;
Color colorline;
Color colorfill;
String[] mausac = {"Khong su dung", "Do", "Xanh la", "Xanh"};
int x1, y1, x2, y2, x3, y3, a, aa;
int c = 1;
Test() {
String hinh;
//Pane
getContentPane().setLayout(null);
setTitle("Test");
setSize(800, 600);
setMaximizedBounds(new Rectangle(800, 700));
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Label1
jlb1.setText("Be Tap Ve");
jlb1.setHorizontalAlignment(SwingConstants.CENTER);
jlb1.setBounds(new Rectangle(0, 0, 800, 20));
jlb1.setBackground(Color.magenta);
jlb1.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(jlb1);
//Label2
jlb2.setText("Mau sac net ve");
jlb2.setHorizontalAlignment(SwingConstants.CENTER);
jlb2.setBounds(new Rectangle(50, 92, 300, 30));
jlb2.setBackground(Color.magenta);
jlb2.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(jlb2);
//Label3
jlb3.setText("Mau sac hinh ve");
jlb3.setHorizontalAlignment(SwingConstants.CENTER);
jlb3.setBounds(new Rectangle(450, 92, 300, 30));
jlb3.setBackground(Color.magenta);
jlb3.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(jlb3);
//Button1
jb1.setText("Hinh Vuong");
jb1.setBounds(new Rectangle(32, 30, 160, 30));
getContentPane().add(jb1);
jb1.addActionListener(this);
//Button2
jb2.setText("Hinh Tron");
jb2.setBounds(new Rectangle(224, 30, 160, 30));
getContentPane().add(jb2);
jb2.addActionListener(this);
//Button3
jb3.setText("Hinh Da Giac");
jb3.setBounds(new Rectangle(416, 30, 160, 30));
getContentPane().add(jb3);
jb3.addActionListener(this);
//Button4
jb4.setText("Hinh Ellipse");
jb4.setBounds(new Rectangle(608, 30, 160, 30));
getContentPane().add(jb4);
jb4.addActionListener(this);
//line,cach xu ly nay cui bap qua>.<
line.setBounds(new Rectangle(0, 80, 800, 1));
line.setBackground(Color.magenta);
line.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(line);
line2.setBounds(new Rectangle(0, 200, 800, 1));
line2.setBackground(Color.magenta);
line2.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(line2);
line3.setBounds(new Rectangle(400, 80, 1, 120));
line3.setBackground(Color.magenta);
line3.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(line3);
//jcombobox1
jcb1.setBounds(new Rectangle(50, 154, 300, 30));
getContentPane().add(jcb1);
jcb1.getSelectedIndex();
jcb1.addActionListener(this);
//jcombobox2
jcb2.setBounds(new Rectangle(450, 154, 300, 30));
getContentPane().add(jcb2);
jcb2.getSelectedIndex();
jcb2.addActionListener(this);
//paintfield (25, 225) 750 x 325
panel1.setBounds(25, 225, 750, 450);
panel1.setBorder(BorderFactory.createEtchedBorder());
panel1.setBackground(Color.magenta);
getContentPane().add(panel1);
addMouseListener(this);
setVisible(true);
setSize(800, 600);
}
JLabel jlb1 = new JLabel();
JLabel jlb2 = new JLabel();
JLabel jlb3 = new JLabel();
JButton jb1 = new JButton();
JButton jb2 = new JButton();
JButton jb3 = new JButton();
JButton jb4 = new JButton();
JLabel line = new JLabel();
JLabel line2 = new JLabel();
JLabel line3 = new JLabel();
JComboBox jcb1 = new JComboBox(mausac);
JComboBox jcb2 = new JComboBox(mausac);
JLabel panel1 = new JLabel();
public void actionPerformed(ActionEvent e) {
//ActionListener cua button
if (e.getSource() == jb1) {
hinh = "r";
}
if (e.getSource() == jb2) {
hinh = "c";
}
if (e.getSource() == jb3) {
hinh = "p";
}
if (e.getSource() == jb4) {
hinh = "e";
}
//ActionListener cua Combobox line
if (e.getSource() == jcb1) {
JComboBox cb1 = (JComboBox) e.getSource();
String p1 = (String) cb1.getSelectedItem();
if (p1.equals("Do")) {
colorline = Color.red;
} else if (p1.equals("Xanh")) {
colorline = Color.blue;
} else if (p1.equals("Xanh la")) {
colorline = Color.green;
} else {
colorline = null;
}
}
//ActionListener cua Combobox fill
if (e.getSource() == jcb2) {
JComboBox cb2 = (JComboBox) e.getSource();
String p2 = (String) cb2.getSelectedItem();
if (p2.equals("Do")) {
colorfill = Color.red;
} else if (p2.equals("Xanh")) {
colorfill = Color.blue;
} else if (p2.equals("Xanh la")) {
colorfill = Color.green;
} else {
colorfill = null;
}
}
}
//kiem tra da nhap day du thong tin
public boolean style() {
if ((hinh != null) && ((colorline != null) | (colorfill != null))) {
return true;
}
return false;
}
public static void main(String[] args) {
Test t = new Test();
}
public void mouseClicked(MouseEvent e) {
c = e.getClickCount();
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
check();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public Boolean field() {
if ((y1 < 250) | (y2 < 250) | (x2 > 750) | (x1 < 25) | (x2 < 25) |
(y1 > 700) | (y2 > 700)) {
return false;
} else {
return true;
}
}
public void check() {
if (
style() && field()
) {
repaint();
} else {
System.out.println("Fail");
}
}
public void paint(Graphics g) {
try {
int w, l, x, y, x4, y4;
if ((x2 - x1) > 0) {
w = (x2 - x1);
} else {
w = x1 - x2;
x = x1;
x1 = x2;
x2 = x;
}
if ((y2 - y1) > 0) {
l = (y2 - y1);
} else {
l = y1 - y2;
y = y1;
y1 = y2;
y2 = y;
}
if (hinh.equals("r")) {
if (colorfill != null) {
g.setColor(colorfill);
g.fillRect(x1, y1, w, l);
} else {
g.setColor(colorline);
g.drawRect(x1, y1, w, l);
}
}
if (hinh.equals("c")) {
if (w > l) {
if (colorfill != null) {
g.setColor(colorfill);
g.fillOval(x1, y1, w, w);
} else {
g.setColor(colorline);
g.drawOval(x1, y1, w, w);
}
} else {
if (colorfill != null) {
g.setColor(colorfill);
g.fillOval(x1, y1, l, l);
} else {
g.setColor(colorline);
g.drawOval(x1, y1, l, l);
}
}
}
if (hinh.equals("e")) {
if (colorfill != null) {
g.setColor(colorfill);
g.fillOval(x1, y1, w, l);
} else {
g.setColor(colorline);
g.drawOval(x1, y1, w, l);
}
}
//chua xu ly dc polygon
if (hinh.equals("p")) {
g.setColor(colorline);
if ((x1 != x2) && (y1 != y2)) {
a = x1;
aa = y1;
g.drawLine(x1, y1, x2, y2);
x3 = x2;
y3 = y2;
} else {
if (c != 2) {
g.drawLine(x3, y3, x2, y2);
x3 = x2;
y3 = y2;
} else {
g.drawLine(a, aa, x2, y2);
a = x2;
aa = y2;
}
}
}
} catch (Exception e) {}
}
}