DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: x and y axis

  1. #1
    Join Date
    Nov 2006
    Posts
    1

    x and y axis

    i need to get the pictures to incrementaly move across the y axis when the south button is clicked and along the x when the east button is clicked. how would i go about doing this?

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    //button declaration

    public class Lab6 extends Applet implements ActionListener, ItemListener {
    Panel northPanel, southPanel, eastPanel, westPanel;
    MyPanel centerPanel;
    boolean seen=true;
    Button northButton;
    Button southButton;
    Button eastButton;
    CheckboxGroup cbg;
    Checkbox radioButton;
    int fontSize;
    //variables

    boolean flag = true;
    int mid;
    int top;
    int y = 0;
    Color myColor;
    Font f;
    Font g;
    Font h;

    public void init() {
    setBackground(Color.white);

    //setting panels

    northPanel = new Panel();
    southPanel = new Panel();
    centerPanel = new MyPanel();
    eastPanel = new Panel ();
    westPanel = new Panel ();

    f = new Font("Arial", Font.BOLD, 12);
    g = new Font("Times", Font.ITALIC, 10);
    h = new Font("Times", Font.BOLD, 14);

    myColor=new Color(123, 0, 255);

    setLayout(new BorderLayout(1, 1));

    //adds the panels

    add(northPanel, BorderLayout.NORTH);
    add(southPanel, BorderLayout.SOUTH);
    add(centerPanel, BorderLayout.CENTER);
    add(eastPanel, BorderLayout.EAST);
    add(westPanel, BorderLayout.WEST);

    //customizes the buttons


    northButton = new Button("Picture");
    northButton.setFont(f);
    southButton = new Button("X Position");
    southButton.setFont(g);
    eastButton = new Button ("Y Position");
    eastButton.setFont(h);

    cbg = new CheckboxGroup();
    radioButton = new Checkbox("Color 1", cbg, false);

    //action listeners for the buttons

    northPanel.add(northButton);
    northButton.addActionListener(this);

    southPanel.add(southButton);
    southButton.addActionListener(this);

    eastPanel.add(eastButton);
    eastButton.addActionListener(this);

    westPanel.add(radioButton);
    radioButton.addItemListener(this);

    }

    //the action performed event for buttons

    public void actionPerformed(ActionEvent e){
    if (e.getSource() == northButton){
    flag = !flag;
    }

    if (e.getSource() == southButton){

    }

    if (e.getSource() == eastButton) {

    }

    centerPanel.repaint();
    }

    //item state change for radio button

    public void itemStateChanged(ItemEvent e)
    {

    if (e.getSource() == radioButton)
    {
    setBackground(Color.red);
    }

    fontSize=Integer.parseInt(cbg.getSelectedCheckbox().getLabel());
    repaint();
    }
    // drawing the objects in separate methods


    class MyPanel extends Panel{

    private void drawBean (Graphics g, int mid, int top){
    g.setColor(Color.blue);

    g.fillOval(150, 80, 25, 60);
    g.fillOval(153, 140, 20, 100);
    g.fillOval(145, 75, 40, 10);
    g.fillOval(163, 70, 5, 5);


    g.setColor(Color.green);
    g.fillOval(83, 180, 10, 10);
    g.fillOval(219, 125, 10, 10);

    }

    private void drawTraffic (Graphics g, int mid, int top){
    g.setColor(Color.black);
    g.drawRect (mid + 100, top - 100, 50, 150);
    g.setColor (Color.red);
    g.fillOval (mid + 100, top - 100, 50, 50);
    g.setColor (Color.yellow);
    g.fillOval (mid + 100, top - 50, 50, 50);
    g.setColor (Color.green);
    g.fillOval (mid + 100, top, 50, 50);
    }

    public void paint(Graphics g){
    Dimension d = getSize();
    mid = d.width / 2;
    top = d.height / 3;

    if (flag == true){
    drawBean(g, mid, top);
    }
    else{
    drawTraffic(g, mid, top);
    }


    }
    }
    }

  2. #2
    Join Date
    Nov 2006
    Posts
    35
    actually i don't like people who write a piece of code without much considering what it should do... i mean - you did the layout before you did any thinking. anyways... your code throws a number format exception when clicking the option button... and moving around a picture should be easy.. put it in a jlabel and change the jlabels position xPos--/xPos++ and yPos--/yPos++ if the up/down or left/right buttons are pressed...

Similar Threads

  1. Chart scale on "Y" axis
    By clarksu in forum ASP.NET
    Replies: 0
    Last Post: 09-04-2006, 09:51 PM
  2. Session in web services with axis
    By wakeup in forum Java
    Replies: 0
    Last Post: 04-25-2006, 12:07 PM
  3. AXIS C++ Help
    By shawnbruman in forum C++
    Replies: 0
    Last Post: 03-29-2006, 12:44 PM
  4. Replies: 0
    Last Post: 11-14-2005, 08:58 PM
  5. Creating multiple asynchrous responses using axis
    By Kai Weingärtner in forum Java
    Replies: 0
    Last Post: 01-24-2003, 09:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links