Results 1 to 8 of 8
  1. #1
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default Whats wrong with my program?

    My program is supposed to make a 'flying saucer' move up, down, left, right, depending on what buttons are clicked.
    First here is the SaucerDriver:
    Java Code:
    public class SaucerDriver{
    
      public static void main(String[] args){
        DisplayWindow d = new DisplayWindow();
        SaucerPanel p = new SaucerPanel();
        d.addPanel(p);
        d.showFrame();
      }
    }
    here is the Display window:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public  class DisplayWindow extends JFrame{
      private Container c;
      public DisplayWindow(){
        super("Display");
        c = this.getContentPane();
      }
      public void addPanel(JPanel p){
        c.add(p);
      }
      public void showFrame(){
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }
    Here is my program:
    Java Code:
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; 
    
    public class SaucerPanel  extends JPanel implements ActionListener{
    static final long serialVersionUID = 1;
    
     JButton up = new JButton("Up"); 
      JButton down = new JButton("Down");
      JButton left = new JButton("Left");
      JButton right = new JButton("Right");
      JButton quit = new JButton("Quit");
      
     public SaucerPanel(){ 
        setPreferredSize(new Dimension(500,400));
        setBackground(Color.red);
        this.add(up);
        up.addActionListener(this);  
        this.add(down); 
        down.addActionListener(this);
        this.add(left); 
        left.addActionListener(this);
        this.add(right); 
        right.addActionListener(this);
        this.add(quit); 
        quit.addActionListener(this);
      }
     
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawLine(0,350,500,350);
    g.drawRect(200,180,30,170); 
    g.drawRect(350,240,15,110);
    g.setColor(Color.black);
    g.fillOval(10,180,100,30);
    g.drawString("hovering...",10,230);
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == quit)
          System.exit(0);
        else if(e.getSource() == up){ 
           xpos = x;
           ypos = y-5;
           g.drawString("hovering...",xpos,ypos-5);
           repaint();
        }
         else if(e.getSource() == right){ 
           xpos = x+5;
           ypos = ypos;
           g.drawString("hovering...",xpos+5,ypos);
           repaint();
         }
         else if(e.getSource() == left){ 
           xpos = x-5;
           ypos = ypos;
           g.drawString("hovering...",xpos-5,ypos);
           repaint();
         }
           else if(e.getSource() == down){ 
    
           xpos = x;
           ypos = ypos+5;
           g.drawString("hovering...",xpos,ypos+5);
           repaint();
         }
    }
       }
    the errors I am getting reside in the actionPerformed method: I get an error each xpos or ypos line that reads:
    "cannot find symbol
    symbol : variable xpos
    location: class SaucerPanel"
    or
    "x is not public in java.awt.Component; cannot be accessed from outside package"

    Can anyone help me and tell me what my issue here is. I have been trying to get the code to compile correctly for a while now and I'm having issues...clearly.

  2. #2
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    thats because in your code nowhere do you declare the two variables nor assign them values they are given values inside a loop but the values they take on have not been specified:

    Java Code:
           xpos = x;
           ypos = y-5;
    where do you define values for x and y??? if need x of the obj than you need something like getx() since x doesn't have public visibility.

  3. #3
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Ah thanks!
    now I fixed that and now my only errors are for every g.drawString("hovering...")
    i get an error that reads
    cannot find symbol
    symbol : variable g
    location: class SaucerPanel
    Which makes sense because this is in the actionPerformed method not the paintComponent method. but how will i be able to make it so that the hovering string moves when the oval moves too?

  4. #4
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Also, I just removed all of the g.drawStrings and it compiled but when I tried to run it I got this error: Static Error: This class does not have a static void main method accepting String[].
    Last edited by jwb4291; 12-12-2010 at 02:17 AM.

  5. #5
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    well ofcourse because you have no functional implementation of what to do when those buttons are pressed. inside your:
    Java Code:
    if(e.getSource == up)
     {
       //you need to define some functionality that happens when this button is clicked.
     }
    basically this is where you need code to move your circle (i.e. saucer) around.

  6. #6
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    how do i merge the xpos,ypos and the moving of the oval, cause i define the ovals position earlier, and idk how to get that to work

  7. #7
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    can't help you there bud; majority and almost all of my java programming experience is non-gui based :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are hard-coding where the hover string is drawn, and so it will of course not move. If you want it to move based on the xpos and ypos, then you must use these variables in the method where you draw the String within paintComponent. The method does take two ints, and perhaps with a slight amount of math, you can draw the Strings where you desire.

Similar Threads

  1. whats wrong with my code?
    By beandip408 in forum New To Java
    Replies: 6
    Last Post: 11-03-2010, 01:01 AM
  2. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 02:55 PM
  3. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 11:56 PM
  4. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:51 AM
  5. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •