0% found this document useful (0 votes)
97 views

Java Draw Train

This Java code defines an applet with three panels that draw and animate different elements. The main panel adds a panel1 object that extends JPanel. Panel1 overrides the paintComponent method to draw various shapes including lines, ovals, rectangles, text and polygons. It implements the Runnable interface and contains a thread that animates the x and y coordinates over time, causing the shapes to move. The thread sleeps between repaint calls to achieve animation.

Uploaded by

FF
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Java Draw Train

This Java code defines an applet with three panels that draw and animate different elements. The main panel adds a panel1 object that extends JPanel. Panel1 overrides the paintComponent method to draw various shapes including lines, ovals, rectangles, text and polygons. It implements the Runnable interface and contains a thread that animates the x and y coordinates over time, causing the shapes to move. The thread sleeps between repaint calls to achieve animation.

Uploaded by

FF
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.awt.

*;
import javax.swing.*;
//import java.util.Date;

public class Innlevering33 extends JApplet {

panel1 pTog;

/* Init metoden. */
public void init() {
Container guiBeholder = getContentPane();
panel panelen = new panel();
guiBeholder.add(panelen, BorderLayout.CENTER);
}

/* Beskriver panel. */
private class panel extends JPanel {
public panel() {
setLayout(new GridLayout(1, 1));
pTog = new panel1();
pTog.start();
add(pTog);
}
}

/* Beskriver panel1. */
private class panel1 extends JPanel implements Runnable {
int delay=10;
double x=0.0,y=0.0,y1=200.0; //aksel B
double x2=580.0,y2=200.0; //aksel C

Thread Thr;
// Date thedate;

public void start() {


if (Thr == null) {
Thr = new Thread(this);
Thr.start();
}
}

public void paintComponent(Graphics g) {


super.paintComponent(g);

int exes[] ={500+(int)x,300+(int)x,300+(int)x,500+(int)x};


int whys[] ={(int)y1-3,197+(int)y,203+(int)y,(int)y1+3};
int pts = exes.length;

g.setColor(Color.darkGray);
g.drawString("Tråder",680+(int)(x*0.1),150+(int)(y*1.5));
g.drawString("Appleter",720+(int)(x*1.2),(int)y1);
g.drawString("GUI",650-(int)(x),170+(int)(y));

g.setColor(Color.blue);
g.fillOval(160, 160, 80, 80);
g.fillOval(260, 160, 80, 80);
g.fillOval(360, 160, 80, 80);
g.fillOval(660, 160, 80, 80);

g.drawLine(155, 210, 140, 210);


g.drawLine(140, 210, 140, 30);
g.drawLine(140, 30, 550, 30);
g.drawLine(550, 30, 550, 100);
g.drawLine(550, 100, 750, 100);
g.drawLine(750, 100, 750, 210);
g.drawLine(750, 210, 745, 210);

g.drawLine(655, 210, 445, 210);


g.drawLine(655, 190, 540, 190);

g.fillRect(580, 190, 70, 20);

g.drawLine(345, 210, 355, 210);


g.drawLine(245, 210, 255, 210);
// g.drawString(thedate.toString(),180+(int)x,100+(int)y);

g.setColor( Color.black);
g.fillRect(500+(int)x, (int)y1-3 ,80-(int)x,(int)y1-194);//aksel C
g.fillPolygon(exes, whys, pts);//aksel B
g.fillRect(200+(int)x, 197+(int)y, 200, 6);//aksel A

g.setColor(Color.darkGray);
g.fillOval(195+(int)x, 195+(int)y, 10, 10);
g.fillOval(295+(int)x, 195+(int)y, 10, 10);
g.fillOval(395+(int)x, 195+(int)y, 10, 10);
g.fillOval(495+(int)x, (int)y1-5, 10, 10);

public void stop() { Thr = null;}

public void run() {


while (true) {
// thedate =new Date();
while (x<31) {
pTog.repaint();
try {
Thread.currentThread().sleep(delay);
}
catch (InterruptedException e) {};
y=-Math.sqrt(900-(x*x));
x=x+1;
}
while (x> -31) {
pTog.repaint();
try {
Thread.currentThread().sleep(delay);
}
catch (InterruptedException e) {}
y=Math.sqrt(900-(x*x));
x=x-1;
}
}
}
}
}

You might also like