不能改变我的椭圆的颜色

时间:2014-05-03 02:18:21

标签: java graphics jpanel

我出于某种原因,我不知道,我无法弄清楚如何改变椭圆的颜色。即使我把它们设置成红色和蓝色,它们仍保持黑色。谢谢你的帮助。

ControlPanel类

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类

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

作业12小程序


1 个答案:

答案 0 :(得分:0)

我相信它与javadocs中的setColor

有关

&#34;将此图形上下文的当前颜色设置为指定的颜色。使用此图形上下文的所有后续图形操作都使用此指定颜色。&#34;

所以翻转这个

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

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

这样你就可以用ballColor绘图了