Domanda

I for some reason, unknown to me, I cannot figure out how to change the color of the ovals. They stay black even though I have set them up to be red and blue. Thanks for any help.

ControlPanel class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFrame;

public class ControlPanel extends JPanel
{

   private BallPanel ball1, ball2;
   //declare all your components here
   private JButton upR, downR, leftR, rightR, stopR, upB, downB, leftB, rightB, stopB;
   private JSlider delayR, delayB;
   private BallPanel redC, blueC;
   private int DIAMETER = 30;
   private JPanel redButtons, blueButtons, sliderR, sliderB, top, bottom, left, canvasCY;
   private JLabel redDelay, blueDelay;

  public ControlPanel(int width, int height)
   {
       width = 450;
       height = 300;

       //create 2 ball panels
        redC = new BallPanel(0,10,Color.red,Color.cyan);
        blueC = new BallPanel(0,10,Color.blue,Color.yellow);


      //create 10 buttons
     upR = new JButton("Up Red");
     downR = new JButton("Down Red");
     leftR = new JButton("Left Red");
     rightR = new JButton("Right Red");
     stopR = new JButton("Stop Red");
     upB = new JButton("Up Blue");
     downB = new JButton("Down Blue");
     leftB = new JButton("Left Blue");
     rightB = new JButton("Right Blue");
     stopB = new JButton("Stop Blue");

      //create 2 sliders
    delayR=new JSlider(SwingConstants.VERTICAL,0,50,25);
    delayB=new JSlider(SwingConstants.VERTICAL,0,50,25);

       //add the corresponding listener to sliders and buttons
        upR.addActionListener(new ButtonListener());
        downR.addActionListener(new ButtonListener());
        leftR.addActionListener(new ButtonListener());
        rightR.addActionListener(new ButtonListener());
        stopR.addActionListener(new ButtonListener());
        upB.addActionListener(new ButtonListener());
        downB.addActionListener(new ButtonListener());
        leftB.addActionListener(new ButtonListener());
        rightB.addActionListener(new ButtonListener());
        stopB.addActionListener(new ButtonListener());

       //organize 5 buttons into a panel using grid layout
        redButtons= new JPanel(new GridLayout(5,1));
        redButtons.add(upR);
        redButtons.add(downR);
        redButtons.add(leftR);
        redButtons.add(rightR);
        redButtons.add(stopR);

       //organize 5 buttons into a panel using grid layout
        blueButtons= new JPanel(new GridLayout(5,1));
        blueButtons.add(upB);
        blueButtons.add(downB);
        blueButtons.add(leftB);
        blueButtons.add(rightB);
        blueButtons.add(stopB);

       //create 2 labels
        redDelay = new JLabel("Red Ball Delay");
        blueDelay = new JLabel("Blue Ball Delay");
       //organize a label and a slider into a panel using border layout
        sliderR = new JPanel(new BorderLayout());
        sliderR.add(redDelay, BorderLayout.NORTH);
        sliderR.add(delayR, BorderLayout.SOUTH);
       //organize the panel containing buttons and the panel with a slider
        top = new JPanel(new GridLayout(1,2));
        top.add(redButtons);
        top.add(sliderR);
       //organize a label and a slider into a panel using border layout
        sliderB = new JPanel(new BorderLayout());
        sliderB.add(blueDelay, BorderLayout.NORTH);
        sliderB.add(delayB, BorderLayout.SOUTH);
       //organize the panel containing buttons and the panel with a slider
        bottom = new JPanel(new GridLayout(1,2));
        bottom.add(blueButtons);
        bottom.add(sliderB);

        left=new JPanel(new GridLayout(2,1));
        left.add(top);
        left.add(bottom);

        canvasCY = new JPanel(new GridLayout(2,1));
        canvasCY.add(redC);
        canvasCY.add(blueC);

        JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, canvasCY);
        setLayout(new GridLayout(0,1));
        add(sp);
        sp.setVisible(true);

    }


  //The ButtonListener class defines actions to be taken in case
  //each of 10 buttons are pushed.
  private class ButtonListener implements ActionListener
   {
       public void actionPerformed(ActionEvent event)
        {
            Object action = event.getSource();

            //if the up button for the red ball is pushed.

             if (event.getSource() == upR)
                            redC.up();
                        else if (event.getSource() == downR)
                            redC.down();
                        else if (event.getSource() == leftR)
                            redC.left();
                        else if (event.getSource() == rightR)
                            redC.right();
                        else if (event.getSource() == stopR)
                            redC.suspend();

            if (event.getSource() == upB)
                            blueC.up();
                        else if (event.getSource() == downB)
                            blueC.down();
                        else if (event.getSource() == leftB)
                            blueC.left();
                        else if (event.getSource() == rightB)
                            blueC.right();
                        else if (event.getSource() == stopB)
                            blueC.suspend();
         }
     } //end of ButtonListener

   //The SliderListener defines actions to be taken in case
   //each of the 2 sliders is moved by a user
   private class SliderListener implements ChangeListener
    {
        public void stateChanged(ChangeEvent event)
         {

         }

     } //end of SliderListener

}

BallPanel class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Color;
import java.awt.Graphics;

public class BallPanel extends JPanel
    {
        int x;
        int y;
        Color ballColor;
        Color backColor;
        Timer timer;
        int delay;
        int stepX=3;
        int stepY=0;
        final int CIRCLE_DIAMETER = 20;

        public BallPanel(int x, int y, Color ballColor, Color backColor)
        {
            this.x=x;
            this.y=y;
            this.ballColor=ballColor;
            this.backColor=backColor;
            delay=20;
            stepX=3;
            stepY=0;
            timer= new Timer(delay, new MovingBallListener());
            timer.start();
            repaint();
            }
        public void up()
        {
            stepX=0;
            stepY=-3;
            repaint();
            }
        public void down()
        {
            stepX=0;
            stepY=3;
            repaint();
            }
        public void left()
        {
            stepX=-3;
            stepY=0;
            repaint();
            }
        public void right()
        {
            stepX=3;
            stepY=0;
            repaint();
            }
        public void suspend()
        {
            stepX=0;
            stepY=0;
            repaint();
            }
        public void setDelay(int delayNum)
        {
            timer.setDelay(delayNum);
            }
        public void paintComponent(Graphics page)
        {
            super.paintComponent(page);

            page.fillOval(x,y,CIRCLE_DIAMETER,CIRCLE_DIAMETER);
            page.setColor(ballColor);
            setBackground(backColor);
            }
        private class MovingBallListener implements ActionListener
                {
        public void actionPerformed(ActionEvent event)
        {
            if (x > getSize().getWidth()-CIRCLE_DIAMETER && stepY == 0)
                    stepX=-3;
                    stepY=0;
                    repaint();

            if(x < getSize().getWidth()-CIRCLE_DIAMETER && stepY == 0)
                    stepX=3;
                    stepY=0;
                    repaint();
            }
        }
        }

hw12.html

Assignment 12 Applet


È stato utile?

Soluzione

I believe it has to do with this in javadocs for setColor

"Sets this graphics context's current color to the specified color. All subsequent graphics operations using this graphics context use this specified color."

So flip this

page.fillOval(x,y,CIRCLE_DIAMETER,CIRCLE_DIAMETER);
page.setColor(ballColor);

to be

page.setColor(ballColor);
page.fillOval(x,y,CIRCLE_DIAMETER,CIRCLE_DIAMETER);

so that you are drawing with ballColor

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top