就是java swing编程
实现;界面上有两个单选框:圆圈和正方形四个按牛:上,下,左,右选中哪个图形点方向按牛,哪个图形就能相应移动就这样
请大家帮帮忙好吗?
我的msn:
[email protected]
在线的,请帮帮忙需要3小时内做出来,谢谢

解决方案 »

  1.   

    import java.awt.*;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D ;import javax.swing.*;
    /*
     * Created on 2006-2-21
     *
     * To change the template for this generated file go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    /**
     * @author IBM
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    public class MoveShape implements ActionListener
    {
    JFrame mainFrame;
    JPanel buttonPanel,radioPanel,downPanel;
    RSPanel mainPanel;
    JRadioButton roundRB,squareRB;
    ButtonGroup group;
    JButton left,right;
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenWidth = screenSize.width;
    int screenHeight = screenSize.height;
    public MoveShape()
    {
    mainFrame = new JFrame("Բԫ·½");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setBounds(screenWidth / 8, screenHeight / 8,6*screenWidth / 8, 6*screenHeight / 8);
    Container contentPane=mainFrame.getContentPane();
    mainPanel= new RSPanel(screenWidth/2,screenHeight/2);
    contentPane.add(mainPanel,BorderLayout.CENTER);
    buttonPanel=new JPanel();
    radioPanel= new JPanel();
    downPanel= new JPanel();
    roundRB = new JRadioButton("Բ");
    roundRB.setSelected(true);
    squareRB = new JRadioButton("·½");
    group=new ButtonGroup();
    group.add(roundRB);
    group.add(squareRB);
    radioPanel.add(roundRB,BorderLayout.WEST);
    radioPanel.add(squareRB,BorderLayout.EAST);
    downPanel.add(radioPanel,BorderLayout.NORTH);
    left= new JButton("<<");
    left.addActionListener(this);
    right= new JButton(">>");
    right.addActionListener(this);
    buttonPanel.add(left);
    buttonPanel.add(right);
    downPanel.add(buttonPanel,BorderLayout.SOUTH);
    contentPane.add(downPanel,BorderLayout.SOUTH);
    //mainFrame.pack();
    mainFrame.setVisible(true);

    }
    public static void createAndShowGUI()
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    MoveShape ms=new MoveShape();
    }
    public static void main(String[] args)
    {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    } /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent event)
    {
    // TODO Auto-generated method stub
    if(event.getSource()==left)
    {
    if(roundRB.isSelected())
    mainPanel.moveRS(true,true);
    else
    mainPanel.moveRS(false,true);
    }
    else if(event.getSource()==right)
    {
    if(roundRB.isSelected())
    mainPanel.moveRS(true,false);
    else
    mainPanel.moveRS(false,false);
    }
    }
    }
    class RSPanel extends JPanel
    {
    public final int ROUNDY=300,SQUAREY=100,MOVESPEED=20;
    int roundCenter,squareCenter,width;
    public RSPanel(int r,int s)
    {
    super();
    this.setBounds(0,0,r*2,s*2-200);
    roundCenter=r;
    squareCenter=r;
    width=s;
    }
    public void paintComponent(Graphics g)
    {
    Graphics2D g2=(Graphics2D)g;
    g2.setBackground(Color.BLUE);
    super.paintComponent(g);
    g2.setColor(Color.BLUE);
    g2.fill(new Ellipse2D.Double(roundCenter,ROUNDY,50,50));
    g2.setColor(Color.BLUE);
    g2.fill(new Rectangle2D.Double(squareCenter,SQUAREY,50,50));
    }
    public void moveRS(boolean round,boolean left)
    {
    if(round)
    {
    if(left)
    {
    if(roundCenter-MOVESPEED>0)
    roundCenter-=MOVESPEED;
    else
    roundCenter=0;
    }
    else
    {
    if(roundCenter+MOVESPEED<width)
    roundCenter+=MOVESPEED;
    else
    roundCenter=width;
    }
    }
    else
    {
    if(left)
    {
    if(squareCenter-MOVESPEED>0)
    squareCenter-=MOVESPEED;
    else
    squareCenter=0;
    }
    else
    {
    if(squareCenter+MOVESPEED<width)
    squareCenter+=MOVESPEED;
    else
    squareCenter=width;
    }
    }
    this.paint(this.getGraphics());
    }
    }数字没有控制好,