Results 1 to 4 of 4
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Red face Drawing an image on JPanel

    Hello!

    I have made applet using Swing, which has one JPanel and one DesktopPane with labels and sliders. I want to draw shapes and images on JPanel, but I don't know how to do it. I tried to draw image, but I lose all my panels and got just picture. I think that main problem is that I don't know how to access to my JPanel on which I want to draw and then to make drawing.

    I have just made a new panel and wrote code for connectiong with the applet. I also wrote code for drawing an image, but it doesn't work. Can anybody help?
    Here is the code:
    Java Code:
    package org.me.pack;
    
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import java.net.*;
    
    public class MajaPanel extends JPanel {
        private static BasicStroke bs1 = new BasicStroke(3.0f);
        BufferedImage bmp;
        BufferedImage bimg;
    
     public void init() {
          URL slika;
            try {
                slika = new URL("http://www.wolflodge.org/prophecy/endofdays_2/White_Rabbit.gif");
                try {
                    bmp = ImageIO.read(slika);
                } catch (IOException ex) {
                    Logger.getLogger(Simulacija.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (MalformedURLException ex) {
                Logger.getLogger(Simulacija.class.getName()).log(Level.SEVERE, null, ex);
            }
     }
    
    public void render(int w, int h, Graphics2D g2) {
            g2.drawImage(bmp, null, this);
            }
            public Graphics2D createGraphics2D(int w, int h) {
            Graphics2D g2 = null;
            if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
                bimg = (BufferedImage) createImage(w, h);
            }
            g2 = bimg.createGraphics();
            g2.setStroke(bs1);
             g2.setBackground(getBackground());
            g2.clearRect(0, 0, w, h);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            return g2;
    }
    
    public void paint(Graphics g) {
    
    	Dimension d1 = getSize();
            Graphics2D g2=(Graphics2D)g;
            render(d1.width, d1.height, g2);
            g.drawImage(bimg, 0, 0, this);
            g2.dispose();
        }
    }
    Last edited by mneskovic; 05-20-2010 at 03:14 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

  3. #3
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    I guess you should override paintComponent( Graphics g ) method and call super.paintComponent( g ); before calling anything else so you won't lose your panels.

  4. #4
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Problem solved! Thank you, guys for interesting :) I have new problem: I drow gif images and rectangle and I would like to make them not transparent, because rectangle edges go over them, but I don't know how to fix this. I read something about opaque, but where to set it?

    Here is the code:
    Java Code:
    package org.me.pack;
    import java.awt.Color;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    import java.awt.Image;
    import java.awt.Graphics;
    public class MajaPanel extends JPanel
    {
      public Image backgroundImage;
      public MajaPanel()
      {
                    setOpaque(true);
          try
          {
             this.backgroundImage = ImageIO.read(new URL("http://www.ajmalbeig.addr.com/pgc/images/icons/icon_computer.GIF"));
          }
          catch(Exception e)
          {
             throw new RuntimeException(e);
          }
      }
      public static void main(String[] args)
      {
         JFrame frame = new JFrame("Testiranje panela");
         frame.setContentPane(new MajaPanel());
         frame.getContentPane().setLayout(null);
         frame.setSize(565,214);
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         frame.setVisible(true);
      }
      protected void paintComponent(Graphics g)
      {
         
         g.drawImage(backgroundImage,30,70,null);
         g.drawImage(backgroundImage,580,70,null);
         g.setColor(Color.blue);
         g.fillRect(90, 100, 500, 14);
         g.fillRect(50, 50, 10, 10);
      }
    }

Similar Threads

  1. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 04:46 AM
  2. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 08:12 PM
  3. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Swing
    Replies: 9
    Last Post: 10-18-2009, 12:21 AM
  4. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 06:00 PM
  5. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 07:42 AM

Posting Permissions

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