Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created September 7, 2015 14:50
Show Gist options
  • Save AnnaBoro/630d5e4bad664c63fd76 to your computer and use it in GitHub Desktop.
Save AnnaBoro/630d5e4bad664c63fd76 to your computer and use it in GitHub Desktop.
lesson2
/*
* Copyright (c) 2014 kademika.com
*/
package lesson2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class BattleFieldTemplate extends JPanel {
boolean COLORDED_MODE = true;
int tankX = 0;
int tankY = 0;
long speed = 225;
/**
* Write your code here.
*/
void runTheGame() throws Exception {
while (true) {
for (int i = 0; i < 9; i ++) {
tankX += 64;
repaint();
Thread.sleep(speed);
if (tankX < 512) {
continue;
}
break;
}
for (int i = 0; i < 8; i ++) {
tankX -= 64;
repaint();
Thread.sleep(speed);
}
}
}
void move(int direction) throws Exception {
repaint();
Thread.sleep(speed);
if (direction == 1) {
if (tankY !=0) {
tankY -= 64;
repaint();
System.out.println("Up: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 2) {
if (tankY != 512) {
tankY += 64;
repaint();
System.out.println("Down: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 3) {
if (tankX != 0) {
tankX -= 64;
repaint();
System.out.println("Left: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
else if (direction == 4) {
if (tankX != 512) {
tankX += 64;
repaint();
System.out.println("Right: " + tankX + "_" + tankY);
Thread.sleep(speed * 2);
}
else System.out.println("Wrong direction");
}
}
// Magic bellow. Do not worry about this now, you will understand everything in this course.
// Please concentrate on your tasks only.
final int BF_WIDTH = 576;
final int BF_HEIGHT = 576;
public static void main(String[] args) throws Exception {
BattleFieldTemplate bf = new BattleFieldTemplate();
// bf.runTheGame();
bf.move(1);
bf.move(2);
bf.move(3);
bf.move(4);
bf.move(4);
bf.move(1);
bf.move(2);
bf.move(2);
}
public BattleFieldTemplate() throws Exception {
JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
frame.setLocation(500, 150);
frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int i = 0;
Color cc;
for (int v = 0; v < 9; v++) {
for (int h = 0; h < 9; h++) {
if (COLORDED_MODE) {
if (i % 2 == 0) {
cc = new Color(252, 241, 177);
} else {
cc = new Color(233, 243, 255);
}
} else {
cc = new Color(180, 180, 180);
}
i++;
g.setColor(cc);
g.fillRect(h * 64, v * 64, 64, 64);
}
}
g.setColor(new Color(255, 0, 0));
g.fillRect(tankX, tankY, 64, 64);
}
}
package lesson2;
public class Frame17 {
public static void main(String[] args) {
System.out.println(oldEnough(2));
}
static boolean oldEnough(int age) {
if (age >= 21){
return true;
}
return false;
}
}
package lesson2;
public class Frame19 {
public static void main(String[] args) {
System.out.println(min(3, -377, -111));
}
static int min(int a, int b, int c) {
int min = a;
if (min > b){
min = b;
}
if (min > c){
min = c;
}
return min;
}
}
package lesson2;
public class Frame21 {
public static void main(String[] args) {
System.out.println(min(-73, -778, -111));
}
static int min(int a, int b, int c){
if (a < b && a < c){
return a;
}
else if (b < c){
return b;
}
return c;
}
}
package lesson2;
public class Frame23 {
public static void main(String[] args) {
System.out.println(isSpring(-2));
System.out.println(isSpring(2));
System.out.println(isSpring(6));
}
static int isSpring(int month) {
if (month == 4 || month == 5 || month ==6){
return 1;
}
if (month <=0 || month > 12){
return -1;
}
return 0;
}
}
package lesson2;
public class Frame25 {
public static void main(String[] args) {
//System.out.println(getQuadrant("b", "2"));
printCoordinates("a", "2");
printCoordinates("c", "1");
printCoordinates("i", "9");
}
/**
* - TANKS-
* @param v
* @param h
* @return
*/
static String getQuadrant(String v, String h) {
String x = null;
String y = null;
String [] vertical = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
for (int i = 0; i < 9; i++){
if (vertical[i] == v) {
x = String.valueOf(i * 64);
}
}
int j = Integer.parseInt(h);
y = String.valueOf((j-1) * 64);
return x + "_" + y;
}
static void printCoordinates(String v, String h){
int dotIndex = getQuadrant(v, h).indexOf("_");
String pix1 = getQuadrant(v, h).substring(0,dotIndex);
String pix2 = getQuadrant(v, h).substring(dotIndex + 1);
System.out.println(v + h + ":" + "(" + pix1 + "px;" + pix2 + "px)");
}
}
package lesson2;
public class Frame31 {
public static void main(String[] args) {
int i = 1;
while (true){
System.out.println(i);
i++;
if (i == 11) {
break;
}
}
}
}
package lesson2;
public class Frame32 {
public static void main(String[] args) {
int i = 1;
while (i <= 10){
System.out.println(i);
i++;
if (i <= 10) {
continue;
}
break;
}
}
}
package lesson2;
public class Frame4 {
public static void main(String[] args) {
hello("Agata");
}
public static void hello(String name) {
System.out.println("Hello " + name);
}
}
package lesson2;
public class Frame5 {
public static void main(String[] args) {
sum(2, 4);
sum(2, -4);
}
public static void sum(int num1, int num2) {
System.out.println(num1 + num2);
}
}
package lesson2;
public class Frame6 {
public static void main(String[] args) {
System.out.println(square(7));
System.out.println(square(8.0));
}
static double square(double number) {
return number * number;
}
static int square(int number){
return number * number;
}
}
package lesson2;
public class Homework4 {
public static void main(String[] args) {
start(9);
}
static void start(int number) {
if (number > 0) {
for (int i = number; i > 0; i--) {
System.out.println(i);
}
System.out.println("Go!");
}
else {
System.out.println("Start failed!");
}
}
}
package lesson2;
public class Homework5 {
public static void main(String[] args) {
printNumbers(14, 60);
}
static void printNumbers(int i, int n) {
while (i < n) {
n++;
i = i+2;
System.out.println(i + ":" + n);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment