Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: The graphics doesn't cover whole window and is moving

  1. #1
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Question The graphics doesn't cover whole window and is moving

    Hi!

    I have tried in different ways to solve this but I have only gotten frustrated . The "Running.jpg" shows what i mean.

    The "Window.png" is where I put it all together and my action listeners. Can anyone see why it is acting this way?

    If you want the code I can paste it here for you, thought it would be easy to go through the images. I will later add some threads to this to make the filled oval move by itself for a certain amount of time.
    Attached Thumbnails Attached Thumbnails The graphics doesn't cover whole window and is moving-running.jpg   The graphics doesn't cover whole window and is moving-window.jpg   The graphics doesn't cover whole window and is moving-graphics.jpg  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    Can anyone see why it is acting this way?
    I missed the description of what the code was doing and what was wrong with it and what you want it to do instead.
    Can you explain?

  3. #3
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I am still experimenting with my program trying to give it some functionality with the help of threads, I will add that later but now my problem is as the first image shows, the graphical component when added to the frame isn't covering the whole frame.

    When the filledOval is moving, the whole graphical component moves not only the filledOval as supposed to.

    This is my code for the window, the graphics are in another program where I have some methods:

    Java Code:
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    
    public class Window extends JFrame {
    	
    	static int[] fx = {360, 500, 640};
    	static int[] fy = {40, 90, 140, 190};
    	
    	static JButton b1 = new JButton("Flytta till höger");
    	static JButton b2 = new JButton("Flyttan neråt");
    	static JButton b3 = new JButton("Flyttan till vänster");
    	static JButton b4 = new JButton("Flyttan uppåt");
    	static JButton b5 = new JButton("Slump rörelse");
    	static JButton b6 = new JButton("Börja om");
    	static JSlider s1 = new JSlider(0, 0, 100, 1);
    	
    	static Window w;
    	static Graphics g;
    	static JFrame f = new JFrame();
    	
    	public static void main(String []args){
    		g = new Graphics();	
    		w = new Window();
    		//Add max 6 buttons in a 2x3 column
    		int width = fx[1] - fx[0];
    		int height = fy[1] - fy[0];
    			b1.setBounds(fx[0],fy[0],width,height);
    			b4.setBounds(fx[1],fy[0],width,height);
    			b3.setBounds(fx[0],fy[1],width,height);
    			b2.setBounds(fx[1],fy[1],width,height);
    			b5.setBounds(fx[0],fy[2],width,height);
    			b6.setBounds(fx[1],fy[2],width,height);
    		//Config slider s1
    		s1.setPaintTicks(true);
    		s1.setSnapToTicks(false);
    		s1.setMajorTickSpacing(5);
    		s1.setBounds(0, 700, 400, 40);
    		f.setSize(640,800);
    		//Add components to f
    		f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(s1);f.add(g);
    		f.setResizable(true); f.setVisible(true);
    		f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		//Add action listeners to components
    		b1.addActionListener(w.new Handler());
    		b2.addActionListener(w.new Handler());
    		b3.addActionListener(w.new Handler());
    		b4.addActionListener(w.new Handler());
    		b5.addActionListener(w.new Handler());
    		b6.addActionListener(w.new Handler());
    		s1.addChangeListener(w.new Handler());
    		g.addKeyListener(w.new Handler());
    		f.addKeyListener(w.new Handler());
    	}
    	public class Handler implements ActionListener, ChangeListener, KeyListener{
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()== b1){
    				g.moveRight();
    			}
    			else if(e.getSource()== b2){
    				g.moveDown();
    			}
    			else if(e.getSource()== b3){
    				g.moveLeft();
    			}
    			else if(e.getSource()== b4){
    				g.moveUp();
    			}
    			if(e.getSource() == b6){
    				g.setXY(5, 50);System.out.println("t");
    			}
    			f.repaint();
    		}
    		public void stateChanged(ChangeEvent e) {
    			
    			if(e.getSource() == s1)
    				g.setMove(s1.getValue());
    			f.repaint();
    		}
    		//Doesn't work yet but this isn't main problem
    		public void keyPressed(KeyEvent k) {
    			System.out.println("TEST");
    			while(k.getKeyCode() == KeyEvent.VK_RIGHT){
    				g.moveRight();
    				f.repaint();
    			}
    				
    			
    			
    		}
    		
    		public void keyReleased(KeyEvent k) {
    			System.out.println("TEST");
    			if(k.getKeyCode() == KeyEvent.VK_RIGHT){
    				while(k.getKeyLocation() == KeyEvent.VK_RIGHT){
    					
    					g.moveRight();
    					f.repaint();
    				}
    			}
    			
    		}
    		
    		public void keyTyped(KeyEvent k) {
    			System.out.println("TEST");
    			while(k.getKeyCode() == KeyEvent.VK_RIGHT){
    				g.moveRight();
    				f.repaint();
    			}
    			
    		}
    	}
    }



    This is the other program, you could copy paste and test it, it works, but not as intended:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Graphics extends JPanel{
    	
    	static private int x=5; 
    	static private int y=50;
    	private int c = 0;
    	
    	public void paintComponent(java.awt.Graphics g){
    		super.paintComponent(g);
    		setBackground(Color.BLACK);
    		g.setColor(Color.GREEN);
    		g.drawOval(100, 100, 50, 80);
    		g.fillOval(x, y, 10, 10);
    		
    	}
    	//Move methods
    	public void moveRight(){
    		x += 1 + c;
    	}
    	public void moveLeft(){
    		x -= (1 + c);		
    	}
    	public void moveDown(){
    		y += 1 + c;	
    	}
    	public void moveUp(){
    		y -= (1 + c);
    	}
    	//Set methods
    	public void setMove(int i){
    		c = i;
    	}
    	public void setXY(int x, int y){
    		this.x=x;
    		this.y=y;
    		repaint();
    	}
    	//Get methods
    	public int getX(){
    		return x;
    	}
    	public int getY(){
    		return y;
    	}	
    }
    Last edited by TheTF; 07-26-2011 at 09:49 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    • You should avoid giving your classes or variables names that conflict with standard Java classes. For instance your Graphics JPanel has a very confusing name.
    • Be careful when extending a class that has many methods as you may unintentionally override a critical method or two with unintended but significant consequences. For instance in your code above, I'd strongly consider renaming the getX() and getY() methods to something like getMyX() and getMyY().
    • Please use code tags whenever you post code in this forum so that your code is in fact readable. The link in my signature below will tell you how to do this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    Also look at your usage of JFrames. You have two that you create: w and f.

    You should remove all the static modifiers except for the main() method. Move the code in main() to a constructor.

  6. #6
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for all of your help! It works perfectly now! Sorry for the delay but I am very thankful for your help!

    Now I can continue and further develop this little basic program to hopefully involve some threading also


    Also I can't make the changes, moving the code from main() to an constructor and removing the static modifiers because I get many errors and it's not easy to fix.
    And now my keyEvents don't work yet, any suggestions?
    Last edited by TheTF; 07-23-2011 at 12:55 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    I get many errors a
    Post your code and errors.

    now my keyEvents don't work
    Do you mean the key listeners?

  8. #8
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class Window extends JFrame {
    	
    	int[] fx = {360, 500, 640};
    	int[] fy = {40, 90, 140, 190};
    	
    	JButton b1 = new JButton("Flytta till höger");
    	JButton b2 = new JButton("Flyttan neråt");
    	JButton b3 = new JButton("Flyttan till vänster");
    	JButton b4 = new JButton("Flyttan uppåt");
    	JButton b5 = new JButton("Slump rörelse");
    	JButton b6 = new JButton("Börja om");
    	JButton b7 = new JButton("Automatisk");
    	JButton b8 = new JButton("Stoppa");
    	JSlider s1 = new JSlider(0, 0, 100, 1);
    	Dimension d = new Dimension(360, 400);
    
    	static Window w;
    	Grap g;
    	JFrame f = new JFrame();
    	
    	public Window(){
    		g = new Grap();	
    		//Add max 6 buttons in a 2x3 column
    		int width = fx[1] - fx[0];
    		int height = fy[1] - fy[0];
    			b1.setBounds(fx[0],fy[0],width,height);
    			b4.setBounds(fx[1],fy[0],width,height);
    			b3.setBounds(fx[0],fy[1],width,height);
    			b2.setBounds(fx[1],fy[1],width,height);
    			b5.setBounds(fx[0],fy[2],width,height);
    			b6.setBounds(fx[1],fy[2],width,height);
    			b7.setBounds(fx[0],fy[3],width,height);
    			b8.setBounds(fx[1],fy[3],width,height);
    		//Config slider s1
    		s1.setPaintTicks(true);
    		s1.setSnapToTicks(false);
    		s1.setMajorTickSpacing(5);
    		s1.setBounds(0, 700, 400, 40);
    		f.setSize(640, 800);
    		//Add components to f
    		f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(s1);f.add(g);
    		f.setResizable(true); f.setVisible(true);
    		f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		//Add action listeners to components
    		b1.addActionListener(w.new Handler());
    		b2.addActionListener(w.new Handler());
    		b3.addActionListener(w.new Handler());
    		b4.addActionListener(w.new Handler());
    		b5.addActionListener(w.new Handler());
    		b6.addActionListener(w.new Handler());
    		b7.addActionListener(w.new Handler());
    		b8.addActionListener(w.new Handler());
    		s1.addChangeListener(w.new Handler());
    		f.addKeyListener(w.new Handler());	
    	}
    	
    	public static void main(String []args){
    		w = new Window();
    		
    	}
    	public class Handler extends KeyAdapter implements ActionListener, ChangeListener{
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()== b1){
    				g.moveRight();
    			}
    			else if(e.getSource()== b2){
    				g.moveDown();
    			}
    			else if(e.getSource()== b3){
    				g.moveLeft();
    			}
    			else if(e.getSource()== b4){
    				g.moveUp();
    			}
    			else if(e.getSource()== b5){
    				g.slump(d.getSize());
    				
    			}
    			else if(e.getSource()== b7){
    								
    			}
    			else if(e.getSource()== b8){
    					
    			}
    			if(e.getSource() == b6){
    				g.setMyXY(5, 50);System.out.println("t");
    			}
    			System.out.println("x:" + g.getMyX() + " y:" + g.getMyY());
    			f.repaint();
    		}
    		public void stateChanged(ChangeEvent e) {
    			if(e.getSource() == s1)
    				g.setMove(s1.getValue());
    			f.repaint();
    		}
    		public void keyPressed(KeyEvent k) {
    			if(k.getKeyCode() == KeyEvent.VK_RIGHT){
    				g.moveRight();
    			}else if(k.getKeyCode() == KeyEvent.VK_LEFT){
    				g.moveLeft();
    			}else if(k.getKeyCode() == KeyEvent.VK_UP){
    				g.moveUp();
    			}else if(k.getKeyCode() == KeyEvent.VK_DOWN){
    				g.moveDown();
    			}
    			f.repaint();
    			}	
    		public void keyTyped(KeyEvent k) {
    			if(k.getKeyCode() == KeyEvent.VK_RIGHT){
    				g.moveRight();
    			}else if(k.getKeyCode() == KeyEvent.VK_LEFT){
    				g.moveLeft();
    			}else if(k.getKeyCode() == KeyEvent.VK_UP){
    				g.moveUp();
    			}else if(k.getKeyCode() == KeyEvent.VK_DOWN){
    				g.moveDown();
    			}
    			f.repaint();
    		}
    	}	
    }

    I am getting this error when running:
    Exception in thread "main" java.lang.NullPointerException
    at Window.<init>(Window.java:51)
    at Window.main(Window.java:64)

    The window comes up but I can't do anything. So what am I missing?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Better go through the Swing tutorials. You'll find a link in the API of most Swing components, from there you can navigate to the main tutorial page.

    1. Don't use setBounds. Learn to use layout managers.
    2. Don't use a switchyard event listener for disparate actions. Use anonymous inner classes. And in the rare case that the actions aren't disparate, don't use multiple instances of the listener.
    3. Learn to use key bindings. Adding a key listener to a top level window doesn't really do anything.
    4. Most instance fields should be private. Not default access.
    5. You have an uninitialized static variable (which is causing your immediate error) that has no place in the scheme of things.
    6. Don't name your classes the same as an existing API class without having a very valid reason to do so.
    7. Your class extends JFrame but is nowhere used as a JFrame.

    There'll be more points if I look for them. It does look like you're trying to run without ever having learned to walk. No surprise that you tripped up.

    db
    Fubarable likes this.

  10. #10
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Okay, I will look at the Swing tutorials.


    I dislike layout managers because I can't put the components where I want to. GridBagLayout is an alternative but we haven't learned this when I was taking the programming course in Java. The setBounds made it easy so thats why I used it, I am lazy

    Anonymous inner classes means that I have to write much code. The way I am doing it means that it is enough with a bunch of if and else if statements. It is more appealing to me than an inner class for every component that should have a functionality.

    I know how to use private but this is my program and I am not worried about other classes getting access to my variables in any way, but I see your point.

    Thanks for the advice

Similar Threads

  1. Any good book to cover the Java API classes?
    By yosi199 in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 12:44 AM
  2. Replies: 4
    Last Post: 06-08-2010, 12:35 AM
  3. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  4. Why doesn't this create a window?
    By Addez in forum New To Java
    Replies: 7
    Last Post: 09-17-2009, 05:15 PM
  5. Scrollbar on graphics output window
    By ann123 in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2009, 08:47 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
  •