Results 1 to 4 of 4
Thread: Drawing an image on JPanel
- 05-20-2010, 11:50 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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.
- 05-20-2010, 12:09 PM #2
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-20-2010, 01:47 PM #3
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.
- 05-20-2010, 01:58 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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
-
Drawing on Jpanel
By nonabhai in forum AWT / SwingReplies: 0Last Post: 03-13-2010, 04:46 AM -
Drawing with JPanel
By m00nchile in forum New To JavaReplies: 2Last Post: 02-18-2010, 08:12 PM -
Drawing image on JPanel from another frame
By jeryslo in forum AWT / SwingReplies: 9Last Post: 10-18-2009, 12:21 AM -
Looking for help on drawing stuff in a jPanel
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 08-28-2009, 06:00 PM -
drawing to a JPanel
By diggitydoggz in forum New To JavaReplies: 11Last Post: 03-09-2009, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks