All pastes #457849 Raw Edit

AIMMOTH

public text v1 · immutable
#457849 ·published 2007-04-25 16:20 UTC
rendered paste body

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Uppgift2f extends JApplet implements Runnable
{

	private JPanel canvas = new JPanel()
		{
			public void paintComponent(Graphics g)
			{
				super.paintComponent(g);
				paintCanvas(g);
			}
		};
	private Thread tråd;
	private boolean run = false;
	
	private Font f = new Font("big", Font.BOLD, 30);
	private FontMetrics fm;
	private String temp = "Wonder Showzen!";
	private char[] ord = temp.toCharArray();
	private int l = ord.length;
	private int[] avstånd = new int[l];
	private int höjd;
	private final int SFAKTOR = 5;
	
	public void init()
	{
		setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
	}
	
	public void start()
	{
		try
		{
			tråd = new Thread(this);
			run = true;
			tråd.run();
		}
		catch (Exception e)
		{
			fel(e);
		}
	}
	
	public void stop()
	{
		run = false;
	}
	
	public void run()
	{
		try
		{
			System.out.print("Exekvering pågår");
			while (run)
			{
				canvas.repaint();
				tråd.sleep(100);
				System.out.print(".");
			}
		}
		catch (Exception e)
		{
			fel(e);
		}
	}
	
	private void paintCanvas(Graphics g)
	{
		g.setFont(f);
		if (fm == null)
		{
			fm = g.getFontMetrics();
			
			for (int i=0; i<l; i++)
				avstånd[i] = fm.charsWidth(ord, 0, i);
			höjd = fm.getAscent();
		}
		
		int w = canvas.getWidth();
		int h = canvas.getHeight();
		
		g.setColor(Color.white);
		g.fillRect(0, 0, w, h);
		
		g.setColor(Color.black);
		for (int i=0; i<l; i++)
		{
			int x = (int) (50 + (Math.random() * SFAKTOR) + avstånd[i]);
			int y = (int) (50 + (Math.random() * SFAKTOR) + höjd);
			g.drawChars(ord, i, 1, x, y);
		}
	}
	
	private void fel(Exception e)
	{
		e.printStackTrace();
		System.exit(1);
	}
}