![]() Ласло Краус |
Програмирање на језику Java - испити Практични испит 12. 2. 2006. |
|---|
Саставити на језику Java следећи пакет класа:
Graphics getGraphics(). Тренутно време може да се очита методом long System.currentTimeMillis().

studentx (x је број радне станице за којим се ради). Лозинка на свакој радној станици је student.N: (Nastava on \\Xs200-2) у каталогу ObjektnoProg\RSx (x је број радне станице за којим се ради).
// Vektor.java
package vatromet;
public class Vektor {
private double x, y;
Vektor (double xx, double yy) {x=xx; y=yy;}
double x() { return x; }
double y() { return y; }
}
// Figura.java
package vatromet;
import java.awt.Canvas;
public abstract class Figura {
protected Scena scena;
protected Figura (Scena s) { (scena = s).dodaj (this); }
public abstract void crtaj ();
public void unisti () { scena.izbaci (this); }
}
// Scena.java
package vatromet;
import java.awt.*;
public class Scena extends Canvas implements Runnable {
private class Elem {
Figura fig; Elem sled;
Elem (Figura f) {
fig = f;
if (prvi == null) prvi = this; else posl.sled = this;
posl = this;
}
}
private Elem prvi, posl;
private Thread nit = new Thread (this);
public Scena () { nit.start (); }
public void dodaj (Figura fig) { new Elem (fig); }
public void izbaci (Figura fig) {
Elem tek = prvi, pret = null;
while (tek!=null && tek.fig!=fig)
{ pret = tek; tek = tek.sled; }
if (tek != null) {
if (pret == null) prvi = tek.sled;
else pret.sled = tek.sled; if (tek.sled == null) posl = pret;
if (prvi == null) posl = null;
}
}
public void paint (Graphics g) {
for (Elem tek=prvi; tek!=null; tek=tek.sled) tek.fig.crtaj ();
}
public void run () {
try {
while (! nit.interrupted()) { repaint (); nit.sleep (20); }
} catch (InterruptedException g) {}
}
public void zavrsi () { nit.interrupt (); }
}
// Petarda.java
package vatromet;
import java.awt.*;
public abstract class Petarda extends Figura {
private Vektor r0, v0; private long t0; protected Color boja;
public Petarda (Scena s, Vektor rr0, Vektor vv0, Color bboja) {
super (s);
r0 = rr0; v0 = vv0; boja = bboja;
t0 = System.currentTimeMillis ();
}
public Vektor polozaj () {
double t = (System.currentTimeMillis() – t0) / 1000.;
return new Vektor (
r0.x() + v0.x() * t,
r0.y() + v0.y() * t - 10 * t * t
);
}
public void crtaj () {
int sir = scena.getWidth (); int vis = scena.getHeight ();
Vektor p = polozaj ();
int x = (int)p.x (); int y = vis - (int)p.y();
if (x>=0 && x<sir && y>=0 && y<vis) {
Graphics g = scena.getGraphics ();
g.setColor (boja); crt (g, x, y);
} else scena.izbaci (this);
}
protected abstract void crt (Graphics g, int x, int y);
}
// Krug.java
package vatromet;
import java.awt.*;
public class Krug extends Petarda {
private int a;
public Krug (Scena s, Vektor r0, Vektor v0, Color boja, int aa)
{ super (s, r0, v0, boja); a = aa; }
public void crt (Graphics g, int x, int y)
{ g.fillOval (x-a/2, y-a/2, a, a); }
}
// Zvezda.java
package vatromet;
import java.awt.*;
public class Zvezda extends Petarda {
private int a;
public Zvezda (Scena s, Vektor r0, Vektor v0, Color boja, int aa)
{ super (s, r0, v0, boja); a = aa; }
public void crt(Graphics g, int x, int y) {
g.drawLine (x-a/2, y-a/2, x+a/2, y+a/2);
g.drawLine (x+a/2, y-a/2, x-a/2, y+a/2);
g.drawLine (x, y-a/2, x, y+a/2);
g.drawLine (x-a/2, y, x+a/2, y );
}
}
// Top.java
package vatromet;
import java.awt.*;
public class Top extends Figura implements Runnable {
private static int ukId = 0;
private int id = ++ukId, x, y, n;
private Thread nit = new Thread (this);
private boolean radi = false;
public Top (Scena s, Vektor p) {
super (s); scena = s;
premesti (p); nit.start();
}
public void premesti (Vektor p) { x = (int)p.x(); y = (int)p.y(); }
public int id () { return id; }
public String toString () { return id + "/" + n; }
public void crtaj () {
int vis = scena.getHeight ();
int y = vis - this.y;
Graphics g = scena.getGraphics ();
g.setColor (Color.LIGHT_GRAY);
g.fillRect (x-20, y-20, 40, 20);
g.setColor (Color.BLACK);
g.drawRect (x-20, y-20, 40, 20);
g.drawString (toString(), x-18, y-4);
}
public void run () {
try {
while (! nit.interrupted ()) {
if (! radi) synchronized (this) { wait (); }
Vektor p = new Vektor(x, y+20);
Vektor v = new Vektor (-100+Math.random()*200, 50+Math.random()*100);
Color b = new Color(
(float)(Math.random()),
(float)(Math.random()),
(float)(Math.random())
);
int a = (int)(4+Math.random()*12);
if (Math.random() < 0.5) new Krug (scena, p, v, b, a);
else new Zvezda (scena, p, v, b, a);
n++;
nit.sleep ((long)(500+Math.random()*500));
}
} catch (InterruptedException g) {}
scena.izbaci (this);
}
public synchronized void kreni () { radi = true; notify (); }
public synchronized void stani () { radi = false; }
public void zavrsi () { nit.interrupt (); }
}
// Vatromet.java
package vatromet;
import java.awt.*;
import java.awt.event.*;
public class Vatromet extends Frame {
private Scena scena;
private Top[] topovi = new Top [3];
public Vatromet () {
super ("Vatromet"); setSize (300, 200);
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent d) {
for (int i=0; i<topovi.length; topovi[i++].zavrsi());
scena.zavrsi (); dispose ();
}
});
add (scena = new Scena(), "Center");
scena.addComponentListener ( new ComponentAdapter () {
public void componentResized (ComponentEvent d) {
for (int i=0; i<topovi.length; i++)
topovi[i].premesti (new Vektor(
scena.getWidth()*(i+1)/
(topovi.length+1), 0
));
}
});
Panel plo = new Panel (); add (plo, "South");
plo.setBackground (Color.GRAY);
Button dgm = new Button ("Kreni"); plo.add (dgm);
dgm.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent d)
{ for (int i=0; i<topovi.length; topovi[i++].kreni()); }
});
plo.add (dgm = new Button ("Stani"));
dgm.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent d)
{ for (int i=0; i<topovi.length; topovi[i++].stani()); }
});
for (int i=0; i<topovi.length; i++)
topovi[i] = new Top (scena, new Vektor(0, 0));
setVisible (true);
}
public static void main (String[] varg) { new Vatromet (); }
}
(садржај)
Copyright © 2010, Laslo Kraus
Последња ревизија: 8.9.2010.