two codes there are 5 area (Stars***) I need to fill in 5 areas of ****. to run
ID: 3569454 • Letter: T
Question
two codes
there are 5 area (Stars***) I need to fill in 5 areas of ****. to run the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise16_15_HW11 extends JFrame {
public Exercise16_15_HW10() {
add(new RaceCar());
}
public static void main(String[] args) {
Exercise16_15_HW10 frame = new Exercise16_15_HW10();
frame.setTitle("Exercise16_15_HW10");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RaceCar extends JPanel {
private int xBase = 0;
private Timer timer = new Timer(100, ****);
public RaceCar() {
timer.start();
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
*****
}
@Override
public void mouseReleased(MouseEvent e) {
*****
}
});
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.****) {
if (timer.getDelay() > 5)
timer.setDelay(timer.getDelay() - 5);
}
else if (e.getKeyCode() == KeyEvent.****)
timer.setDelay(timer.getDelay() + 1);
}
});
}
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int yBase = getHeight();
if (xBase > getWidth())
xBase = -20;
else
xBase += 1;
g.setColor(Color.BLACK);
g.fillOval(xBase + 10, yBase - 10, 10, 10);
g.fillOval(xBase + 30, yBase - 10, 10, 10);
g.setColor(Color.GREEN);
g.fillRect(xBase, yBase - 20, 50, 10);
g.setColor(Color.RED);
Polygon polygon = new Polygon();
polygon.addPoint(xBase + 10, yBase - 20);
polygon.addPoint(xBase + 20, yBase - 30);
polygon.addPoint(xBase + 30, yBase - 30);
polygon.addPoint(xBase + 40, yBase - 20);
g.fillPolygon(polygon);
}
}
______________________________________
second has six areas
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise17_03_HW11 extends JFrame {
private JRadioButton jrbRed, jrbYellow, jrbGreen;
private ButtonGroup btg = new ButtonGroup();
private Light light = new Light();
public static void main(String[] args) {
Exercise17_03_HW11 frame = new Exercise17_03_HW11();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 170);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public Exercise17_03_HW11() {
setTitle("Exercise17_03_HW11");
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(light);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jrbRed = new JRadioButton("Red"));
p2.add(jrbYellow = new JRadioButton("Yellow"));
p2.add(jrbGreen = new JRadioButton("Green"));
jrbRed.setMnemonic('R');
jrbYellow.setMnemonic('Y');
jrbGreen.setMnemonic('G');
btg.add(jrbRed);
btg.add(jrbYellow);
btg.add(jrbGreen);
// Place p1 and p2 in the frame
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
jrbRed.addItemListener(****);
jrbYellow.addItemListener(****);
jrbGreen.addItemListener(****);
jrbGreen.setSelected(true);
light.turnOnGreen();
}
class MyItemEventListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (jrbRed.isSelected()) {
light.****;
}
if (jrbYellow.isSelected()) {
light.****;
}
if (jrbGreen.isSelected()) {
light.****;
}
}
}
class Light extends JPanel {
private boolean red;
private boolean yellow;
private boolean green;
public Light() {
}
public void turnOnRed() {
red = true;
yellow = false;
green = false;
repaint();
}
public void turnOnYellow() {
red = false;
yellow = true;
green = false;
repaint();
}
public void turnOnGreen() {
red = false;
yellow = false;
green = true;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (red) {
g.setColor(Color.red);
g.fillOval(10, 10, 20, 20);
g.setColor(Color.black);
g.drawOval(10, 35, 20, 20);
g.drawOval(10, 60, 20, 20);
g.drawRect(5, 5, 30, 80);
}
else if (yellow) {
g.setColor(Color.yellow);
g.fillOval(10, 35, 20, 20);
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10, 20, 20);
g.drawOval(10, 60, 20, 20);
}
else if (green) {
g.setColor(Color.green);
g.fillOval(10, 60, 20, 20);
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10, 20, 20);
g.drawOval(10, 35, 20, 20);
}
else {
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10, 20, 20);
g.drawOval(10, 35, 20, 20);
g.drawOval(10, 60, 20, 20);
}
}
public Dimension getPreferredSize() {
return new Dimension(40, 90);
}
}
}
Explanation / Answer
1st program:
1 **** = new Listener()
2nd ****={
graphics g;
this.paintComponent(g);
}
3rd *** {
/*nothing happens here*/
}
4th*** KeyEvent.getkeycode();
5th *** KeyEvent.getkeylocation();
2nd program:
1st:**** :new MyItemEventListener()
2nd***:new MyItemEventListener()
3rd***: new MyItemEventListener()
4th***:light.turnOnRed();
5th***:light.turnOnYellow();
6th***:light.turnOnGreen();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.