4 Replies - 362 Views - Last Post: 16 August 2013 - 01:58 AM Rate Topic: -----

#1 zerimar97  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-April 13

Make a slightly opaque rectangle

Posted 15 August 2013 - 07:45 PM

Hi, i'm making a hospital user interface. I want to have a list of buttons on the left side of the screen and behind it like a nice slightly see through rectangle to give it a professional look. It's kind of hard to describe. How to I do that in swing?

thanks in advance

package medicine;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.Toolkit;  

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Frame extends JFrame implements ActionListener{
	
    private JButton button;
    private JPanel panel;

    public Frame()
    {           	
        button = new JButton("Login");;
        panel = new JPanel(){  
                     public void paintComponent(Graphics g) {  
        	                    Image img = Toolkit.getDefaultToolkit().getImage(  
        	                    Frame.class.getResource("/photos/Hospital.jpg"));  
        	                    g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);  
        	                    
        	                }  
                   };  
                   
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));  
        panel.setLayout(new BorderLayout(0, 0));  
        setContentPane(panel); 
        
        button.setLocation(20,50);
        button.setSize(150, 50);
        button.setBackground(Color.WHITE);
        button.setVisible(true);
        
        Container contentPane = getContentPane();
        contentPane.add(button);
        button.addActionListener(this);
        
        setLayout(null);
        setSize(1050, 712);   
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setVisible(true);   
        setLocationRelativeTo(null);
        
    
    }
    
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == button)             
        {
            Color color = new Color(222, 12, 54);      
            getContentPane().setBackground(color); 
        }
    }
    
	
	public static void main(String[] args ) {
		Frame app = new Frame();
	}
}



p.s. Is it me or does it seem like everyone always forgets the simplest things in coding?

Is This A Good Question/Topic? 0
  • +

Replies To: Make a slightly opaque rectangle

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Make a slightly opaque rectangle

Posted 15 August 2013 - 07:51 PM

completly ridiculous to read from disk the Image at run time


     Image img = Toolkit.getDefaultToolkit().getImage(  
     Frame.class.getResource("/photos/Hospital.jpg"));  


read it only once at construction time

an paintComponent() should execute in less than 1/60 of a second... not really time to do disk access there :^:
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,607
  • Joined: 29-July 11

Re: Make a slightly opaque rectangle

Posted 15 August 2013 - 09:29 PM

It's ot hard to set the translucency of a Graphics2D object for which you could draw a rectangle, but why don't you post an image of hat you want.
Was This Post Helpful? 0
  • +
  • -

#4 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Make a slightly opaque rectangle

Posted 16 August 2013 - 01:41 AM

I guess you have to change alpha value. For example 0.5 makes half-transparent image. If I am wrong, correct me.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: Make a slightly opaque rectangle

Posted 16 August 2013 - 01:58 AM

When you say you want it to be transparent, what do you mean? The background of a Frame is grey (by default) so you can achieve the same effect by editing your image to make it a bit greyer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1