bardzo prosze
Applet
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Applet extends JApplet
{
Arkanoid canvas;
BufferedImage statek;
public void init()
{
canvas = new Arkanoid();
setLayout(new BorderLayout());
add(canvas);
}
public void start()
{
canvas.start();
}
public void stop()
{
canvas.stop();
}
public static void main(String[] args)
{
Applet applet = new Applet();
Frame frame = new Frame();
frame.addWindowListener(closer);
frame.add(applet);
frame.setSize(1000,700);
applet.init();
frame.setVisible(true);
applet.start();
Cursor invisibleCursor = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage(""), new Point(0, 0), "invisible");
applet.setCursor(invisibleCursor);
}
private static WindowListener closer = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
}
Canvas
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
class Arkanoid extends Canvas implements Runnable
{
public BufferStrategy buforowanie;
public BufferedImage statek, kulka, tlo, klocek;
Thread thread;
public int
x = 100,
y,
x_kulki = 500,
y_kulki = 580;
public int
lewo_prawo = (int)(10 * Math.random()),
gora_dol = -3,
x_klocka = 0,
y_klocka = 0;
public boolean[][] jest_czy_nie;
public Rectangle[][] klocki;
public Rectangle[][] klocki_gora;
public Rectangle[][] klocki_dol;
public Rectangle[][] klocki_lewo;
public Rectangle[][] klocki_prawo;
public boolean czy_zbite = false;
public int szybkosc = 5;
public Arkanoid()
{
statek = loadImage("statek.gif");
kulka = loadImage("kulka.gif");
tlo = loadImage("8.jpg");
klocek = loadImage("klocek.gif");
setFocusable(true);
addMouseListener(new Mysz(this));
addMouseMotionListener(new Mysz(this));
addKeyListener(new Klawisze(this));
jest_czy_nie = new boolean[10][10];
klocki = new Rectangle[10][10];
klocki_gora = new Rectangle[10][10];
klocki_dol = new Rectangle[10][10];
klocki_lewo = new Rectangle[10][10];
klocki_prawo = new Rectangle[10][10];
requestFocus();
setFocusable(true);
Cursor invisibleCursor = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage(""), new Point(0, 0), "invisible");
setCursor(invisibleCursor);
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
jest_czy_nie[i][j] = true;
klocki_gora[i][j] = zwroc_wsp_gory(klocek, 80 * i, 20 * j);
klocki_dol[i][j] = zwroc_wsp_dolu(klocek, 80 * i, 20 * j);
klocki_lewo[i][j] = zwroc_wsp_lewa(klocek, 80 * i, 20 * j);
klocki_prawo[i][j] = zwroc_wsp_prawa(klocek, 80 * i, 20 * j);
}
}
public BufferedImage loadImage(String sciezka)
{
URL url = null;
try
{
url = getClass().getClassLoader().getResource(sciezka);
return ImageIO.read(url);
}
catch (Exception e)
{
System.out.println("w pizdeczke " + sciezka);
System.exit(0);
return null;
}
}
public Rectangle zwroc_wsp(BufferedImage obrazek, int x, int y)
{
int a, b;
BufferedImage image;
a = x;
b = y;
image = obrazek;
return new Rectangle(a, b, image.getWidth(), image.getHeight());
}
public Rectangle zwroc_wsp_gory(BufferedImage obrazek, int x, int y)
{
int a, b;
BufferedImage image;
a = x;
b = y;
image = obrazek;
return new Rectangle(a, b, 80, 3);
}
public Rectangle zwroc_wsp_dolu(BufferedImage obrazek, int x, int y)
{
int a, b;
BufferedImage image;
a = x;
b = y;
image = obrazek;
return new Rectangle(a, b + 17, 80, 3);
}
public Rectangle zwroc_wsp_lewa(BufferedImage obrazek, int x, int y)
{
int a, b;
BufferedImage image;
a = x;
b = y;
image = obrazek;
return new Rectangle(a, b + 3 , 3, 14);
}
public Rectangle zwroc_wsp_prawa(BufferedImage obrazek, int x, int y)
{
int a, b;
BufferedImage image;
a = x;
b = y;
image = obrazek;
return new Rectangle(a + 77, b + 3 , 3, 14);
}
public void run()
{
Graphics g = buforowanie.getDrawGraphics();
g.drawImage(tlo , 0, 0, 800, 600, 0, 0, 800, 600, this);
int i = -1;
int j = -1;
Graphics2D g2 = (Graphics2D)g;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (jest_czy_nie[i][j] == true)
g.drawImage(klocek, 80*i, 20 * j, 80, 20, null);
}
j = 0;
}
while (true)
{
if (x > 700) x = 700;
g.drawImage(tlo , 0, 550, 800, 570, 0, 550, 800, 570, this);
g.drawImage(tlo , x_kulki, y_kulki, x_kulki + 20, y_kulki + 20,
x_kulki, y_kulki , x_kulki + 20, y_kulki + 20, this);
x_kulki = x_kulki + lewo_prawo;
y_kulki = y_kulki + gora_dol;
if (x_kulki < 0) lewo_prawo = -lewo_prawo;
if (x_kulki > 800) lewo_prawo = -lewo_prawo;
if (y_kulki < 0) gora_dol = - gora_dol;
if (y_kulki > 600) System.exit(0);
g.drawImage(statek , x, 550, this);
g.drawImage(kulka , x_kulki, y_kulki, this);
Rectangle statek_wsp = zwroc_wsp(statek, x, 550);
Rectangle kulka_wsp = zwroc_wsp(kulka, x_kulki, y_kulki);
if (statek_wsp.intersects(kulka_wsp))
{
gora_dol= - gora_dol;
lewo_prawo = (int)(((x_kulki - x) - 50)/10);
}
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (kulka_wsp.intersects(klocki_lewo[i][j]) &&
jest_czy_nie[i][j] && !czy_zbite)
{
g.drawImage(tlo , 80 * i, 20 * j, 80 * i + 800, 20 * j + 20,
80 * i, 20 * j, 80 * i + 80, 20 * j + 20, this);
lewo_prawo = - lewo_prawo;
jest_czy_nie[i][j] = false;
System.out.println("lewo");
czy_zbite = true;
}
else if (kulka_wsp.intersects(klocki_prawo[i][j]) &&
jest_czy_nie[i][j] && !czy_zbite)
{
g.drawImage(tlo , 80 * i, 20 * j, 80 * i + 80, 20 * j + 20,
80 * i, 20 * j, 80 * i + 80, 20 * j + 20, this);
lewo_prawo = - lewo_prawo;
jest_czy_nie[i][j] = false;
System.out.println("prawo");
czy_zbite = true;
}
else if (kulka_wsp.intersects(klocki_dol[i][j]) &&
jest_czy_nie[i][j] && !czy_zbite)
{
g.drawImage(tlo , 80 * i, 20 * j, 80 * i + 80, 20 * j + 20,
80 * i, 20 * j, 80 * i + 80, 20 * j + 20, this);
gora_dol = - gora_dol;
jest_czy_nie[i][j] = false;
System.out.println("dol");
czy_zbite = true;
}
else if (kulka_wsp.intersects(klocki_gora[i][j]) &&
jest_czy_nie[i][j] && !czy_zbite)
{
g.drawImage(tlo , 80 * i, 20 * j, 80 * i + 80, 20 * j + 20,
80 * i, 20 * j, 80 * i + 80, 20 * j + 20, this);
gora_dol = - gora_dol;
jest_czy_nie[i][j] = false;
System.out.println("gora");
czy_zbite = true;
}
}
j = 0;
}
czy_zbite = false;
buforowanie.show();
try
{
Thread.sleep(szybkosc);
}
catch (InterruptedException e)
{
System.out.println("interrupt");
break; // We need to exit this loop now.
}
}
}
public void start()
{
if(buforowanie == null)
{
createBufferStrategy(2);
buforowanie = getBufferStrategy();
}
thread = new Thread(this);
thread.start();
}
public void stop()
{
thread.interrupt();
thread = null;
}
}
oraz 2 sluchacze
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Klawisze implements KeyListener
{
public Klawisze(Arkanoid panel)
{
p = panel;
}
public void keyPressed(KeyEvent zdarzenie)
{
kod_klawisza = zdarzenie.getKeyCode();
if (kod_klawisza == KeyEvent.VK_ESCAPE) System.exit(0);
if (kod_klawisza == KeyEvent.VK_S)
{
p.start();
System.out.println("sdfgfdsgfs");
}
if (kod_klawisza == KeyEvent.VK_D) p.stop();
}
public void keyReleased(KeyEvent zdarzenie)
{
}
public void keyTyped(KeyEvent zdarzenie)
{
}
int kod_klawisza;
Arkanoid p;
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mysz implements MouseListener, MouseMotionListener
{
public Mysz (Arkanoid arkanoid)
{
a = arkanoid;
}
public void mousePressed(MouseEvent zdarzenie)
{
x = zdarzenie.getX();
System.out.println(x);
}
public void mouseExited(MouseEvent zdarzenie)
{
}
public void mouseClicked(MouseEvent zdarzenie)
{
}
public void mouseReleased(MouseEvent zdarzenie)
{
}
public void mouseEntered(MouseEvent zdarzenie)
{
}
public void mouseDragged(MouseEvent zdarzenie)
{
}
public void mouseMoved(MouseEvent zdarzenie)
{
a.x = zdarzenie.getX();
a.y = zdarzenie.getY();
//System.out.println(x);
}
int x, y;
Graphics g;
Arkanoid a;
}