Advertisement
Guest User

Untitled

a guest
Jul 6th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public class JavaApplication30 {
  2.  
  3. /**
  4. * @param args the command line arguments
  5. */
  6. public static void main(String[] args) {
  7. // TODO code application logic here
  8.  
  9. JFrame f = new JFrame();
  10. //f.setSize(800,600);
  11.  
  12. String imagePath = "http://duke.kenai.com/misc/Bullfight.jpg";
  13. Image image = null;
  14. try
  15. {
  16. URL url = new URL(imagePath);
  17. image = ImageIO.read(url);
  18. }
  19. catch (IOException e)
  20. {
  21. System.out.println(e);
  22. }
  23.  
  24.  
  25. JPanel backgroundPanel=new ImagePanel(image);
  26. backgroundPanel.setLayout(null);
  27. backgroundPanel.setSize(638, 311);
  28. JPanel drawingPanel=new DrawingPanel();
  29. drawingPanel.setSize(638, 311);
  30. drawingPanel.setOpaque(false); //make it transparent
  31. backgroundPanel.add(drawingPanel);
  32. f.add(backgroundPanel);
  33. f.pack();
  34. f.setVisible(true);
  35.  
  36.  
  37. }
  38.  
  39.  
  40. static class ImagePanel extends JPanel {
  41.  
  42. private Image img;
  43.  
  44. public ImagePanel(Image img) {
  45. this.img = img;
  46. Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
  47. setPreferredSize(size);
  48. setMinimumSize(size);
  49. setMaximumSize(size);
  50. setSize(size);
  51. setLayout(null);
  52. }
  53.  
  54. public void paintComponent(Graphics g) {
  55. g.drawImage(img, 0, 0, null);
  56. }
  57. }
  58.  
  59.  
  60. static class DrawingPanel extends JPanel {
  61. public void paintComponent(Graphics g) {
  62. super.paintComponent(g);
  63. g.setColor(Color.GREEN);
  64. g.fillRect(0, 0 , 100, 100);
  65. }
  66. }
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement