Headlines(LAB9-2)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Headlines extends JFrame{
HeadlInePanel news=new HeadlInePanel();
public Headlines(){
super("Headlines");
setSize(800,100);
JPanel pane=new JPanel();
pane.setLayout(new GridLayout(1,1,15,15));
pane.add(news);
setContentPane(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
news.scroll();
}
public static void main(String[]args){
Headlines head=new Headlines();
}
}
class HeadlInePanel extends JPanel{
String[]headlInes={
"นางนวลเจ้าเอย ทะเลกว้างใหญ่ เห็นเพียงแต่ขอบฟ้าไกล",
"ตัวเจ้าก็คงยังบินมุ่งไปอาจจะเหนื่อยแต่ความหวังยังมี",
"นางเอยเจ้าเอย เจ้าบินเรื่อยไป เหมือนดังว่าใจรู้ดี",
"จุดหมายในใจเจ้าคงต้องมี ห่างจากตรงนี้ไปอีกไกล"
};
int y=76;
void scroll(){
while(true){
y=y-1;
if(y<-75)
y=76;
repaint();
try{
Thread.sleep(200);
}catch(InterruptedException e){}
}
}
public void paintComponent(Graphics comp){
Graphics2D comp2D=(Graphics2D)comp;
Font type=new Font("monospaces",Font.BOLD,14);
comp2D.setFont(type);
comp2D.setColor(getBackground());
comp2D.fillRect(0,0,getSize().width,getSize().height);
comp2D.setColor(Color.black);
for(int i=0; i < headlInes.length;i++)
comp2D.drawString(headlInes[i],5,y+(20*i));
}
}
MediaPlayerDemo(LAB9-1)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerDemo extends JFrame{
private Player player;
private File file;
public MediaPlayerDemo ()
{
super("Demonstring the Java Media Player");
JButton openFile=new JButton("Open file to play");
openFile.addActionListener(
new ActionListener (){
public void actionPerformed(ActionEvent e)
{
openFile();
createPlayer();
}
}
);
getContentPane().add(openFile,BorderLayout.NORTH);
setSize(300,300);
show();
}
private void openFile()
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY);
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
file=null;
else
file=fileChooser.getSelectedFile();
}
private void createPlayer()
{
if(file==null)
return;
removePreviousPlayer();
try{
player=Manager.createPlayer(file.toURL());
player.addControllerListener(new EventHandler());
player.start();
}
catch(Exception e){
JOptionPane.showMessageDialog(this,
"Invalid file or location","Error loading file",
JOptionPane.ERROR_MESSAGE);
}
}
private void removePreviousPlayer()
{
if(player==null)
return;
player.close();
Component visual=player.getVisualComponent();
Component control=player.getControlPanelComponent();
Container c=getContentPane();
if(visual !=null)
c.remove(visual);
if(control !=null)
c.remove(control);
}
public static void main (String[]args){
MediaPlayerDemo app=new MediaPlayerDemo();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
private class EventHandler implements ControllerListener{
public void controllerUpdate(ControllerEvent e){
if (e instanceof RealizeCompleteEvent){
Container c=getContentPane();
Component visualComponent=
player.getVisualComponent();
if(visualComponent !=null)
c.add(visualComponent,BorderLayout.CENTER);
Component controlsComponent=
player.getControlPanelComponent();
if(controlsComponent !=null)
c.add(controlsComponent,BorderLayout.SOUTH);
c.doLayout();
}
}
}
}
PlayMidi(LAB8)
import java.awt.event.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.awt.GridLayout;
import java.io.File;
public class PlayMidi extends JFrame{
public PlayMidi(String title){
super(title);
setSize(180,100);
MidiPanel midi=new MidiPanel("./sounds/midi.midi");
JPanel pane=new JPanel();
pane.add(midi);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(pane);
setVisible(true);
}
public static void main(String[]args){
PlayMidi myMidi=new PlayMidi("Java Sound MIDI");
}
}
class MidiPanel extends JPanel implements Runnable{
Thread runner;
JProgressBar progress=new JProgressBar();
Sequence currentSound;
Sequencer player;
String songFile;
public MidiPanel(String song){
super();
songFile=song;
JLabel label=new JLabel("Playing file....");
setLayout(new GridLayout(2,1));
add(label);
add(progress);
if(runner==null){
runner=new Thread(this);
runner.start();
}
}
public void run(){
try{
File file=new File(songFile);
currentSound=MidiSystem.getSequence(file);
player=MidiSystem.getSequencer();
player.open();
player.setSequence(currentSound);
progress.setMinimum(0);
progress.setMaximum((int)player.getMicrosecondLength());
player.start();
while(player.isRunning()){
progress.setValue((int)player.getMicrosecondPosition());
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
progress.setValue((int)player.getMicrosecondPosition());
player.close();
}catch(Exception e){
System.err.println(e);
}
}
}
UseGridBag2(LAB7)
import javax.swing.*;
import java.awt.*;
class UseGridBag2 extends JFrame{
public UseGridBag2(String title){
super(title);
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gc=new GridBagConstraints();
Container c=getContentPane();
c.setLayout(gb);
gc.gridx=0; gc.gridy=0;
c.add(new JButton("Button 1"),gc);
gc.gridx=1;gc.gridy=1;
c.add(new JButton("Button2"),gc);
gc.gridx=2; gc.gridy=2;
c.add(new JButton("Button3"),gc);
}
public static void main(String[]args){
UseGridBag2 f=new UseGridBag2("Use GridBag2");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.pack();
f.show();
}
}
LayeredPaneTest(LAB7)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class LayeredPaneTest extends JFrame{
Container c=getContentPane();
JLayeredPane layeredPane=new JLayeredPane();
LayeredPaneTest(){
JButton b1=new JButton("1");
JButton b2=new JButton("2");
b1.setBackground(Color.blue);
b2.setBackground(Color.red);
b1.setBounds(60,30,100,60);
b2.setBounds(90,60,100,60);
layeredPane.add(b1,new Integer(2));
layeredPane.add(b2,new Integer(2));
layeredPane.setLayer(b2,100);
setLayeredPane(layeredPane);
setTitle("LayeredPaneTest");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[]args){
new LayeredPaneTest();
}
}
UseBound1(LAB7)
import javax.swing.*;
import java.awt.*;
class UseBound1{
public static void DisplayBound(Component comp){
if(comp !=null){
Rectangle r=comp.getBounds();
System.out.println(comp.getClass().getName());
System.out.println("x coordinate:"+r.x);
System.out.println("y coordinate:"+r.y);
System.out.println("height:"+r.height);
System.out.println("width:"+r.width);
}
else return;
}
public static void main(String[]args){
JTextField text=new JTextField(10);
JButton b1=new JButton("OK");
JFrame f=new JFrame();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setSize(300,100);
Container c=f.getContentPane();
c.setLayout(new FlowLayout());
c.add(text);
c.add(b1);
f.show();
DisplayBound(b1);
}
}