6 Replies - 253 Views - Last Post: 15 November 2012 - 09:37 AM Rate Topic: -----

#1 status90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 24-September 09

images output

Posted 15 November 2012 - 07:57 AM

Is it possible to make image "window.png" that has transparent background to appear within an image "greenhouse.jpg"?

what i did atm, it only appear as individual image(s)

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class AutoCad extends JFrame {
	
	JLabel test = new JLabel();
	JLabel overlay = new JLabel();
	
	public AutoCad() {
		setTitle("AutoCad for Mentally Challenged");
		setSize(575, 350);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		JRadioButton greenCmd = new JRadioButton("Green");
		JRadioButton blueCmd = new JRadioButton("Blue");
		JRadioButton blackCmd = new JRadioButton("Black");
		
		JCheckBox windowCmd = new JCheckBox("Window");
		JCheckBox doorCmd = new JCheckBox("Door");
		JCheckBox chimneyCmd = new JCheckBox("Chimney");
		JCheckBox mailboxCmd = new JCheckBox("Mailbox");
		
		JPanel housePanel = new JPanel();
		JPanel colorPanel = new JPanel();
		JPanel partPanel = new JPanel();
		
		Container pane = getContentPane();
		pane.setLayout(new FlowLayout());
		
		ButtonGroup colorGroup = new ButtonGroup();
		pane.add(colorPanel, BorderLayout.CENTER);
		colorPanel.setLayout(new GridLayout(3,0));
		colorPanel.setBorder(BorderFactory.createTitledBorder("   Color   "));
		colorGroup.add(greenCmd);
		colorPanel.add(greenCmd);
		colorGroup.add(blueCmd);
		colorPanel.add(blueCmd);
		colorGroup.add(blackCmd);
		colorPanel.add(blackCmd);
		
		pane.add(partPanel,BorderLayout.WEST);
		partPanel.setLayout(new GridLayout(4,0));
		partPanel.setBorder(BorderFactory.createTitledBorder("   Parts   "));
		partPanel.add(windowCmd);
		partPanel.add(doorCmd);
		partPanel.add(chimneyCmd);
		partPanel.add(mailboxCmd);
		
		pane.add(housePanel, BorderLayout.EAST);
		test.setIcon(new ImageIcon(getClass().getResource("image/greenhouse.jpg")));
		housePanel.add(test);
		
		greenCmd.setSelected(true);
		greenCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                test.setIcon(new ImageIcon(getClass().getResource("image/greenhouse.jpg")));
            }
        });
		
		blueCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                test.setIcon(new ImageIcon(getClass().getResource("image/bluehouse.jpg")));
            }
        });
		blackCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
                test.setIcon(new ImageIcon(getClass().getResource("image/blackhouse.jpg")));
            }
        });
		
		windowCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
//                overlay.setIcon(new ImageIcon(getClass().getResource("image/window.png")));
            }
        });
		doorCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
//            	overlay.setIcon(new ImageIcon(getClass().getResource("image/door.png")));
            }
        });
		chimneyCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
//            	overlay.setIcon(new ImageIcon(getClass().getResource("image/chimney.png")));
            }
        });
		mailboxCmd.addActionListener(new ActionListener()
        {
            public void actionPerformed (ActionEvent e)
            {
//            	overlay.setIcon(new ImageIcon(getClass().getResource("image/mail.png")));
            }
        });
		
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new AutoCad();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: images output

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: images output

Posted 15 November 2012 - 08:02 AM

It is possible and quite easy, but I have no idea what you're doing. Care to explain?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: images output

Posted 15 November 2012 - 08:15 AM

There's possibly a better way of doing it, but try overriding ImageIcon.paintIcon
Was This Post Helpful? 0
  • +
  • -

#4 status90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 24-September 09

Re: images output

Posted 15 November 2012 - 08:20 AM

i want 4 JCheckBox which each checkboxes have a .png images that would appear within or on top of .jpg image if ticked

say the image greenhouse.jpg is a house that has no window when ticked checkbox window, window.png will appear on top of that image making that house look like it has a window..well something like that
Was This Post Helpful? 0
  • +
  • -

#5 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: images output

Posted 15 November 2012 - 08:33 AM

Despite g00se's solution there are 3 other ones

- using JLayeredPane and playing with the opacity of its layers if it is possible (never tried that one)

- playing with the opacity of the JPanel before calling paint() as I did here
http://www.dreaminco...snippet5599.htm

- using JFrame glasspane
http://docs.oracle.c...s/rootpane.html
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: images output

Posted 15 November 2012 - 08:42 AM

Actually my suggestion has grown on me a bit ;) The following should give you a clue as to a possible implementation:

public class OverlayingIcon<List<Image>> extends ImageIcon {}

Was This Post Helpful? 1
  • +
  • -

#7 status90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 24-September 09

Re: images output

Posted 15 November 2012 - 09:37 AM

View Postg00se, on 15 November 2012 - 08:15 AM, said:

There's possibly a better way of doing it, but try overriding ImageIcon.paintIcon


how to do that? do you have an example that uses checkbox or anything similar for that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1