Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. /* JEditorPaneFileChooser.java
  2. * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
  3. */
  4. import java.io.*;
  5. import java.nio.*;
  6. import java.nio.charset.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9. import javax.swing.filechooser.*;
  10. import java.util.Scanner;
  11. public class JEditorPaneFileChooser implements ActionListener {
  12. JFrame myFrame = null;
  13. JEditorPane myPane = null;
  14. JScrollPane mySPane = null;
  15. JMenuItem cmdOpen = null;
  16. JMenuItem cmdSave = null;
  17. String dirName = "\\herong\\swing\\";
  18. String fileName = "";
  19.  
  20. public static void main(String[] a) {
  21. (new JEditorPaneFileChooser()).test();
  22. }
  23.  
  24. private void test() {
  25. myFrame = new JFrame("JEditorPane JFileChooser Test");
  26. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. myFrame.setSize(300,400);
  28.  
  29. myPane = new JEditorPane();
  30. myPane.setContentType("text/plain");
  31. myPane.setText(
  32. "CLICK ON \'File\' AND SELECT \'Open\'\nTHEN SELECT \'A-large-practice.in\'");
  33. mySPane = new JScrollPane(myPane);
  34. myFrame.setContentPane(mySPane);
  35.  
  36. JMenuBar myBar = new JMenuBar();
  37. JMenu myMenu = getFileMenu();
  38. myBar.add(myMenu);
  39. myFrame.setJMenuBar(myBar);
  40.  
  41. myFrame.setVisible(true);
  42. }
  43.  
  44. private JMenu getFileMenu() {
  45. JMenu myMenu = new JMenu("File");
  46. cmdOpen = new JMenuItem("Open");
  47. cmdOpen.addActionListener(this);
  48. myMenu.add(cmdOpen);
  49.  
  50. return myMenu;
  51. }
  52.  
  53. public void actionPerformed(ActionEvent e) {
  54. JFileChooser chooser = new JFileChooser();
  55. chooser.setCurrentDirectory(new File(dirName));
  56.  
  57. chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  58.  
  59. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  60. ".in file", "in");
  61. chooser.setFileFilter(filter);
  62.  
  63. Object cmd = e.getSource();
  64. try {
  65. if (cmd == cmdOpen) {
  66. int code = chooser.showOpenDialog(myPane);
  67. if (code == JFileChooser.APPROVE_OPTION) {
  68. File selectedFile = chooser.getSelectedFile();
  69. long ag= selectedFile.length();
  70. fileName = selectedFile.getName();
  71. FileInputStream fis =
  72. new FileInputStream(selectedFile);
  73. InputStreamReader in=
  74. new InputStreamReader(fis, Charset.forName("UTF-8"));
  75. char[] buffer=new char[(int)ag];
  76. int n = in.read(buffer);
  77. String text = new String(buffer, 0, n);
  78. String ans =bag(text);
  79.  
  80. myPane.setText(ans);
  81. in.close();
  82. }
  83. }
  84. } catch (Exception f) {
  85. f.printStackTrace();
  86. }
  87.  
  88. }
  89.  
  90. public static String bag(String input){
  91.  
  92. Scanner ab=new Scanner(input);
  93. int case1,lp=0;
  94. case1 = ab.nextInt();
  95. int total, k;
  96. char[][] chari,m=null,n=null;
  97. int R1[][]=null,B1[][]=null;
  98. String line="",line1="",ghost="",answ="";
  99. int axr=0,minr=0,maxr=0,axrb=0,minrb=0,maxrb=0,xr=0,lr=0,nex=0,xrb=0,lrb=0,nexb=0,yr=0,lr1=0,lr2=0,lr3=0,yrb=0,lr1b=0,lr2b=0,lr3b=0,man=0,min=0,
  100. manb=0,minb=0,rax=0,ray=0,cra=0,raxb=0,rayb=0,crab=0;
  101.  
  102. int t,i,j,x,p,q,p1,g,h,y,w,sumit,sum,RX=0,BX=0;
  103. for (i=1;i<=case1;i++)
  104. {
  105. total = ab.nextInt();
  106. k=ab.nextInt();
  107. chari=new char[total][total];
  108.  
  109. for(x=0;x<total;x++)
  110. {
  111. line = ab.nextLine();
  112. for (y=0;y<total;y++)
  113. { chari[x][y] =line.charAt(y);}
  114.  
  115. }
  116.  
  117.  
  118.  
  119. for (g=0;g<total;g++)
  120. {
  121.  
  122. w=0;
  123.  
  124. for(h=total-1;h>=0;h--)
  125. {
  126. m[g][w]=chari[h][g];
  127. w++;
  128. }
  129. }
  130.  
  131. for (q=0;q<total;q++)
  132. {
  133. p1=total-1;
  134. for (p=total-1;p>=0;p--)
  135. {
  136. if( (m[p][q]=='R') || (m[p][q]=='B'))
  137. {
  138. n[p1][q]=m[p][q];
  139. p--;}
  140. }
  141. }
  142.  
  143. for (sumit=total-1;sumit>=0;sumit--)
  144. {
  145.  
  146. for (sum=0;sum<total;sum++)
  147. {
  148.  
  149. if (n[sumit][sum] == 'R')
  150. {
  151. R1[RX][0]=sumit;
  152. R1[RX][1]=sum;
  153. RX++;
  154. }
  155.  
  156. else if (n[sumit][sum]=='B')
  157. { B1[BX][0] = sumit;
  158. B1[BX][1]= sum;
  159. BX++;
  160. }
  161. }
  162. }
  163.  
  164. axr=R1.length;
  165.  
  166. for (minr=0;minr<axr;minr++)
  167.  
  168. {
  169. xr = R1 [minr][0];
  170.  
  171. for (maxr=minr+1;maxr<axr;maxr++)
  172.  
  173. {
  174.  
  175. if (R1 [maxr][0] == xr)
  176. lr++;}
  177.  
  178. if (lr>=k) nex=1;
  179. if (lr<=k) break;
  180. else lr=0;
  181. }
  182.  
  183. if (nex!=1)
  184. {
  185. for (minr=0;minr<axr;minr++)
  186. {
  187.  
  188. yr=R1[minr][1];
  189.  
  190. for (maxr=minr+1;maxr<axr;maxr++)
  191. {
  192. if (R1[maxr][1]==yr)
  193. lr1++;}
  194.  
  195. if (lr1>=k)nex=1;
  196. if (lr1>=k) break;
  197. else lr1=0;
  198. }
  199.  
  200. if (nex!=1)
  201. {
  202.  
  203. for (man=0;man<axr;man++)
  204.  
  205. {
  206.  
  207. rax=R1[man][0];
  208. ray = R1 [man][1];
  209. cra=1;
  210. for (min=man+1;min<axr;min++)
  211.  
  212. {
  213.  
  214. if ( (R1[min][0]==rax-cra) && (R1[min][1] == ray +cra) )
  215.  
  216. {lr2++;
  217. cra++;}
  218.  
  219. else break;
  220.  
  221. }
  222. cra=1;
  223. if (lr2>=k) nex=1;
  224. if (lr2>=k) break;
  225. else lr2=0;
  226. }
  227.  
  228. if (nex!=1)
  229. {
  230. for (man=0;man<axr;man++)
  231.  
  232. {
  233.  
  234. rax=R1[man][0];
  235. ray = R1 [man][1];
  236. cra=1;
  237. for (min=man+1;min<axr;min++)
  238.  
  239. {
  240.  
  241. if ( (R1[min][0]==rax-cra) && (R1[min][1] == ray -cra) )
  242.  
  243. {lr3++;
  244. cra++;}
  245.  
  246. else break;
  247.  
  248. }
  249. cra=1;
  250. if (lr3>=k) nex=1;
  251. if (lr3>=k) break;
  252. else lr3=0;
  253. }
  254. }
  255. }
  256. }
  257.  
  258. axrb=B1.length;
  259. for (minrb=0;minrb<axrb;minrb++)
  260. {
  261. xrb = B1 [minrb][0];
  262.  
  263. for (maxrb=minrb+1;maxrb<axrb;maxrb++)
  264.  
  265. {
  266.  
  267. if (B1 [maxrb][0] == xrb)
  268. lr++;}
  269.  
  270. if (lrb>=k) nexb=1;
  271. if (lrb<=k) break;
  272. else lr=0;
  273. }
  274.  
  275. if (nexb!=1)
  276. {
  277. for (minrb=0;minrb<axrb;minrb++)
  278. {
  279.  
  280. yr=B1[minrb][1];
  281.  
  282. for (maxrb=minrb+1;maxrb<axrb;maxrb++)
  283. {
  284. if (B1[maxrb][1]==yrb)
  285. lr1b++;}
  286.  
  287. if (lr1b>=k)nexb=1;
  288. if (lr1b>=k) break;
  289. else lr1b=0;
  290. }
  291.  
  292. if (nexb!=1)
  293. {
  294.  
  295. for (manb=0;manb<axrb;manb++)
  296.  
  297. {
  298.  
  299. raxb=B1[manb][0];
  300. rayb = B1 [manb][1];
  301. crab=1;
  302. for (minb=manb+1;minb<axrb;minb++)
  303.  
  304. {
  305.  
  306. if ( (B1[minb][0]==raxb-crab) && (B1[minb][1] == rayb +crab) )
  307.  
  308. {lr2b++;
  309. crab++;}
  310.  
  311. else break;
  312.  
  313. }
  314. crab=1;
  315. if (lr2b>=k) nexb=1;
  316. if (lr2b>=k) break;
  317. else lr2b=0;
  318. }
  319.  
  320. if (nexb!=1)
  321. {
  322. for (manb=0;manb<axrb;manb++)
  323.  
  324. {
  325.  
  326. raxb=B1[manb][0];
  327. rayb = B1 [manb][1];
  328. crab=1;
  329. for (minb=manb+1;minb<axrb;minb++)
  330.  
  331. {
  332.  
  333. if ( (B1[minb][0]==raxb-crab) && (B1[minb][1] == rayb -crab) )
  334.  
  335. {lr3b++;
  336. crab++;}
  337.  
  338. else break;
  339.  
  340. }
  341. crab=1;
  342. if (lr3b>=k) nexb=1;
  343. if (lr3b>=k) break;
  344. else lr3b=0;
  345. }
  346. }
  347. }
  348. }
  349.  
  350. if (nex==1)
  351. {ghost=(nexb==1)?"Both": "Red";}
  352. else
  353. {ghost =(nexb==1)? "Blue":"Neither";}
  354.  
  355. answ= "case1 #" + i+ ": " +ghost+"\n";
  356.  
  357. }
  358.  
  359.  
  360. return answ;
  361. }
  362.  
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement