Mình mới học Java có chút hà.Ông thầy bảo về nhà thử viết 1 đoạn code tìm kiếm file trong ổ cứng.Và mình làm như sau(à tất cả mình viết trong cùng 1 class). Đầu tiên mình viết phần main như sau Mã: public static void main(String[] args) { String search = "conmgr.log"; String where = "c:/"; File f = new File(where); if (f.isDirectory()) { Directories(f, search); } else { Files(where, search); } } Với biến search là tên file cần tìm và biến where là nơi bắt đầu tìm.Mình cho nó kiểm tra xem nếu là Folder hay File thì sẽ gọi tới 2 phương thức Directories và Files. 2 phương thức đó như vầy Mã: public static void Directories(File path, String search) { String Dir[] = path.list(); for (int i = 0; i < Dir.length; i++) { File f1 = new File(Dir[i]); if (f1.isFile()) { if (f1.equals(search)) { System.out.println("Tim thay"); break; } } else { Directories(f1, search); } } } và Mã: public static void Files(String filename, String search) { if (filename.equals(search)) { System.out.println("OK"); } } V2 mình gặp lỗi thế này NullPointerException ở các dòng Mã: for (int i = 0; i < Dir.length; i++) { Mã: Directories(f1, search); Mã: Directories(f, search); Ai giúp mình với,mình mới học nên đừng ai cười nha []
Code được sửa lại rồi bạn xem coi mình đã thiếu sót ở đâu, gọi đệ quy tìm kiếm hơi lâu, nếu nó nằm ở những folder có thứ tự ABC ở cuối bảng chữ cái... Mã: import java.io.*; public class ListFile { public static void Directories(File path, String search) { String Dir[] = path.list(); int i; for (i = 0; i < Dir.length; i++) { File f1 = new File(path.getPath()+"/"+Dir[i]); System.out.println("Line: "+f1.getPath()); if (f1.isFile()) { if (f1.getName().equals(search)) { System.out.println("Tim thay"); System.out.println(f1.getPath()); System.exit(0); break; } } else { Directories(new File(f1.getPath()), search); } } } public static void Files(String filename, String search) { if (filename.equals(search)) { System.out.println("OK"); } } public static void main(String[] args) { String search = "baihat.txt"; String where = "D:/"; File f = new File(where); if (f.isDirectory()) { Directories(f, search); } else { Files(where, search); } } }
Cám ơn bạn,mình cũng vừa làm ra vào sáng này.Bạn nói đệ qui làm lâu,vậy vui lòng bày mình thêm cách khác với^_^
Vâng, cách sắp xếp theo thứ tự folder ABC thì gặp vấn đề như đã nêu ở trên, cho nên muốn nhanh hơn mình có thể sắp xếp dựa vào tiêu chí khác như: dung lượng file, ngày tạo file, sắp xếp abc theo phần mở rộng, hihi...cái này đòi hỏi bạn phải biết về cấu trúc dữ liệu 1 chút (Cây nhị phân là 1 ví dụ)...
Hix,mới học chút chút mà Y_Y,nếu giỏi thì đâu có hỏi mí cái này. Greek này,mình có 1 bài hơi khó,k biết bạn có thể hướng dẫn sơ cho mình k?Nếu đc mình sẽ gửi qua pm.
Được! Bạn gởi đi, lần sau có gì cứ post lên đây, nếu mình k giúp được còn nhiều thành viên khác cũng có thể giúp được mà...
Muốn search 1 file nào đó trên toàn bộ X: hay là là chỉ tại root folder X: Nếu bạn tìm trên toàn bộ HDD thì bạn có thể xài tree node, expanse từ root folder tới tận folder cuối cùng trên cái X: Bạn có thể lên http://www.codeproject.com/ để tham khảo thêm.
Giờ mình lại thắc mắc 1 cái mới. Mình viết 1 đoạn code nhỏ mục đích là vẽ hình,có 4 button,mục đích là khi click vào button nào thì nó sẽ gán cho biến "hinh" 1 giá trị tương ứng. Mã: import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Test extends JFrame implements ActionListener { String hinh; Test() { String hinh; //Pane getContentPane().setLayout(null); setTitle("Test"); setSize(800, 2000); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //Tieu de 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); //Button1 jb1.setText("Rectangle"); jb1.setBounds(new Rectangle(32, 30, 160, 30)); getContentPane().add(jb1); jb1.addActionListener(this); //Button2 jb2.setText("Cricle"); jb2.setBounds(new Rectangle(224, 30, 160, 30)); getContentPane().add(jb2); jb2.addActionListener(this); //Button3 jb3.setText("Polygon"); jb3.setBounds(new Rectangle(416, 30, 160, 30)); getContentPane().add(jb3); jb3.addActionListener(this); //Button4 jb4.setText("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); } //paintfield JLabel jlb1 = 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(); public void actionPerformed(ActionEvent e) { if (e.getSource() == jb1) { hinh = "r"; } else if (e.getSource() == jb2) { hinh = "c"; } else if (e.getSource() == jb3) { hinh = "p"; } else if (e.getSource() == jb4) { hinh = "e"; } System.out.println(hinh); } public static void main(String[] args) { Test t = new Test(); } } Bây giờ mình bị kẹt vài thứ 1/Mấy cái đường phân chia mình k biết tạo ra thế nào nên làm 1 cái lablel,sau đó thì tạo border,cuối cùng thì chó nó hẹp lại,cách này chuối quá.Có cách nào thức tế hơn k? 2/Mình cần tạo 1 cái Jmenulist mà k biết tạo. 3/Cũng là điều quan trọng nhất.Khi mở lên bạn sẽ thấy phía dưới cùng trống rất nhiều,chổ ấy mình muốn tạo ra paintfield và có ý định là set lại đỉnh tọa độ của paintfield là ( 0 , 0 ).(tức là khi mouseover paintfield thì mouse sẽ nhận tọa độ theo paintfield) À đây là bài tập của mình nên mình rất mong và rất cám ơn khi các bạn chỉ hướng đi cho mình,chứ đừng làm dùm^_^.
Trả lời bạn như sau: [*]Câu 1:Có thể tạo ra các đường phân chia bằng hàm drawLine(x1,y1,x2,y2) như sau: Mã: public void paint(Graphics g) { g.setColor(Color.RED); g.drawLine(x1,y1,x2,y2); } [*]Câu 2: Ví dụ để tạo 1 menu có duy nhất menu File, và các thành phần của menu File gồm: New, Save, Exit. Code như sau: Mã: JMenuBar mb=new JMenuBar(); JMenu fileMenu=new JMenu("File"); JMenuItem newItem=new JMenuItem("New"); JMenuItem saveItem=new JMenuItem("Save"); JMenuItem exitItem=new JMenuItem("Exit"); fileMenu.add(newItem); fileMenu.add(saveItem); fileMenu.add(exitItem); mb.add(fileMenu); getContentPane().setMenubar(mb); [B]Để bắt được sự kiện click menuItem cũng dùng ActionListener[/B] [*]Câu 3: Để bắt chuột implements Interface MouseListener sau đó override 2 hàm bắt sự kiện: Mã: void mouseClicked(MouseEvent e) {}//khi click chuột //dùng e.getX(); e.getY(); để lấy tọa độ void mouseReleased(MouseEvent e) {}//khi thả chuột //các hàm còn lại viết như bên dưới, nếu không phải dùng MouseAdapter để chỉ ra chỉ implements 2 hàm trên thôi. void mouseEntered(MouseEvent e) {} void mouseExited(MouseEvent e) {} void mousePressed(MouseEvent e) {}
Mã: public void paint(Graphics g) { g.setColor(Color.RED); g.drawLine(x1,y1,x2,y2); } Cái này mình có sử dụng qua rồi nhưng bị 1 lỗi k hiểu nỗi là lúc ấy tất cả các thành phần trc mất tiêu,khi rê chuột tới thì chúng mới hiện ra. Mã: JMenuBar mb=new JMenuBar(); JMenu fileMenu=new JMenu("File"); JMenuItem newItem=new JMenuItem("New"); JMenuItem saveItem=new JMenuItem("Save"); JMenuItem exitItem=new JMenuItem("Exit"); fileMenu.add(newItem); fileMenu.add(saveItem); fileMenu.add(exitItem); mb.add(fileMenu); getContentPane().setMenubar(mb); Cái này hình như bị lỗi chổ " getContentPane().setMenubar(mb); ".Mình đã viết như thế lại như thế này Mã: import java.awt.*; import javax.swing.*; public class Untitled1 extends JFrame { Untitled1() { //Pane getContentPane().setLayout(null); setTitle("Test"); setSize(800, 2000); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //menu fileMenu.add(newItem); fileMenu.add(saveItem); fileMenu.add(exitItem); mb.add(fileMenu); getContentPane().setMenubar(mb); } JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem newItem = new JMenuItem("New"); JMenuItem saveItem = new JMenuItem("Save"); JMenuItem exitItem = new JMenuItem("Exit"); public static void main(String[] args) { Untitled1 f = new Untitled1(); } Có điều hình như Jmenu và Jcombobox khác nhau phải hem?:P Ngoài ra mình muốn hỏi có cách nào lồng phương thức paint vào 1 phương thức khác k?Vì mình muốn tạo ra 1 phương thức nhận giá trị vào để tạo hình ví dụ như vầy(code đoạn này lỗi là cái chắc vì mình lồng vào k đcX() Đại khái ý mình là thế này Mã: int x1,x2,y1,y2; public Untitled1(int x1,int y1,int x2,int y2) { public void paint(Graphics g){ g.drawOval(x1,y1,x2,y2); } } Với lại còn cái paintfield mình hỏi ơ trên làm thía nào gợi ý mình với^_6 Có lẽ mọi người cảm thấy quái vì mình học tùm lum mà chả biết cái gì,vì mình đang đc học theo phương pháp học trải đều tất cả rồi mới đi sâu vào từng thứ sau^_^ Cám ơn mọi người
Dùng hàm repaint(); để vẽ lại khi thực hiện thao tác, sẽ k bị thế nữa. Cái trên mình chỉ viết code snippet nếu bê nguyên xi vào thì chạy k được đâu nhé. Đúng vậy !!! Khác nhau hoàn toàn. Dùng phương thức repaint() ở các phương thức muốn lồng vào; khi đó nó tự dộng gọi lại hàm paint() Khi click vào button sẽ dùng đẻ xác định loại hình muốn vẽ(drawRect, drawOval....), việc còn lại là xác định tọa độ và kích thước của hình bằng e.getY() và e.getX, với x1,y1: là tọa độ lúc click chuột x2,y2: là tọa độ lúc thả chuột chiều rộng= x2-x1; chiều cao=y2-y1;
Xem giúp mình tại sao mình vẫn bị mất hình,trừ khiclick trúng nó hoặc réize thì mới thấy. Làm sao để sau khi vẽ thì hình có màu background luôn?Mình k biết cách xử lý cái polygon thế nào,ai giúp mình với. Mã: 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 = 0; int y1 = 0; int x2 = 0; int y2 = 0; int x3 = 0; int y3 = 0; Test() { String hinh; //Pane getContentPane().setLayout(null); setTitle("Test"); setSize(800, 600); setMaximizedBounds(new Rectangle(800, 700)); setVisible(true); 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); } 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) { } 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() ) { System.out.println("OK"); repaint(); } else { System.out.println("Fail"); } } public void paint(Graphics g) { int w, l, x, y; 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 } }
Ở constructor đem 2 hàm này xuống để gọi đến sau cùng: Mã: setVisible(true); setSize(800,600); //theo đúng thứ tự này nếu không cũng bị mất tiếp. Cái này trong demo đã làm được rồi cơ mà(dùng setColor + fillRect???) Đa giác được xác định như thế này Polygon(int[] xpoints, int[] ypoints, int npoints) xpoints: mảng các tọa độ x ypoints: mảng các tọa độ y npoints: số lượng các đỉnh, khi chọn vẽ ở chế độ này thì dùng mouseClicked để xác định tọa độ, lưu e.getX () vào xpoints, e.getY() vào ypoints...
Mình xử lý polygon đc rồi nhưng theo kiểu là vẽ đường thẳng rồi nối lại,khi nào double click thì nó sẽ nối cạnh cuối cùng vì thế k làm fill cho polygon đc. Còn cách bạn nói mình hem làm đc,Greek có thể minh họa giúp hem? Mình đã để cuối cùng(cuối phương thức test() mà sao nó vẫn k hiện lên,chán qua đi Y_Y).Xem giúp mình nhé ^_^ Với lại mình bị cái lỗi là vẽ ra ngoài khung vẽ,mình chỉ có thể định vị cho chuột hoạt động trong phạm vi cho phép thôi,nhưng đôi khi hình vẫn bị lố ra ngoài nếu vẽ to quá.Ví dụ thế này Mình gửi lại đoạn code gần đây nhất Mã: 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) {} } } À còn 1 cái rất ngớ ngẩn mà mình k hiểu nổi,ở trên mình định khai báo thêm 2 biến là x4,y4 để xài ở cái vẽ polygon,nhưng thật ngớ ngẩn làm sao ấy x4,y4 thì nó báo lỗ là biến chưa chuẩn bị,cái biến nó mờ đi khi mình gán giá trị sửa thành a và aa thì k báo lỗi nữa.
Click chuột trên vùng vẽ để xác định tọa độ đỉnh đa giác, khi muốn vẽ click vào button Draw. Done ! Mã: import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.*; import javax.swing.*; public class fillPolygon extends JFrame implements ActionListener,MouseListener { JButton btnDraw= new JButton ("Draw"); int max=50; int []toadoX=new int [max]; int []toadoY=new int[max]; int index=0; String shape=""; fillPolygon() { super("Draw Polygon"); getContentPane().setLayout(new FlowLayout()); getContentPane().add(btnDraw); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnDraw.addActionListener(this); addMouseListener(this); setSize(500,500); setVisible(true); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) { toadoX[index]=e.getX(); toadoY[index]=e.getY(); index++; repaint(); } public void actionPerformed(ActionEvent e) { shape="polygon"; repaint(); } public static void main(String[]a) { new fillPolygon(); } public void paint(Graphics g) { super.paint(g); if(shape.equals("polygon")) { g.setColor(Color.BLUE); g.drawPolygon(toadoX,toadoY,index); } } } Hic....Nhìn kỹ lại đi bạn, ban vẫn chưa xóa setSize(800,600); ở đầu constructor. Xóa luôn đi nhé. Cái demo của bạn mình chép về k bị lỗi này????? Bạn chú ý xem lại hàm paint đã khai báo x4,y4 trùng với biến của class, xóa x4y4 cục bộ này đi là hết.
hàm super() là gì thế mình chưa học. Cũng hơi hơi hiểu minh họa của Greek roài. Mà Greek nà,có cách nào giới hạn hình vẽ trong 1 khoảng nào k?Ví dụ mình chỉ muốn hình vẽ đc trong khoảng từ (0,0) đến (800,600).Nếu vẽ lồ ra ngoài thì những đoạn lố ra sẽ k hiện lên. À,tại sao mọi người thích dùng flowlayout thế?Nó có lợi gì k vậy?Tại sao lại k dùng null hay gridlayout để tùy biến đặt đối tượng thoải mái hơn?
hàm super() được gọi mỗi lần để vẽ lại tất cả các obj trong JFrame. Để giới hạn nét vẽ bạn tìm hiểu cách sử dụng lớp Canvas trong Java. FlowLayout mình sử dụng vì nó là Layout đơn giản nhất hơn nữa demo chỉ có 1 button Draw thì không cần dùng đến các Layout phức tạp khác, cũng tùy cách bố trí trên Frame mà chọn Layout cho phù hợp.