fork download
  1. import java.awt.Dimension;
  2. import java.awt.Toolkit;
  3. import javax.swing.JFrame;
  4.  
  5. public class menu extends JFrame{
  6. private static final long serialVersionUID = 1L;
  7. centrum centrum;
  8. prawa prawa;
  9. ranking ran;
  10.  
  11. public menu(String title){
  12. super(title);
  13.  
  14. Dimension location = new Dimension(Toolkit.getDefaultToolkit().getScreenSize()); //
  15. setSize(600, 500); //
  16. setResizable(false); //
  17. setLayout(null); // ustawienia okna
  18. setLocation((location.width-600)/2,(location.height-500)/2); //
  19. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
  20.  
  21. prawa = new prawa();
  22. centrum = new centrum();
  23. ran = new ranking(this, prawa);
  24.  
  25. prawa.referencja(centrum);
  26. centrum.referencja(prawa,this,ran);
  27.  
  28. add(prawa);
  29. add(centrum);
  30.  
  31.  
  32. }
  33. public static void main(String[] args) {
  34.  
  35. JFrame j = new menu("Snake");
  36. j.setVisible(true);
  37. }
  38.  
  39. }
  40.  
  41.  
  42. import java.awt.Color;
  43. import java.awt.Component;
  44. import java.awt.Font;
  45. import java.awt.event.MouseAdapter;
  46. import java.awt.event.MouseEvent;
  47.  
  48. import javax.swing.JButton;
  49. import javax.swing.JLabel;
  50. import javax.swing.JPanel;
  51. import javax.swing.JSlider;
  52. import javax.swing.JTextField;
  53. import javax.swing.SwingConstants;
  54.  
  55.  
  56. public class prawa extends JPanel{
  57. private static final long serialVersionUID = 1L;
  58. centrum cen;
  59. JSlider predkosc;
  60. JTextField koniec;
  61. JButton zacznij;
  62. JLabel ilosc;
  63.  
  64. public void referencja(centrum cen){
  65. this.cen=cen;
  66. }
  67.  
  68. public prawa(){
  69.  
  70. this.setBounds(500, 0, 100, 500);
  71. this.setLayout(null);
  72.  
  73. zacznij = new JButton("Zacznij");
  74. zacznij.addMouseListener(new MouseAdapter() {
  75. public void mousePressed(MouseEvent me) {
  76. if(!cen.t.isAlive()==true){
  77. cen.t.start();
  78. zacznij.setVisible(false);
  79. predkosc.setVisible(false);
  80. }
  81. else {
  82. cen.odnowa();
  83. zacznij.setVisible(false);
  84. predkosc.setVisible(false);
  85. }
  86. }
  87. });
  88. zacznij.setAlignmentX(Component.CENTER_ALIGNMENT);
  89. zacznij.setBounds(0, 0, 100, 40);
  90. add(zacznij);
  91.  
  92. JLabel punkty = new JLabel("Punkty:");
  93. punkty.setForeground(Color.LIGHT_GRAY);
  94. punkty.setFont(new Font("Tekton Pro Cond", Font.BOLD, 20));
  95. punkty.setBounds(0, 50, 100, 25);
  96. add(punkty);
  97.  
  98. ilosc = new JLabel("0");
  99. ilosc.setForeground(Color.GREEN);
  100. ilosc.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 20));
  101. ilosc.setBounds(0, 80, 100, 25);
  102. add(ilosc);
  103.  
  104. predkosc = new JSlider();
  105. predkosc.setPaintTicks(true);
  106. predkosc.setPaintLabels(true);
  107. predkosc.setValue(5);
  108. predkosc.setMinimum(1);
  109. predkosc.setMaximum(10);
  110. predkosc.setMajorTickSpacing(1);
  111. predkosc.setBounds(0, 105, 100, 200);
  112. predkosc.setOrientation(SwingConstants.VERTICAL);
  113. predkosc.setEnabled(true);
  114. add(predkosc);
  115.  
  116. }
  117. }
  118.  
  119.  
  120. import java.awt.Color;
  121. import java.awt.Graphics;
  122. import java.util.ArrayList;
  123. import java.util.Random;
  124.  
  125. import javax.swing.JPanel;
  126.  
  127.  
  128. public class centrum extends JPanel implements Runnable{
  129. private static final long serialVersionUID = 1L;
  130.  
  131. int dots=5; // ilosc kropek
  132. int aX, aY; // x i y jabłbka
  133. boolean apple=false;
  134. boolean czysc = false;
  135. boolean ramka=false;
  136. Random r;
  137.  
  138. ArrayList<Integer> x,y;
  139. Thread t;
  140. prawa prawa;
  141. menu menu;
  142. ranking ranking;
  143.  
  144. boolean left=false,right=true,up=false,down=false;
  145.  
  146. public void referencja(prawa prawa, menu menu, ranking ranking){
  147. this.prawa = prawa;
  148. this.menu = menu;
  149. this.ranking = ranking;
  150. }
  151.  
  152. public centrum() {
  153.  
  154. this.setLayout(null);
  155. this.setBounds(0, 0, 500, 500);
  156.  
  157. x = new ArrayList<Integer>();
  158. y = new ArrayList<Integer>();
  159.  
  160. for(int i =0;i<(dots)*10;i+=10){
  161. x.add(100-i);
  162. y.add(250);
  163. }
  164.  
  165. r = new Random();
  166. t = new Thread(this);
  167. }
  168.  
  169. public void run() {
  170. setFocusable(true);
  171. sterowanie sterowanie = new sterowanie(this);
  172. addKeyListener(sterowanie);
  173.  
  174. synchronized (this) {
  175. while(true){
  176. for (int i = dots-1;i>0;i--){ //
  177. x.set(i, x.get(i-1)); //
  178. y.set(i, y.get(i-1)); //
  179. } //
  180. //
  181. if(sterowanie.gora==true){ //
  182. y.set(0, y.get(0)-10); //
  183. } //
  184. if(sterowanie.dol==true){ // ustawianie współrzędnych
  185. y.set(0, y.get(0)+10); //
  186. } //
  187. if(sterowanie.lewo==true){ //
  188. x.set(0, x.get(0)-10); //
  189. } //
  190. if(sterowanie.prawo==true){ //
  191. x.set(0, x.get(0)+10); //
  192. } //
  193.  
  194. if(apple == false){
  195. apple=true;
  196. aX = r.nextInt(47)*10+10;
  197. aY = r.nextInt(44)*10+10;
  198. }
  199. if(aX==x.get(0) && aY==y.get(0)){
  200. apple=false;
  201. x.add(-50);
  202. y.add(-50);
  203. dots++;
  204. prawa.ilosc.setText(Integer.parseInt(prawa.ilosc.getText())+prawa.predkosc.getValue()+"");
  205. }
  206.  
  207. repaint();
  208. try {
  209. Thread.sleep(300/prawa.predkosc.getValue());
  210. } catch (InterruptedException e) {
  211. e.printStackTrace();
  212. }
  213.  
  214. if ( (x.get(0) == 0 || x.get(0) == 490 || y.get(0) == 0 || y.get(0) == 460) || najechanie()){
  215. ramka=false;
  216. try {
  217. czysc = true;
  218. ranking.punkty.setText(prawa.ilosc.getText());
  219. prawa.predkosc.setVisible(true);
  220. prawa.zacznij.setVisible(true);
  221. menu.ran.setVisible(true);
  222.  
  223. wait();
  224. repaint();
  225. prawa.ilosc.setText("0");
  226. Thread.sleep(10);
  227. x.clear(); y.clear();
  228. dots=5;
  229. for(int i =0;i<(dots)*10;i+=10){
  230. x.add(100-i);
  231. y.add(250);
  232.  
  233. dots=5;
  234. apple=false;
  235. sterowanie.lewo=false;sterowanie.prawo=true;sterowanie.dol=false;sterowanie.gora=false;
  236. }
  237. } catch (InterruptedException e) {
  238. e.printStackTrace();
  239. }
  240. }
  241. }
  242. }
  243. }
  244. synchronized void odnowa(){
  245. notify();
  246. }
  247.  
  248. boolean najechanie(){
  249. for (int i = 1;i<dots;i++){
  250. if( (x.get(0).equals(x.get(i))) && y.get(0).equals(y.get(i)) )
  251. return true;
  252. }
  253. return false;
  254. }
  255.  
  256.  
  257. public void paint(Graphics g){
  258.  
  259. if(ramka==false){
  260. for(int i = 0;i<500;i+=10){
  261. g.setColor(Color.BLUE);
  262. g.fillRect(i, 0, 10, 10);
  263. g.fillRect(0, i, 10, 10); // rysowanie granic
  264. g.fillRect(490, i, 10, 10);
  265. g.fillRect(i, 460, 10, 15);
  266. }
  267. }
  268.  
  269. g.setColor(Color.GREEN);
  270. g.fillRect(x.get(0), y.get(0), 10, 10);
  271.  
  272. int k=0;
  273. for(int i=1;i<dots-1;i++){
  274. if(i>125){
  275. k=i-125;
  276. g.setColor(new Color(255-k*2,k*2,255-k*2));
  277. }
  278. else{
  279. k=i;
  280. g.setColor(new Color(k*2,255-k*2,k*2));
  281. }
  282. g.fillRect(x.get(i), y.get(i), 10, 10);
  283.  
  284. g.setColor(new Color(0,0,0));
  285. g.fillOval(x.get(i), y.get(i), 10, 10);
  286. }
  287.  
  288. g.setColor(getBackground());
  289. g.fillRect(x.get(x.size()-1), y.get(y.size()-1), 10, 10);
  290.  
  291. if(apple == true){
  292. g.setColor(Color.RED);
  293. g.fillOval(aX, aY, 10, 10);
  294. }
  295.  
  296. if(czysc==true){
  297. for(int i=0;i<dots-1;i++){
  298. g.setColor(getBackground());
  299. g.fillRect(x.get(i), y.get(i), 10, 10);
  300. g.fillRect(aX, aY, 10, 10);
  301. }
  302. czysc=false;
  303. }
  304.  
  305. }
  306. }
  307.  
  308.  
  309. import java.awt.Color;
  310. import java.awt.Dimension;
  311. import java.awt.Font;
  312. import java.awt.Toolkit;
  313. import java.awt.event.MouseAdapter;
  314. import java.awt.event.MouseEvent;
  315. import java.io.File;
  316. import java.io.FileNotFoundException;
  317. import java.io.PrintWriter;
  318. import java.util.ArrayList;
  319. import java.util.NoSuchElementException;
  320. import java.util.Scanner;
  321.  
  322. import javax.swing.JButton;
  323. import javax.swing.JDialog;
  324. import javax.swing.JFrame;
  325. import javax.swing.JLabel;
  326. import javax.swing.JTextField;
  327.  
  328.  
  329. public class ranking extends JDialog{
  330. private static final long serialVersionUID = 1L;
  331.  
  332. File plikDane;
  333. Scanner skanuj;
  334. String plik;
  335. PrintWriter zapisz;
  336. prawa prawa;
  337. JLabel punkty;
  338. JTextField nicktext;
  339.  
  340. ArrayList<String> lista = new ArrayList<String>();
  341. ArrayList<String> points = new ArrayList<String>();
  342.  
  343. JLabel l[] = new JLabel[5];
  344. JLabel p[] = new JLabel[5];
  345. public ranking(JFrame okno, prawa prawa){
  346. super(okno,"RANKING GRACZY",true);
  347. this.prawa=prawa;
  348.  
  349. setResizable(false);
  350. getContentPane().setEnabled(false);
  351. this.setSize(300, 300);
  352. getContentPane().setLayout(null);
  353. Dimension location = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
  354. setLocation((location.width-300)/2,(location.height-300)/2);
  355.  
  356. JButton zatwierdz = new JButton("OK");
  357. zatwierdz.addMouseListener(new MouseAdapter() {
  358. @Override
  359. public void mouseClicked(MouseEvent arg0) {
  360. if(!nicktext.getText().trim().equals("")){
  361. pozycjonowanie();
  362. zmienDane();
  363. setVisible(false);
  364. }
  365. }
  366. });
  367. zatwierdz.setBounds(50, 230, 90, 30);
  368. getContentPane().add(zatwierdz);
  369.  
  370. JButton wroc = new JButton("Anuluj");
  371. wroc.addMouseListener(new MouseAdapter() {
  372. @Override
  373. public void mouseClicked(MouseEvent arg0) {
  374. setVisible(false);
  375. }
  376. });
  377. wroc.setBounds(150, 230, 90, 30);
  378. getContentPane().add(wroc);
  379.  
  380. JLabel nick = new JLabel("Podaj swój nick");
  381. nick.setFont(new Font("Tekton Pro", Font.PLAIN, 30));
  382. nick.setBounds(57, 11, 184, 30);
  383. getContentPane().add(nick);
  384.  
  385. l[0] = new JLabel("New label");
  386. l[0].setBounds(50, 77, 100, 14);
  387. getContentPane().add(l[0]);
  388.  
  389. l[1] = new JLabel("New label");
  390. l[1].setBounds(50, 102, 100, 14);
  391. getContentPane().add(l[1]);
  392.  
  393. l[2] = new JLabel("New label");
  394. l[2].setBounds(50, 127, 100, 14);
  395. getContentPane().add(l[2]);
  396.  
  397. l[3] = new JLabel("New label");
  398. l[3].setBounds(50, 155, 100, 14);
  399. getContentPane().add(l[3]);
  400.  
  401. l[4] = new JLabel("New label");
  402. l[4].setBounds(50, 180, 100, 14);
  403. getContentPane().add(l[4]);
  404.  
  405. nicktext = new JTextField();
  406. nicktext.setBounds(54, 46, 128, 20);
  407. getContentPane().add(nicktext);
  408. nicktext.setColumns(10);
  409.  
  410. JLabel wynik = new JLabel("WYNIK");
  411. wynik.setFont(new Font("Tekton Pro", Font.BOLD | Font.ITALIC, 14));
  412. wynik.setBounds(192, 46, 46, 14);
  413. getContentPane().add(wynik);
  414.  
  415. punkty = new JLabel("0");
  416. punkty.setForeground(Color.GREEN);
  417. punkty.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 14));
  418. punkty.setBounds(238, 46, 46, 14);
  419. getContentPane().add(punkty);
  420.  
  421.  
  422. p[0] = new JLabel("New label");
  423. p[0].setBounds(150, 77, 46, 14);
  424. getContentPane().add(p[0]);
  425.  
  426. p[1] = new JLabel("New label");
  427. p[1].setBounds(150, 102, 46, 14);
  428. getContentPane().add(p[1]);
  429.  
  430. p[2] = new JLabel("New label");
  431. p[2].setBounds(150, 127, 46, 14);
  432. getContentPane().add(p[2]);
  433.  
  434. p[3] = new JLabel("New label");
  435. p[3].setBounds(150, 155, 46, 14);
  436. getContentPane().add(p[3]);
  437.  
  438. p[4] = new JLabel("New label");
  439. p[4].setBounds(150, 180, 46, 14);
  440. getContentPane().add(p[4]);
  441.  
  442. wczytywanie();
  443. }
  444.  
  445. @SuppressWarnings("resource")
  446. private void wczytywanie(){
  447. plik = "ranking.txt";
  448. plikDane= new File(plik);
  449. try {
  450. Scanner skanuj = new Scanner(plikDane);
  451. while(skanuj.hasNextLine()){
  452. lista.add(skanuj.nextLine());
  453. points.add(skanuj.nextLine());
  454. }
  455.  
  456. for (int i=0;i<5;i++){
  457. l[i].setText(lista.get(i));
  458. p[i].setText(points.get(i)+"");
  459. }
  460. }
  461. catch (FileNotFoundException e) {
  462. nowyPlik();
  463. }
  464. }
  465. }
  466.  
  467. private void nowyPlik(){
  468. String plik = "ranking.txt";
  469. try {
  470. zapisz = new PrintWriter(plik);
  471. zapisz.println("Anonim ");
  472. zapisz.println("500");
  473. zapisz.println("Anonim ");
  474. zapisz.println("415");
  475. zapisz.println("Anonim ");
  476. zapisz.println("364");
  477. zapisz.println("Anonim ");
  478. zapisz.println("274");
  479. zapisz.println("Anonim ");
  480. zapisz.println("170");
  481. zapisz.close();
  482. wczytywanie();
  483. }
  484. }
  485. }
  486.  
  487. private void zmienDane(){
  488. String plik = "ranking.txt";
  489. try {
  490. zapisz = new PrintWriter(plik);
  491. for(int i = 0; i<lista.size();i++){
  492. zapisz.println(lista.get(i));
  493. zapisz.println(points.get(i));
  494. }
  495. zapisz.close();
  496. wczytywanie();
  497. }
  498. }
  499. }
  500.  
  501. private void pozycjonowanie(){
  502. for(int i = 0;i<points.size();i++){
  503. if( Integer.parseInt(points.get(i)) < Integer.parseInt(punkty.getText())){
  504. points.add(i, punkty.getText()+"");
  505. points.remove(points.size()-1);
  506.  
  507. lista.add(i, nicktext.getText().trim()+"");
  508. lista.remove(lista.size()-1);
  509. break;
  510. }
  511. }
  512. }
  513. }
  514.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:42: error: class, interface, or enum expected
import java.awt.Color;
^
Main.java:43: error: class, interface, or enum expected
import java.awt.Component;
^
Main.java:44: error: class, interface, or enum expected
import java.awt.Font;
^
Main.java:45: error: class, interface, or enum expected
import java.awt.event.MouseAdapter;
^
Main.java:46: error: class, interface, or enum expected
import java.awt.event.MouseEvent;
^
Main.java:48: error: class, interface, or enum expected
import javax.swing.JButton;
^
Main.java:49: error: class, interface, or enum expected
import javax.swing.JLabel;
^
Main.java:50: error: class, interface, or enum expected
import javax.swing.JPanel;
^
Main.java:51: error: class, interface, or enum expected
import javax.swing.JSlider;
^
Main.java:52: error: class, interface, or enum expected
import javax.swing.JTextField;
^
Main.java:53: error: class, interface, or enum expected
import javax.swing.SwingConstants;
^
Main.java:120: error: class, interface, or enum expected
import java.awt.Color;
^
Main.java:121: error: class, interface, or enum expected
import java.awt.Graphics;
^
Main.java:122: error: class, interface, or enum expected
import java.util.ArrayList;
^
Main.java:123: error: class, interface, or enum expected
import java.util.Random;
^
Main.java:125: error: class, interface, or enum expected
import javax.swing.JPanel;
^
Main.java:309: error: class, interface, or enum expected
import java.awt.Color;
^
Main.java:310: error: class, interface, or enum expected
import java.awt.Dimension;
^
Main.java:311: error: class, interface, or enum expected
import java.awt.Font;
^
Main.java:312: error: class, interface, or enum expected
import java.awt.Toolkit;
^
Main.java:313: error: class, interface, or enum expected
import java.awt.event.MouseAdapter;
^
Main.java:314: error: class, interface, or enum expected
import java.awt.event.MouseEvent;
^
Main.java:315: error: class, interface, or enum expected
import java.io.File;
^
Main.java:316: error: class, interface, or enum expected
import java.io.FileNotFoundException;
^
Main.java:317: error: class, interface, or enum expected
import java.io.PrintWriter;
^
Main.java:318: error: class, interface, or enum expected
import java.util.ArrayList;
^
Main.java:319: error: class, interface, or enum expected
import java.util.NoSuchElementException;
^
Main.java:320: error: class, interface, or enum expected
import java.util.Scanner;
^
Main.java:322: error: class, interface, or enum expected
import javax.swing.JButton;
^
Main.java:323: error: class, interface, or enum expected
import javax.swing.JDialog;
^
Main.java:324: error: class, interface, or enum expected
import javax.swing.JFrame;
^
Main.java:325: error: class, interface, or enum expected
import javax.swing.JLabel;
^
Main.java:326: error: class, interface, or enum expected
import javax.swing.JTextField;
^
33 errors
stdout
Standard output is empty