Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to change the code so that it extends Application instead of the JFrame? import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.util.ArrayList; public

How to change the code so that it extends Application instead of the JFrame?

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.util.ArrayList; public class Races extends JFrame{ private JPanel jpCenter = new JPanel(); private boolean keepGoing = true; private ArrayList threadList = new ArrayList<>(); public static void main(String[] args){ int num = 0; try{ num = Integer.parseInt(args[2]); } catch(Exception e){ num = 5; } new Races(num); } public Races(int num){ this.setTitle("Off to the Races"); this.setSize(500, 50 * (num + 2)); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for(int i = 0; i < num; i++){ RaceThread racer = new RaceThread(i + 1, i * 32); this.add(racer); Thread t = new Thread(racer); threadList.add(t); } this.setVisible(true); for(int i = 0; i < threadList.size(); i++){ threadList.get(i).start(); } } public void paintComponent(Graphics g){ g.setColor(Color.black); g.drawLine(450,0, 450, 1000); } class RaceThread extends JPanel implements Runnable{ private JLabel label; private JProgressBar progress; private String name; private Icon pic = null; private int x = 0; private int y = 0; private JLabel picLabel; public RaceThread(int num, int yPos){ this.name = "" + num; this.y = yPos; pic = new ImageIcon("races.png"); picLabel = new JLabel("", pic, JLabel.CENTER); } public void paintComponent(Graphics g){ super.paintComponent(g); pic.paintIcon(this, g, x, y); if(keepGoing == false){ g.drawString("Winner is: " + name, 0, y); revalidate(); } } public void run(){ try{ Thread.sleep(1000); } catch(InterruptedException ie){ System.out.println("Error: " + ie); } for(int i = 1; (i <= 180) && (keepGoing == true); i++){ x += 0 + (int)(Math.random() * ((5 - 0) + 1)); this.repaint(); try{ Thread.sleep((int)(Math.random() * 100)); } catch(InterruptedException ie){ System.out.println("Error: " + ie); } } keepGoing = false; this.repaint(); revalidate(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

What is the pension formula for a unit benefit plan?

Answered: 1 week ago

Question

4. Who should be invited to attend?

Answered: 1 week ago

Question

7. How will you encourage her to report back on the findings?

Answered: 1 week ago