662305 lab13

10
662305 Information Technology Laboratory II- Chapter 13 หน ้า 1 จาก 10 บททีบทที13 การ การ ใช้งาน ใช้งาน Image และ และ JPanel การใช้งานคลาส ImageIcon และคลาส JPanel แบบ Composition การทดลองที13-1 // File Name : Picture.java import java.awt.*; import javax.swing.*; public class Picture { private ImageIcon image; private int x, y; public Picture(String fname) { image = new ImageIcon(fname); } public void draw( JPanel jPanel ) { image.paintIcon( jPanel , jPanel.getGraphics(), x, y ); } public void position(int x, int y) { this.x = x; this.y = y; } public void clear(JPanel jPanel) { jPanel.paint(jPanel.getGraphics()); } } // File Name : Lab13_01.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab13_01 extends JApplet implements ActionListener { private Picture image1, image2, image3; private JPanel guiPanel, graphicsPanel; private JButton btn1, btn2, btn3, btn4, btn5; private boolean isShow = false; private int imageNo; public void init() { setSize(500,350); Container c = getContentPane(); c.setLayout(new FlowLayout()); guiPanel = new JPanel(); guiPanel.setPreferredSize(new Dimension(400, 50));

Upload: nitigan-nakjuatong

Post on 15-May-2015

389 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 1 จาก 10

บทที ่บทที ่1133 การการใช้งาน ใช้งาน IImmaaggee และ และ JJPPaanneell

การใชง้านคลาส ImageIcon และคลาส JPanel แบบ Composition การทดลองที ่13-1 // File Name : Picture.java import java.awt.*; import javax.swing.*; public class Picture { private ImageIcon image; private int x, y; public Picture(String fname) { image = new ImageIcon(fname); } public void draw( JPanel jPanel ) { image.paintIcon( jPanel , jPanel.getGraphics(), x, y ); } public void position(int x, int y) { this.x = x; this.y = y; } public void clear(JPanel jPanel) { jPanel.paint(jPanel.getGraphics()); } } // File Name : Lab13_01.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab13_01 extends JApplet implements ActionListener { private Picture image1, image2, image3; private JPanel guiPanel, graphicsPanel; private JButton btn1, btn2, btn3, btn4, btn5; private boolean isShow = false; private int imageNo; public void init() { setSize(500,350); Container c = getContentPane(); c.setLayout(new FlowLayout()); guiPanel = new JPanel(); guiPanel.setPreferredSize(new Dimension(400, 50));

Page 2: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 2 จาก 10

guiPanel.setBorder( BorderFactory.createBevelBorder(0)); btn1 = new JButton("Logo"); btn1.addActionListener(this); guiPanel.add(btn1); btn2 = new JButton("Car"); btn2.addActionListener(this); guiPanel.add(btn2); btn3 = new JButton("Beach"); btn3.addActionListener(this); guiPanel.add(btn3); btn4 = new JButton("Show"); btn4.addActionListener(this); guiPanel.add(btn4); btn5 = new JButton("Clear"); btn5.addActionListener(this); guiPanel.add(btn5); graphicsPanel = new JPanel(); graphicsPanel.setPreferredSize(new Dimension(320, 240)); graphicsPanel.setBorder( BorderFactory.createBevelBorder(0)); c.add(guiPanel); c.add(graphicsPanel); loadImage(); } public void loadImage() { image1 = new Picture( "logo.gif" ); image2 = new Picture( "car.gif" ); image3 = new Picture( "beach.jpg" ); } public void paint( Graphics g ) { super.paint( g ); if (isShow) { switch( imageNo ) { case 1: image1.draw( graphicsPanel ); break; case 2: image2.draw( graphicsPanel ); break; case 3: image3.draw( graphicsPanel ); break; } } } public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) imageNo = 1;

Page 3: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 3 จาก 10

else if (e.getSource() == btn2) imageNo = 2; else if (e.getSource() == btn3) imageNo = 3; else if (e.getSource() == btn4) { if (isShow == true) btn4.setText("Not show"); else btn4.setText("Show"); isShow = !isShow; } else { imageNo = 0; image1.clear( graphicsPanel ); } repaint(); } }

ผลลพัธ์

Page 4: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 4 จาก 10

การใชง้านคลาส ImageIcon การภาพอนิเมชัน่ และคลาส JPanel แบบ Inheritance กบัคลาส Timer การทดลองที ่13-2 // File Name : Animation.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; public class Animation extends JPanel implements ActionListener { private int width = 320; private int height = 240; private Timer time; private ImageIcon image[] = new ImageIcon[30]; private int imageNo = 0; private Color color; public Animation() { super(); setPreferredSize( new Dimension(width, height)); setBorder( BorderFactory.createBevelBorder(0)); time = new Timer(100, this); loadImage(); color = getBackground(); } public Animation(int w, int h) { super(); width = w; height = h; setPreferredSize( new Dimension(width, height)); setBorder( BorderFactory.createBevelBorder(0)); time = new Timer(100, this); loadImage(); color = getBackground(); } private void loadImage() { for(int n = 0; n < image.length ; n++) { image[n] = new ImageIcon("images\\deitel"+n+".gif"); } } public void play() { time.start(); } public void stop() { time.stop(); } public void actionPerformed(ActionEvent e) { nextImage();

Page 5: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 5 จาก 10

Graphics g = getGraphics(); g.setColor( color); //Color.WHITE); g.fillRect( 1, 1, width, height); image[imageNo].paintIcon( this, getGraphics(), 1, 1); } public void nextImage() { imageNo++; if (imageNo == 30) imageNo = 0; } protected void paintComponent(Graphics g) { super.paintComponent(g); } } // File Name : Lab13_02.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab13_02 extends JApplet implements ActionListener { private Animation display; private JButton playBtn, stopBtn; public void init() { setSize(400,350); Container c = getContentPane(); c.setLayout(new FlowLayout()); playBtn = new JButton("Play"); playBtn.addActionListener(this); stopBtn = new JButton("Stop"); stopBtn.addActionListener(this); display = new Animation(); c.add(playBtn); c.add(stopBtn); c.add(display); } public void paint(Graphics g) { super.paint(g); } public void stop( ) { display.stop(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == playBtn) { display.play(); playBtn.setEnabled(false); } else if (e.getSource() == stopBtn) { display.stop();

Page 6: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 6 จาก 10

playBtn.setEnabled(true); } } } ผลลพัธ์

การใชง้าน Timer และ JPanel สาํหรับสร้างคลาส Clock

การทดลองที ่13-03 // File Name : Clock.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; public class Clock extends JPanel implements ActionListener { private int width = 300; private int height = 300; private int xC = 150;

Page 7: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 7 จาก 10

private int yC = 150; private Color red, blue, green; private int Hour, Minute, Second; private double angle1 = -270, angle2 = -180, angle3 = -270; private Timer time; public Clock() { super(); setPreferredSize( new Dimension(width, height)); setBorder( BorderFactory.createBevelBorder(0)); red = Color.RED; blue = Color.BLUE; green = Color.GREEN; setFont(new Font("Tahoma",Font.BOLD,16)); Hour = 0; Minute = 45; Second = 0; time = new Timer(1000, this); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval( xC -145, yC -145, 290, 290); g.drawOval( xC -146, yC -146, 292, 292); drawText(g); g.setColor( red ); int x = (int)(125 * Math.cos( toRadians(angle1) )); int y = (int)(125 * Math.sin( toRadians(angle1) )); // Second if (angle1 <= 90) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle1 <= 180) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle1 <= 270) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle1 <= 360) g.drawLine( 150, 150, xC+x, yC-y ); // Minute g.setColor( green ); x = (int)(100 * Math.cos( toRadians(angle2) )); y = (int)(100 * Math.sin( toRadians(angle2) )); if (angle2 <= 90) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle2 <= 180) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle2 <= 270) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle2 <= 360) g.drawLine( 150, 150, xC+x, yC-y ); // Hour g.setColor( blue );

Page 8: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 8 จาก 10

x = (int)(80 * Math.cos( toRadians(angle3) )); y = (int)(80 * Math.sin( toRadians(angle3) )); if (angle3 <= 90) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle3 <= 180) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle3 <= 270) g.drawLine( 150, 150, xC+x, yC-y ); else if (angle3 <= 360) g.drawLine( 150, 150, xC+x, yC-y ); g.setColor( Color.BLACK ); g.fillOval(xC - 4, yC -4, 8, 8); } public void drawText(Graphics g) { g.drawString("12",140,22); g.drawString("6", 145,290); g.drawString("3", 280,157); g.drawString("9", 10,157); } private double toRadians(double angle) { return( Math.PI * angle / 180.0); } public void start() { time.start(); } public void stop() { time.stop(); } public void actionPerformed(ActionEvent e) { if (Second == 60) { angle1 = -270; Second = 0; Minute = Minute + 1; if (Minute == 60) { angle2 = -270; Minute = 0; Hour = Hour + 1; angle3 = angle3 - 30; if (Hour == 12) { angle3 = -270; Hour = 0; } } else { angle2 = angle2 - 6; } } else {

Page 9: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 9 จาก 10

angle1 = angle1 - 6; Second = Second + 1; } repaint(); } } // File Name : Lab13_03.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab13_03 extends JApplet implements ActionListener { private Clock time; private JButton playBtn, stopBtn; public void init() { setSize(400,400); Container c = getContentPane(); c.setLayout(new FlowLayout()); playBtn = new JButton("Stat Time"); playBtn.addActionListener(this); stopBtn = new JButton("Stop Time"); stopBtn.addActionListener(this); time = new Clock(); c.add(playBtn); c.add(stopBtn); c.add(time); } public void paint(Graphics g) { super.paint(g); } public void actionPerformed(ActionEvent e) { if (e.getSource() == playBtn) { time.start(); playBtn.setEnabled(false); } else if (e.getSource() == stopBtn) { time.stop(); playBtn.setEnabled(true); } } }

Page 10: 662305 LAB13

662305 Information Technology Laboratory II- Chapter 13 หนา้ 10 จาก 10

ผลลพัธ์