13 Replies - 287 Views - Last Post: 09 February 2013 - 08:51 PM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Can't draw on JPanel

Posted 08 February 2013 - 02:00 AM

I want to add background image to my JApplet application. But it doesn't work. Background is still gray. I tried to use the same JPanel class with normal application and it works. Why with JApplet I can't do this task?

In JPanel I'm using paintComponent() method. If I for example change panel's colour in constructor, then everything works fine, but when I try to drawImage in paintComponent, program breaks.

This post has been edited by novakasss: 08 February 2013 - 02:01 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Can't draw on JPanel

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Can't draw on JPanel

Posted 08 February 2013 - 03:52 AM

Provide an example of the code that is not providing the desired results. The shorter the example, the better.
Was This Post Helpful? 0
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Can't draw on JPanel

Posted 08 February 2013 - 05:04 AM

View PostGregBrannon, on 08 February 2013 - 03:52 AM, said:

Provide an example of the code that is not providing the desired results. The shorter the example, the better.

Main. (Extends JApplet)
public void grafika(){
		gui = new GUI();
		setContentPane(gui);
		setSize(300,200);
	}


GUI class :
public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.drawImage(bg, 0,0, this.getWidth(), this.getHeight(), this);
	}

public GUI() {
bg = new ImageIcon("Fonas.jpg").getImage();
}


This post has been edited by novakasss: 08 February 2013 - 05:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Can't draw on JPanel

Posted 08 February 2013 - 05:07 AM

A minimalist. A runnable example would have been nice.

What's the init() method look like?
Was This Post Helpful? 0
  • +
  • -

#5 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Can't draw on JPanel

Posted 08 February 2013 - 05:30 AM

View PostGregBrannon, on 08 February 2013 - 05:07 AM, said:

A minimalist. A runnable example would have been nice.

What's the init() method look like?

public void init(){
		try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                	prisijungtiPrieBazes();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
            e.printStackTrace();
        }
	}


prisijungtiPrieBazes():
public void prisijungtiPrieBazes(){
		try{
			String drive = "com.mysql.jdbc.Driver";
			Class.forName(drive).newInstance();
			String url = "jdbc:mysql://sportoviktorina.lt/sportovikt_db";
			String userName = "sportovikt_base"; 
			String password = "xxxx";
			con = DriverManager.getConnection(url, userName, password);
			grafika();
			pradeti();
		}catch(Exception e){
			e.printStackTrace();
		}
	}


grafika() method you know.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Can't draw on JPanel

Posted 08 February 2013 - 05:40 AM

I'm wondering what this.getWidth() and this.getHeight() return. You might want to check those values.
Was This Post Helpful? 0
  • +
  • -

#7 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Can't draw on JPanel

Posted 08 February 2013 - 05:44 AM

View PostGregBrannon, on 08 February 2013 - 05:40 AM, said:

I'm wondering what this.getWidth() and this.getHeight() return. You might want to check those values.

Zeroes. ://>/> So what I need to use? Those 300,200?

EDIT: I changed them with 300,200. Same result.

This post has been edited by novakasss: 08 February 2013 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Can't draw on JPanel

Posted 08 February 2013 - 07:39 AM

So? It is impossible to do this? I guess that, because nobody knows how to fix this.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: Can't draw on JPanel

Posted 08 February 2013 - 08:36 AM

In general, you should not be drawing on root components (JFrame, JApplet) because they have sophisticated code involved in them, and they have a content pane within tem. So even if you do succeed in drawing, there is a panel on top of it. You should make your own JPanel that you do your drawing on and then add that component to your JFrame or JApplet.
Was This Post Helpful? 0
  • +
  • -

#10 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 332
  • Joined: 11-July 12

Re: Can't draw on JPanel

Posted 08 February 2013 - 09:38 AM

View PostDogstopper, on 08 February 2013 - 08:36 AM, said:

In general, you should not be drawing on root components (JFrame, JApplet) because they have sophisticated code involved in them, and they have a content pane within tem. So even if you do succeed in drawing, there is a panel on top of it. You should make your own JPanel that you do your drawing on and then add that component to your JFrame or JApplet.

I said before that GUI class extends JPanel so I'm doing exactly the same thing as you told here :D
Was This Post Helpful? 0
  • +
  • -

#11 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Can't draw on JPanel

Posted 08 February 2013 - 10:03 AM

You said it works in an application, but not an applet? Try printing out the name of the Image after you create it in the applet to see if it actually found the image.

P.S. Is it just drawImage() that doesn't work, or can't you draw lines and shapes as well?
Was This Post Helpful? 0
  • +
  • -

#12 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Can't draw on JPanel

Posted 08 February 2013 - 10:04 AM

As my girlfriends always tell me before they dump me, size matters. Give your JPanel a size and see if that helps.
Was This Post Helpful? 1
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Can't draw on JPanel

Posted 09 February 2013 - 06:19 AM

The code below is what I had in mind when I asked for a short example that demonstrates the problem, but this one demonstrates a possible solution. It's not impossible, but very difficult, to help you with your 'broken' code, if we can't see it or understand it. The results from discussing theory are widely variable.
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JPanel;

// SimpleApplet demonstrates a JApplet with a JPanel as its main content
// and the JPanel has a background image
public class SimpleApplet extends JApplet
{
    // the init() method builds the JApplet
    @Override
    public void init()
    {
        AppletJPanel aPanel = new AppletJPanel();

        getContentPane().add( aPanel );
    }
    
} // end class SimpleApplet

// AppletJPanel is the main content of the JApplet. the paintComponent() method
// adds a background image to the JPanel.
class AppletJPanel extends JPanel
{
    BufferedImage bufImage;
    
    public AppletJPanel()
    {
        setLayout( new GridLayout( 3, 3 ) );
        
        // this is my image, replace it with yours
        URL img = SimpleApplet.class.
                getResource( "airbornetoxiceventcover.jpg" );
        
        try
        {
            bufImage = ImageIO.read( img );
        }
        catch ( IOException ex )
        {
            System.out.println( "Error loading image." );
            ex.printStackTrace();
        }

    } // end constructor AppletJPanel()

    @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );
        g.drawImage( bufImage, 0, 0, null );
    }
    
} // end class AppletJPanel

This post has been edited by GregBrannon: 09 February 2013 - 06:19 AM

Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Can't draw on JPanel

Posted 09 February 2013 - 08:51 PM

http://www.dreaminco...0&#entry1800023
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1