Java - Programmierung - Grafik - Ereignisse 1

Werbung
Java - Programmierung - Grafik - Ereignisse 1
import java.awt.*;
import java.awt.event.*;
class Head extends Frame implements
{
Thread thread;
boolean auf;
Runnable, WindowListener
Head()
{
auf = true;
thread = new Thread(this);
thread.start();
}
public void paint( Graphics g )
{
auf = !auf;
g.setColor( Color.blue );
g.drawOval( 50, 50, 300, 300 );
g.drawOval( 100, 120, 80, 80 );
if (auf) g.drawOval( 220, 120, 80, 80 );
else g.drawLine( 220, 160, 300, 160 );
g.drawArc ( 100, 200, 200, 100, -20, -140 );
g.drawLine( 200, 200, 200, 230);
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 2
public void run()
{ while (thread != null)
{ try {thread.sleep((int)(1000.0 * Math.random()));}
catch (InterruptedException e) {System.out.println(e);}
repaint();
}
}
public void windowClosing(WindowEvent e)
{ thread.stop();
setVisible(false);
dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {thread.suspend();}
public void windowDeiconified(WindowEvent e) {thread.resume();}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
public class Smile
{ public static void main( String argv[] )
{ Frame f = new Head();
f.setTitle( "Smile" );
f.setSize( 400, 400 );
f.setVisible(true);
// JDK 1.x
f.addWindowListener((WindowListener)f);
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 3
import java.awt.*;
import java.awt.event.*;
public void paint( Graphics g )
{ auf = !auf;
g.setColor( Color.blue );
class SmileHead extends Frame
implements Runnable
{
Thread thread;
boolean auf;
g.drawOval( 50, 50, 300, 300 );
g.drawOval( 100, 120, 80, 80 );
if (auf) g.drawOval( 220, 120, 80, 80 );
else g.drawLine( 220, 160, 300, 160 );
g.drawArc (100, 200, 200, 100, -20, -140);
g.drawLine( 200, 200, 200, 230 );
public static void main(String argv[])
{
new SmileHead();
}
}
public void run()
{ while (thread != null)
{ try {thread.sleep
((int)(1000.0 * Math.random()));}
catch (InterruptedException e)
{System.out.println(e);}
repaint();
}
}
class SHWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ thread.stop();
setVisible(false);
dispose();
System.exit(0);
}
}
SmileHead()
{
setTitle( "Smile" );
setSize( 400, 400 );
setVisible(true);
// JDK 1.x
addWindowListener
(new SHWindowListener());
auf = true;
thread = new Thread(this);
thread.start();
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 4
import java.awt.*;
import java.awt.event.*;
class Text extends Frame
{
Font
foHp, foCb, foTi;
FontMetrics fmHp, fmCb, fmTi;
String sHead = "TU Bergakademie Freiberg",
sFak = "Fakultät ",
sInf = "Ein Studium in Freiberg lohnt sich";
int i, xa, ya;
public static void main( String argv[] ) { new Text(); }
Text()
{ setTitle( "TU Bergakademie Freiberg" );
setSize( 600, 450 );
setVisible(true);
addWindowListener
(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
} );
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 5
public void paint( Graphics g )
{
foHp = new Font( "Helvetica", Font.PLAIN, 36 );
foCb = new Font( "Courier",
Font.BOLD, 24 );
foTi = new Font( "TimesRoman", Font.ITALIC, 18 );
fmHp = g.getFontMetrics( foHp );
fmCb = g.getFontMetrics( foCb );
fmTi = g.getFontMetrics( foTi );
g.setColor( Color.blue );
xa = (getBounds().width - fmHp.stringWidth(sHead)) / 2;
g.setFont(foHp);
g.drawString(sHead, xa, 2 * fmHp.getHeight());
g.setFont(foCb);
g.setColor( Color.black );
ya = 4 * fmHp.getHeight();
xa = 50;
for (i = 1; i < 7; i++)
{ g.drawString(sFak + i, xa, ya);
ya += fmCb.getHeight();
}
g.setColor( Color.darkGray);
g.setFont(foTi);
xa = (getBounds().width - fmTi.stringWidth(sInf) - 50);
ya += (3 * fmTi.getHeight());
g.drawString(sInf, xa, ya);
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 6
import java.awt.*;
import java.awt.event.*;
class News extends Frame implements Runnable
{ Font
foHp,
foCb,
foTi;
FontMetrics fmHp,
fmCb,
fmTi;
String sHead = "TU Bergakademie Freiberg",
sFak = "Fakultät ",
s0 = " +++ NEWS +++ ",
s1 = "Die Fakultät 1 plant für das nächste Studienjahr, ...",
s2 = "Ein Besuch der TU Bergakademie Freiberg lohnt sich immer.";
Image
bI;
Graphics bG;
Thread thread;
int i,
xa,
ya,
breit,
hoch,
pos,
rand;
public static void main( String argv[] ) {new News();}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 7
News()
{
pos = 100;
rand = 50;
bI = null;
foHp = new Font( "Helvetica", Font.PLAIN, 30 );
foCb = new Font( "Courier", Font.BOLD, 20 );
foTi = new Font( "TimesRoman", Font.ITALIC, 18 );
fmHp = getToolkit().getFontMetrics( foHp );
fmCb = getToolkit().getFontMetrics( foCb );
fmTi = getToolkit().getFontMetrics( foTi );
breit = fmTi.stringWidth( s0+s1+s0+s2+s0 );
hoch = fmTi.getHeight();
setBackground( Color.lightGray );
setTitle( "TU Bergakademie Freiberg" );
setSize( 500, 400 );
setVisible(true);
addWindowListener
(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ thread.stop();
setVisible(false);
dispose();
System.exit(0);
}
} );
thread = new Thread(this);
thread.start();
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 8
void checksize()
{
if (
bI == null
|| getBounds().width
!= bI.getWidth(null)
|| getBounds().height
!= bI.getHeight(null) )
{
bI = createImage
(getBounds().width,
getBounds().height);
bG = bI.getGraphics();
}
}
public void paint( Graphics g )
{
checksize();
if (bG == null) paintD(g);
else
{
paintD(bG);
g.drawImage(bI, 0,
0, null);
}
}
public void paintD( Graphics g )
{
g.clearRect(0, 0, getBounds().width,
getBounds().height);
g.clipRect(rand, rand,
getBounds().width - 2 * rand,
getBounds().height - 2 * rand);
g.setColor( Color.blue );
xa = (getBounds().width
- fmHp.stringWidth(sHead)) / 2;
g.setFont(foHp);
g.drawString(sHead, xa, 2 * fmHp.getHeight());
g.setFont(foCb);
g.setColor( Color.black );
ya = 4 * fmHp.getHeight();
xa = 50;
for (i = 1; i < 7; i++)
{ g.drawString(sFak + i, xa, ya);
ya += fmCb.getHeight();
}
g.setFont(foTi);
g.setColor(Color.white);
g.fillRect(0, ya + hoch,
getBounds().width, 3 * hoch / 2);
ya += (2 * hoch );
g.setColor(Color.black);
g.drawString(s0+s1+s0+s2+s0, pos, ya);
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 9
public void update(Graphics g)
{
paint(g);
Toolkit.getDefaultToolkit().sync();
}
public void run()
{
while (thread != null)
{
if (pos + breit > rand)
pos--;
else
pos = getBounds().width - rand;
try
{
thread.sleep(30);
}
catch (InterruptedException e)
{
System.out.println(e);
}
repaint();
}
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 10
import java.awt.*;
import java.awt.event.*;
class SlideShow extends Frame implements Runnable
{
Thread thread;
Image bilder[];
int i, select, anzahl;
public static void main( String argv[] )
{ new SlideShow(argv[0], Integer.parseInt(argv[1]));
}
SlideShow (String b_name, int anzahl)
{ this.anzahl = anzahl;
select = 0;
bilder = new Image [anzahl];
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.print(anzahl);
System.out.print(" Bilder werden geladen ... ");
System.out.flush();
for (i = 0; i < anzahl; i++)
bilder[i] = tk.getImage( b_name + i + ".gif" );
MediaTracker mt = new MediaTracker( this );
for (i = 0; i < anzahl; i++)
mt.addImage( bilder[i], i );
try { mt.waitForAll(); }
catch( InterruptedException e ) { return; }
System.out.println( "fertig" );
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 11
setBackground( Color.orange );
setTitle( "SlideShow" );
setSize( 450, 350 );
setVisible(true);
addWindowListener
(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ thread.stop();
setVisible(false);
dispose();
System.exit(0);
}
} );
thread = new Thread(this);
public void run()
thread.start();
{
}
while (thread != null)
{
public void paint( Graphics g )
select++;
{
select %= anzahl;
g.drawImage( bilder[select], 50, 70, null );
try {thread.sleep(2000);}
}
catch (InterruptedException e)
{
System.out.println(e);
}
repaint();
}
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 12
import java.awt.*;
import java.awt.event.*;
class Display extends Canvas
{ Polygon pf;
int select_f,
phi,
maxy,
mtx,
mty,
i;
Display()
{
select_f = 1;
phi = 0;
maxy = 100;
mtx = 0;
setBackground(Color.black);
new_function();
addMouseListener(new MButton());
}
Display(int select_f, int phi)
{
this.select_f = select_f;
this.phi = phi;
maxy = 100;
setBackground(Color.black);
new_function();
addMouseListener(new MButton());
}
void new_function()
{ pf = new Polygon();
switch (select_f)
{ case 0:
break;
case 1: case 2: case 3: case 4:
pf.addPoint(20, 2 * maxy);
for (i = 0; i <= 360; i++)
pf.addPoint
(i + 20,
(int)Math.round
(maxy * (2 - fkt(select_f, phi, i))));
pf.addPoint(i + 19, 2 * maxy);
break;
case 5: case 6: case 7: case 8:
for (int i = 0; i <= 360; i++)
pf.addPoint
((int)Math.round
(maxy * (2 - fkt(1, 0, i))),
(int)Math.round
(maxy * (2 - fkt(select_f-4, phi, i))));
break;
}
}
void new_function(int s, int p)
{ select_f = s;
phi = p;
new_function();
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 13
double fkt(int art, int phi, int pos)
{
double fw = 0.0;
switch (art)
{ case 1 :
fw = Math.sin((pos + phi) *
2 * Math.PI / 360 );
break;
case 2 :
fw = Math.sin((2 * pos + phi) *
2 * Math.PI / 360);
break;
case 3 :
fw = Math.sin
((pos + phi) * 2 *
Math.PI / 360) + 1 / 3.0 *
Math.sin(3 * (pos + phi) *
2 * Math.PI / 360);
break;
case 4 :
fw = Math.sin((2 * pos + phi) *
2 * Math.PI / 360)
+ 1 / 3.0 * Math.sin
( 3 * (2 * pos + phi) *
2 * Math.PI / 360);
break;
}
return fw;
}
public void paint( Graphics g )
{ g.setColor( Color.green );
g.drawPolygon( pf );
g.setColor( Color.yellow );
if (mtx > 0)
{ g.drawLine(mtx, maxy / 2,
mtx, 7 * maxy / 2);
g.drawLine(mtx - 10, 2 * maxy - mty,
mtx + 10, 2 * maxy - mty);
g.drawString((new Integer(mty).toString()),
mtx+10, maxy / 2);
}
}
public class MButton extends MouseAdapter
{ public void mousePressed(MouseEvent e)
{ int x = e.getX() - 20;
if ( 0 <= x && x <= 360 &&
0 < select_f && select_f < 5)
{ mtx = x + 20; mty = (int)Math.round
(maxy * fkt(select_f, phi, x));
}
repaint();
}
public void mouseReleased(MouseEvent e)
{ mtx = 0;
repaint();
}
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 14
import java.awt.*;
import java.awt.event.*;
public class Disp_F extends Frame
{ /* Test der Klasse mit:
Disp_F Auswahl Phase
int Auswahl 1..8
(1..4 Zeitfunktion 5..8 Lissajous-Figuren)
int Phase
0..360 (Phasenwinkel in Grad)
*/
public static void main( String argv[] ) { new Disp_F(argv); }
Disp_F( String argv[] )
{ add(new Display(Integer.parseInt(argv[0]), Integer.parseInt(argv[1])));
setTitle("Display Function");
setSize( 420, 400 );
setVisible(true);
addWindowListener
(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ setVisible(false);
dispose();
System.exit(0);
}
} );
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 15
import java.awt.*;
import java.awt.event.*;
public class Oszilloscop extends Frame
implements ActionListener,
ItemListener,
TextListener,
AdjustmentListener
{ Display disp;
Scrollbar sb;
Checkbox cb;
TextField tf;
Button b[] = new Button[5];
int phi,
fw,
art;
public static void main(String argv[])
{
new Oszilloscop();
}
Oszilloscop ()
{
int i;
phi = 0;
fw = 1;
art = 0;
setBackground(Color.lightGray);
setLayout(new BorderLayout());
// oben
Panel oben = new Panel();
oben.setLayout
(new FlowLayout(FlowLayout.CENTER));
b[0] = new Button("
sin( x)
");
b[1] = new Button("
sin(2x)
");
b[2] = new Button("sin( x) + 1/3 sin(3x)");
b[3] = new Button("sin(2x) + 1/3 sin(6x)");
for ( i = 0; i <= 3; i++)
{
b[i].addActionListener(this);
oben.add(b[i]);
}
add(oben, BorderLayout.NORTH);
//links
Panel links = new Panel();
b[4] = new Button("Aus");
b[4].addActionListener(this);
links.add(b[4]);
add(links, BorderLayout.WEST);
//zentral
disp = new Display();
add(disp, BorderLayout.CENTER );
//rechts
sb = new Scrollbar
(Scrollbar.VERTICAL, 0, 20, 0, 380);
sb.addAdjustmentListener(this);
sb.setUnitIncrement(3);
sb.setBlockIncrement(30);
add(sb, BorderLayout.EAST);
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 16
//unten
Panel unten = new Panel();
cb = new Checkbox("Lissajous");
cb.addItemListener(this);
unten.add(cb);
unten.add(new Label("Phasenwinkel"));
tf = new TextField("0",3);
tf.addTextListener(this);
unten.add(tf);
unten.add(new Label("°"));
add(unten, BorderLayout.SOUTH);
setBackground(Color.lightGray);
setTitle("Oszilloscop");
resize(600,500);
setVisible(true);
addWindowListener
(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
} );
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 17
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
String arg = e.getActionCommand();
if (obj instanceof Button)
{
if (arg.equals("
sin( x)
"))
fw = 1;
if (arg.equals("
sin(2x)
"))
fw = 2;
if (arg.equals("sin( x) + 1/3 sin(3x)"))
fw = 3;
if (arg.equals("sin(2x) + 1/3 sin(6x)"))
fw = 4;
if (arg.equals("Aus"))
{ fw = 0;
art = 0;
phi = 0;
public void itemStateChanged(ItemEvent e)
tf.setText("0");
{
sb.setValue(phi);
Object obj = e.getSource();
cb.setState(false);
if (obj == cb)
}
{
disp_new();
if (cb.getState()) art = 4;
}
else art = 0;
}
if (fw != 0) disp_new();
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Java - Programmierung - Grafik - Ereignisse 18
public void textValueChanged(TextEvent e)
{
Object obj = e.getSource();
if (obj == tf)
{
phi = Integer.parseInt(tf.getText());
if (phi < 0) phi = 0;
if (phi > 360) phi = 360;
sb.setValue(phi);
disp_new();
}
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
Object obj = e.getSource();
if (obj == sb)
{
phi = sb.getValue();
disp_new();
tf.setText(new Integer(phi).toString());
}
}
void disp_new()
{
disp.new_function(art + fw, phi);
disp.repaint();
}
}
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Herunterladen