Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I think one class is over riding the other one?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I think one class is over riding the other one?

    Hey guys! I'm new to Java and really like learning it! But I've encountered a little snag. I wrote a small program and for some reason it's only executing two of the three classes. Here is the code:

    Main Method class:

    import javax.swing.JFrame;
     
    public class main
    {
      public static void main(String[] args)
      {
        JFrame frame = new JFrame();
        Infout m = new Infout();
        obj2 o = new obj2();
     
        frame.add(o);
        frame.add(m);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(3);
        frame.setSize(300, 400);
        frame.setTitle("Circle");
     
     
      }
    }

    Class that draws a keyboard-controlled circle.

    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
     
    	public class Infout extends JPanel implements ActionListener, KeyListener {
     
    		Timer t = new Timer(5, this);
    		double x = 0, y = 0, velx = 0, vely = 0;
     
    		public Infout(){
     
    			t.start();
    			addKeyListener(this);
    			setFocusable(true);
    			setFocusTraversalKeysEnabled(false);
    		}
     
     
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			Graphics2D g2 = (Graphics2D) g;
    			g2.fill(new Ellipse2D.Double(x, y, 40, 40));
    			g2.fill(new Rectangle2D.Double(0, 270, 300, 5));
    			g2.fill(new Rectangle2D.Double(140, 270, 5, 300));
    			g2.fill(new Rectangle2D.Double(140, 60, 70, 5));
    			g2.fill(new Rectangle2D.Double(50, 140, 5, 70));
    			g2.fill(new Rectangle2D.Double(150, 130, 5, 40));
    			g2.fill(new Rectangle2D.Double(190, 210, 40, 5));
    			if (x >= 120 && y >= 270) {
    	    		System.out.println("sum1 has reached tha corner");
    	    		g.drawString("You win!",115,35);
    	    	}
    			if (x <= 120 && y >= 270) {
    	    		System.out.println("sum1 has reached tha corner");
    	    		g.drawString("You lose!",115,35);
    	    	}
    			if (x == 120 && y >= 270){
    				velx = 0;
    				vely = 0;
    			}
    			if (x == 31.5 && y <= 200 && y >= 100){
    				velx = 0;
    			}
    			if (x == 132 && y <= 170 && y >= 100){
    				velx = 0;
    			}
    			if (x <= 190 && x >= 120 && y == 42){
    				velx = 0;
    			}
    			if (x <= 210 && x >= 171 && y == 192){
    				velx = 0;
    			}
     
    		}
     
     
    		public void actionPerformed(ActionEvent e) {
     
    			System.out.println("x "+x+"y "+y);
     
    			repaint();
    			x += velx;
    			y += vely;
     
    			if (x < 0 || x > 260)
    	    	{
    	    		velx = 0;
    	    		vely = 0;
    	    	}
    	    	if (y < 0 || y > 340)
    	    	{
    	    		velx = 0;
    	    		vely = 0;
    	    	}
    		}
     
    		public void up() {
    			vely = -1.5;
    			velx = 0;
    		}
     
    		public void down() {
    			vely = 1.5;
    			velx = 0;
    		}
     
    		public void left() {
    			velx = -1.5;
    			vely = 0;
    		}
     
    		public void right() {
    			velx = 1.5;
    			vely = 0;
    		}
     
    		public void keyPressed(KeyEvent e) {
    			int code = e.getKeyCode();
    			if (code == KeyEvent.VK_UP) {
    				up();
    			}
    			if (code == KeyEvent.VK_DOWN) {
    				down();
    			}
    			if (code == KeyEvent.VK_RIGHT) {
    				right();
    			}
    			if (code == KeyEvent.VK_LEFT) {
    				left();
    			}
     
    		}
     
    		public void keyTyped(KeyEvent e) {}
    		public void keyReleased(KeyEvent e) {}
    	}

    Class that's supposed to draw a static circle but for some reason, is not showing up on the JFrame :

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JPanel;
     
     
    public class obj2 extends JPanel {
     
    	 public void paintComponent(Graphics g1)
    	  {
    		  super.paintComponent(g1);
    	    Graphics2D g3 = (Graphics2D)g1;
    	    Ellipse2D circle = new Ellipse2D.Double(50.0D, 50.0D, 40.0D, 40.0D);
    	    g3.fill(circle);
    	  }
    }

    So basically, main.java and Infout.java work, but obj2.java is not executing at all. It's supposed to draw a circle on the JFrame but isn't. Am I doing something wrong?

    Thanks!

    - Ab


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    221
    My Mood
    Amazed
    Thanks
    5
    Thanked 42 Times in 41 Posts

    Default Re: I think one class is over riding the other one?

    For testing purposes, when you run it and you see the frame empty, grab the corner of the frame and stretch it out to a larger size to see if the drawing appears. If it does then I might be able to help you.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I think one class is over riding the other one?

    Quote Originally Posted by A b View Post
        Infout m = new Infout();
        obj2 o = new obj2();
     
        frame.add(o);
        frame.add(m);
    The content pane of JFrame has, by default, a BorderLayout. A BorderLayout subdivides the space in only 5 areas and only one component can be shown in each area.

    Doing:
    frame.add(xyz);
    is equivalent (since Java 5) to:
    frame.getContentPane().add(xyz, BorderLayout.CENTER);

    So you are adding two things in the same area. Please, learn layout managers, otherwise you won't go very far in GUI apps with AWT/Swing.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 09:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 04:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 07:22 PM