Results 1 to 4 of 4
  1. #1
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Javax.Swing - not drawing?

    Hey

    i have made a program that draws an image, i want to make a game.
    the problem is it will not draw the image i have added, it works on another program which i took the code from, then i tried to make my own but no luck!
    tried to change many things yet nothing seems to happen...

    Here is the code:
    image is attached "craft.png"



    Main:
    Java Code:
    package spaceCraft;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.ImageObserver;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Board implements ActionListener
    {
    		
        private Timer timer;
    	private JPanel p1, p2;
    	private Craft craft = new Craft();
    	private Image img;
    	private JFrame f;
    	
    	public Board() 
    	{
    		p1 = new JPanel();		//panels
    		p2 = new JPanel(); 
      
      		f = new JFrame("Space Craft v1.0");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setBounds(100, 100, 800, 600);
    		f.setVisible(true);
    		f.setResizable(false);
    		
    		f.add(p1);
    		p1.setVisible(true);
    		p1.setBackground(Color.BLACK);
    		
            timer = new Timer(5, this);
            timer.start();
    	}
    	
    
    	public void paint(Graphics2D g)				
    	{
    		
    		
    		Graphics2D gfx = (Graphics2D)g; 
    		gfx.drawImage(craft.getImg(), craft.getX(), craft.getY(), p1);
    		
    //		f.paint(g);
    		p1.paint(g);
    		
    	}
    	
    	public static void main(String[] args) 		//Main
    	{ new Board(); }
    
    
    
    	public void actionPerformed(ActionEvent e) 	// Actions on Key Press - To Do
    	{
    		
    	}
    
    
    }

    Sub - image loader

    Java Code:
    package spaceCraft;
    
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    
    
    
    public class Craft
    {
    
    	private String craft = "craft.png";
    
    	private int x = 1, y = 1;
    	public int dx = 0, dy = 0, width, height;
    	Image img;
    	
    
    	
    	public Craft()
    	{
    		ImageIcon ii = new ImageIcon(getClass().getResource(craft));
    		img = ii.getImage();
    		
    		height = img.getHeight(null);
    		width  = img.getWidth (null);
    		
    		
    	}
    
    	//----------------------------------------------------------------
    	//--------------------------- Methods ----------------------------
    	
    	public void move()
    	{
    		x += dx;
    		y += dy;
    		
    		if(x < 0) {x = 0;}
    		if(y < 0) {y = 0;}
    	}
    
    	public int getX() {return x;}
    	public int getY() {return y;}
    	public Image getImg() {return img;}
        public int getWidth () {return width; }
        public int getHeight() {return height;}
    	
        
        
        //------------------------------------------------------------------- 
        //------------------------ Action Listeners -------------------------
        
    	public void KeyPressed(KeyEvent e)
    	{
    		int key = e.getKeyCode();
    		
    		if(key == KeyEvent.VK_SPACE) {fire();}
    		if(key == KeyEvent.VK_UP)	 {dy += 1;}
    		if(key == KeyEvent.VK_DOWN)	 {dy -= 1;}
    		if(key == KeyEvent.VK_LEFT)	 {dx -= 1;}
    		if(key == KeyEvent.VK_RIGHT) {dx += 1;}	
    	}
    	
    	
    	public void fire()
    	{
    		
    	}
    	
    	
    	public void KeyReleased(KeyEvent e)
    	{
    		int key = e.getKeyCode();
    		
    		if(key == KeyEvent.VK_SPACE) {fire();}
    		if(key == KeyEvent.VK_UP)	 {dy = 0;}
    		if(key == KeyEvent.VK_DOWN)	 {dy = 0;}
    		if(key == KeyEvent.VK_LEFT)	 {dx = 0;}
    		if(key == KeyEvent.VK_RIGHT) {dx = 0;}
    	}
    	
    	
    
    }

    i think the error is loading the image to the panel (drawing method) but cant get it right, would appreciate the help.
    also if you give me more than 1 way to draw it would be nice, but please make it simple (just the drawing method along with the definitions of what u use to draw them with/on).

    Thank you
    Attached Thumbnails Attached Thumbnails Javax.Swing - not drawing?-craft.png  

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Javax.Swing - not drawing?

    I haven't looked over all you code but a major problem is that you are adding an unadulterated JPanel to the JFrame. This means it is using the paint method from JPanel and not your Board class. Nor will it process action events since it is Board that is implementing ActionListener. I recommend you subclass JPanel via Board and add a Board instance to the JFrame. You should also override paintComponent(Graphics g) and not paint(Graphics g). And you should use SwingUtilities.invokeLater to invoke your Swing app on the Event Dispatch Thread.

    Edit: I just noticed that you are using Graphics2D g as a parameter to the paint method. It should be Graphics g and as a parameter to the paintComponent method. Of course, you said you copied this out of a working example so perhaps I am missing something.

    Regards,
    Jim
    Last edited by jim829; 04-22-2013 at 10:22 PM. Reason: addional info
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    bakriawad is offline Member
    Join Date
    Apr 2013
    Posts
    22
    Rep Power
    0

    Default Re: Javax.Swing - not drawing?

    i tried what you said, but it wont even show me the panel anymore? what am i doing wrong here?

    Main
    Java Code:
    package spaceCraft;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    
    
    
    public class Main extends JFrame implements ActionListener
    {
    
    	public Main() 
    	{
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 800, 600);
    		setVisible(true);
    		setResizable(false);
    		
    		 add(new Board()); 
    	}
    
    	
    	public static void main(String[] args) 		//Main
    	{
    		new Main();
    	}
    
    	
    	public void actionPerformed(ActionEvent e) 
    	{	
    		
    	}
    
    }
    Panel
    Java Code:
    package spaceCraft;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.ImageObserver;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Board extends JPanel implements ActionListener
    {
    		
        private Timer timer;
    	private Craft craft = new Craft();
    	private Image img;
    
    	
    	public Board() 
    	{
    		setFocusable(true);
    		setVisible(true);
    		setBackground(Color.BLACK);
    
    		
            timer = new Timer(5, this);
            timer.start();
    	}
    	
    
    	public void actionPerformed(ActionEvent e) 	// Actions on KeyPress - To Do
    	{
    		
    	}
    
    
    }

    JFrame does not turn black, which is the background color... so the panel is not loaded...
    somehow i cant seem to be able to fix that...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Javax.Swing - not drawing?

    You don't need to (and shouldn't) extend JFrame. Just use it like you did before. In you Board constructore, do f.add(this).
    Your constructor should look something like this:

    Java Code:
    public Board() 
        {
           
            f = new JFrame("Space Craft v1.0");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setPreferredSize(new Dimension(600,600));
            f.setResizable(false);
            f.add(this);
            f.pack();
            setBackground(Color.BLACK);
            f.setVisible(true);
             
             
            timer = new Timer(5, this);
            timer.start();
        }
    Your paintComponent method should look something like this:

    Java Code:
    public void paintComponent(Graphics g)             
        {
            super.paintComponent(g);
            Graphics2D gfx = (Graphics2D)g; 
            gfx.drawImage(craft.getImg(), craft.getX(), craft.getY(), null);
        }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Relationship Between java.awt and javax.swing
    By awinston in forum AWT / Swing
    Replies: 5
    Last Post: 07-21-2012, 05:03 AM
  2. javax.swing.table
    By asai in forum AWT / Swing
    Replies: 2
    Last Post: 02-01-2012, 08:33 PM
  3. Error in Javax.Swing.ImageIcon
    By raj.mscking@gmail.com in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2012, 12:14 PM
  4. package javax.swing does not exist
    By timosoft in forum AWT / Swing
    Replies: 5
    Last Post: 02-15-2011, 01:17 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Tags for this Thread

Posting Permissions

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