Hello This code draws this stadium as is shown in the picture (attached files) I
ID: 3694439 • Letter: H
Question
Hello
This code draws this stadium as is shown in the picture (attached files)
I want to put a circle moving inside this stadium
Please Please
Thanks and regards
code\
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
public class Animation extends JPanel {
public static final Color DARK_GREEN = new Color(0x00, 0xC0, 0x00);
public static final Color VERY_DARK_GREEN = new Color(0x00, 0x80, 0x00);
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(800, 800);
f.add(new Animation());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
//for green ground
public void paint(Graphics g) {
// g.setColor(LIGHT_GREEN);
int x = 30,y=30;
for(int i=0;i<20;i++){
if(i%2 == 0) g.setColor(DARK_GREEN);
else g.setColor(VERY_DARK_GREEN);
g.fillRect (x, y, 400, 30);
y += 30;
}
// g.setStroke(stroke);
g.setColor(Color.WHITE);
//outer rectangle
g.drawRect(35,35,390,590);
//top two rectangles
g.drawRect(150,35,150,80);
g.drawRect(185,35,80,50);
//center LINe
g.drawLine(35,35+295,35+390,35+295);
//midle circle
g.drawOval(195,295,70,70);
//corner arcs
g.drawArc(-15,-15,100,100,270,90);
g.drawArc(-15+390,-15,100,100,180,90);
g.drawArc(-15,-15+590,100,100,0,90);
g.drawArc(-15+390,-15+590,100,100,90,90);
//botton rectangles
g.drawRect(150,-45+590,150,80);
g.drawRect(185,-15+590,80,50);
//top rectangle arc
g.drawArc(200,-55+590,50,20,0,180);
//botton rectangle arc
g.drawArc(200,105,50,20,180,180);
}
}
Explanation / Answer
I just implemented Runnable interface which has run() that calls paint() repeatedly. And in the run method we should increment and reset the x and y values based on xLimit and yLimit ranges.
So in the output the center ball will move around the stadium based on xLimit and yLimit values.
Please change xLimit and yLimit values according to your circle movement needs.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Stadium extends JPanel implements Runnable {
Thread t;
// xLimit and yLimit values should be changed according to moment we need
int xLimit = 390;
int yLimit = 240;
int x = 0;
int y = yLimit;
public static final Color DARK_GREEN = new Color(0x00, 0xC0, 0x00);
public static final Color VERY_DARK_GREEN = new Color(0x00, 0x80, 0x00);
public void start() {
t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
// g.setColor(LIGHT_GREEN);
int x1 = 30, y1 = 30;
for (int i = 0; i < 20; i++) {
if (i % 2 == 0)
g.setColor(DARK_GREEN);
else
g.setColor(VERY_DARK_GREEN);
g.fillRect(x1, y1, 400, 30);
y1 += 30;
}
// g.setStroke(stroke);
g.setColor(Color.WHITE);
// outer rectangle
g.drawRect(35, 35, 390, 590);
// top two rectangles
g.drawRect(150, 35, 150, 80);
g.drawRect(185, 35, 80, 50);
// center LINe
g.drawLine(35, 35 + 295, 35 + 390, 35 + 295);
// midle circle
g.drawOval(x, y, 50, 50);
// corner arcs
g.drawArc(-15, -15, 100, 100, 270, 90);
g.drawArc(-15 + 390, -15, 100, 100, 180, 90);
g.drawArc(-15, -15 + 590, 100, 100, 0, 90);
g.drawArc(-15 + 390, -15 + 590, 100, 100, 90, 90);
// botton rectangles
g.drawRect(150, -45 + 590, 150, 80);
g.drawRect(185, -15 + 590, 80, 50);
// top rectangle arc
g.drawArc(200, -55 + 590, 50, 20, 0, 180);
// botton rectangle arc
g.drawArc(200, 105, 50, 20, 180, 180);
}
public void run() {
try {
for (;;) {
for (;;) {
if (y == yLimit) {
break;
} else if (x == xLimit) {
x = 0;
y = 0;
repaint();
} else {
y = y + 3;
x = x + 3;
Thread.sleep(100);
repaint();
}
}
for (;;) {
if (y == 0) {
break;
} else if (x == xLimit) {
x = 0;
y = 0;
repaint();
} else {
y = y - 3;
x = x + 3;
Thread.sleep(100);
repaint();
}
}
run();
}
} catch (InterruptedException e) {
}
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(800, 800);
Stadium stadium = new Stadium();
f.add(stadium);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
stadium.start();
}
}
Output:
Center ball will be moving around the stadium with ln the xLimit and yLimit ranges
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.