Swing

add image picture in our GUI,how to use 2D API
Previous Home Next

 

We can make the Programming more interactive with Java 2D API. we can add figures, images, animation to our GUI and even pass visual information with the help of Java 2D API. We can easily use 2D within Swing components such as drop shadowssince Swing is built on 2D package.

 

 

package r4r;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class twoDAPItest extends JFrame{
	JLabel msgLab;
	  
	  public twoDAPItest(){
	  super("Swing");
	  setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	  addWindowListener(new WindowAdapter(){
	  public void windowClosing(WindowEvent e){
	  System.exit(0);
	  }
	  });
	  
	  ActionListener actl = new ActionListener(){
	  public void actionPerformed(ActionEvent ae){
	  msgLab.setText(((JButton)ae.getSource()).getText());
	  }
	  };
	  
	  JButton btn;
	  JPanel btnPanel = new JPanel();
	  btnPanel.setBorder(new TitledBorder("Click at button"));
	  for (int i = 0; i < 3; i++){
	  btn = new JButton("Button " + (i + 1));
	  btn.addActionListener(actl);
	  btnPanel.add(btn);
	  }
	  
	  JPanel panel2 = new JPanel(new BorderLayout());
	  panel2.setBorder(new EmptyBorder(8, 8, 8, 8));
	  msgLab = new JLabel("No button pressed!");
	  panel2.add(msgLab, BorderLayout.NORTH);
	  panel2.add(btnPanel, BorderLayout.CENTER);
	  setContentPane(panel2);
	  pack();
	  setVisible(true);
	  }
	  
	  public static void main(String[] args){
	  new twoDAPItest();
	  }
}

Previous Home Next