Hi All,

I need to write a class to draw and add some components on frame like images,rectangles.
I have a class which is already written by some other person, and now i need to add 14 adjacent rectangles on the existing frame.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Graphics;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import com.ordyn.hbap.HbapBrowserWindow;
import com.ordyn.hbap.HbapNode;
import com.ordyn.lct.madm.gui.madmplugin.MadmPlugin;
import com.ordyn.lct.madm.gui.madmplugin.MadmViewComponent;
import com.ordyn.lct.madm.gui.neview.StmChassis;
import com.ordyn.lct.madm.taskmanager.core.TaskManager_I;

/**
 * @author unknown
 *
 */

public class EzhilChassis
extends StmChassis
{
	private ImageIcon bgIcon = new ImageIcon(this.getClass().getResource("/images/ezh/chassis_bg.jpg"));
	private ImageIcon fanIcon = new ImageIcon(this.getClass().getResource("/images/ezh/fan_bg.png"));
	private ImageIcon powerIcon = new ImageIcon(this.getClass().getResource("/images/ezh/power_bg.jpg"));

	private ImageIcon redIcon = new ImageIcon(this.getClass().getResource("/images/ezh/led_red.gif"));
	private ImageIcon greenIcon = new ImageIcon(this.getClass().getResource("/images/ezh/led_green.png"));
	
	
	private ImagePanel bgPanel = new ImagePanel(bgIcon);
	
	//cons 1
	public EzhilChassis() {
		super();
		initGUI();
	}

	// cons 2
	public EzhilChassis(HbapBrowserWindow browser, MadmViewComponent mainTPane,
			TaskManager_I tm, MadmPlugin madmPlugin) {
		super(browser, mainTPane, tm, madmPlugin);
		initGUI();
	}
	
	private void initGUI(){
		bgPanel.setBorder(BorderFactory.createRaisedBevelBorder());
		bgPanel.setPreferredSize(new Dimension(bgIcon.getIconWidth(),bgIcon.getIconHeight()));
		bgPanel.setLayout(new BorderLayout());
		
		JPanel topPanel = new JPanel(new BorderLayout());
		topPanel.setPreferredSize(new Dimension(100,70));
		topPanel.setOpaque(false);
		bgPanel.add(topPanel,BorderLayout.PAGE_START);
		
		ImagePanel powerIPanel1 = new ImagePanel(powerIcon);
		powerIPanel1.setPreferredSize(new Dimension(70,70));
powerIPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
		powerIPanel1.setLayout(new BorderLayout());
		powerIPanel1.add(getTopPanelLabel("PWR Filter 1"),BorderLayout.PAGE_END);
		powerIPanel1.add(getLEDLabel(redIcon),BorderLayout.PAGE_START);
		topPanel.add(powerIPanel1,BorderLayout.LINE_START);

		ImagePanel powerIPanel2 = new ImagePanel(powerIcon);
		powerIPanel2.setPreferredSize(new Dimension(70,70));
		powerIPanel2.setBorder(BorderFactory.createLoweredBevelBorder());
		powerIPanel2.setLayout(new BorderLayout());
		powerIPanel2.add(getTopPanelLabel("PWR Filter 2"),BorderLayout.PAGE_END);
		powerIPanel2.add(getLEDLabel(greenIcon),BorderLayout.PAGE_START);
		topPanel.add(powerIPanel2,BorderLayout.LINE_END);
					
		JPanel topCentrePanel = new JPanel(new GridLayout(2,1));
		topCentrePanel.setOpaque(false);
		topCentrePanel.setBorder(BorderFactory.createLoweredBevelBorder());
		topPanel.add(topCentrePanel);
		
		ImagePanel fanPanel1 = new ImagePanel(fanIcon);
		fanPanel1.setBorder(BorderFactory.createEtchedBorder());
		fanPanel1.setLayout(new BorderLayout());
		fanPanel1.add(getTopPanelLabel("Fan Tray 1"),BorderLayout.CENTER);
		topCentrePanel.add(fanPanel1);
		ImagePanel fanPanel2 = new ImagePanel(fanIcon);
		fanPanel2.setBorder(BorderFactory.createEtchedBorder());
		fanPanel2.setLayout(new BorderLayout());
		fanPanel2.add(getTopPanelLabel("Fan Tray 2"),BorderLayout.CENTER);
		topCentrePanel.add(fanPanel2);
		
		JPanel slotPanel = new JPanel();
		slotPanel.setOpaque(false);
		bgPanel.add(slotPanel);
	}
	   
	private JLabel getTopPanelLabel(String text){
		JLabel label = new JLabel(text);
		label.setOpaque(false);
		label.setForeground(Color.WHITE);
		label.setHorizontalAlignment(JLabel.CENTER);
		return label;
	}

	private JLabel getLEDLabel(ImageIcon icon){
		JLabel label = new JLabel(icon);
		label.setOpaque(false);
		label.setHorizontalAlignment(JLabel.CENTER);
		return label;
	}

	
	public Component getNodeViewProcessor(HbapNode arg0){
		JPanel mainPanel = new JPanel();
		mainPanel.add(bgPanel);
		bgPanel.setBorder(BorderFactory.createEmptyBorder(10, 27, 45, 28 ));
		return mainPanel;
	}
	
  
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setTitle("Chassis");  // added
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.getContentPane().add(new EzhilChassis().getNodeViewProcessor(null));
		frame.setSize(new Dimension(550,550));
		frame.setVisible(true);
		JFrame window = new JFrame();
	}
}

The above code is working fine... displaying a frame with good images as expected.

The following is the code which i wrote. Now i have to merge these code to the above original code.

i tried out bt not working. Please help me.

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawRectPanel extends JPanel {
	int x1=20;	int x2=20;	int y1=25;	int y2=250;   

  public void paint(Graphics g) {
    //super.paint(g);
    g.setColor(Color.blue);
      for (int i=1;i<=14;i++){
		  g.drawRect (x1,x2,y1,y2);
		  x1=x1+30;
	  }
    }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(550, 550);
    frame.addWindowListener(new WindowAdapter() {
    	 public void windowClosing(WindowEvent e) {
    	        System.exit(0);
    	      }
    });
    
    Container contentPane = frame.getContentPane();
    contentPane.add(new DrawRectPanel());
    frame.setVisible(true);
  }
}

Thanks in advance.

Regards,
RaniThomas.

i tried out bt not working

Can you post what is NOT WORKING and show the errors or problems with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.