บทที่ 13 การใช้ งาน Image และ JPanel       การใช้งานคลาส ImageIcon และคลาส      JPanel แบบ Compositionการทดลองที่ 13-1// F...
guiPanel.setBorder( BorderFactory.createBevelBorder(0));            btn1 = new JButton("Logo");            btn1.addActionL...
else if (e.getSource() == btn2) imageNo = 2;            else if (e.getSource() == btn3) imageNo = 3;            else if (e...
การใช้งานคลาส ImageIcon การภาพอนิ เมชัน และคลาส                                             ่            JPanel แบบ Inheri...
Graphics g = getGraphics();            g.setColor( color); //Color.WHITE);            g.fillRect( 1, 1, width, height);   ...
playBtn.setEnabled(true);              }      }}ผลลัพธ์          การใช้งาน Timer และ JPanel สําหรับสร้างคลาส Clockการทดลอง...
private     int yC = 150;      private     Color red, blue, green;      private     int Hour, Minute, Second;      private...
x = (int)(80 * Math.cos( toRadians(angle3) ));            y = (int)(80 * Math.sin( toRadians(angle3) ));            if (an...
angle1 = angle1 - 6;                  Second = Second + 1;            }            repaint();      }}// File Name : Lab13_...
ผลลัพธ์662305 Information Technology Laboratory II- Chapter 13   หน ้า 10 จาก 10
Upcoming SlideShare
Loading in …5
×

662305 LAB13

515 views
448 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
515
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

662305 LAB13

  1. 1. บทที่ 13 การใช้ งาน Image และ JPanel การใช้งานคลาส ImageIcon และคลาส JPanel แบบ Compositionการทดลองที่ 13-1// File Name : Picture.javaimport 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.javaimport 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));662305 Information Technology Laboratory II- Chapter 13 หน ้า 1 จาก 10
  2. 2. 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;662305 Information Technology Laboratory II- Chapter 13 หน ้า 2 จาก 10
  3. 3. 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(); }}ผลลัพธ์662305 Information Technology Laboratory II- Chapter 13 หน ้า 3 จาก 10
  4. 4. การใช้งานคลาส ImageIcon การภาพอนิ เมชัน และคลาส ่ JPanel แบบ Inheritance กับคลาส Timerการทดลองที่ 13-2// File Name : Animation.javaimport 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("imagesdeitel"+n+".gif"); } } public void play() { time.start(); } public void stop() { time.stop(); } public void actionPerformed(ActionEvent e) { nextImage();662305 Information Technology Laboratory II- Chapter 13 หน ้า 4 จาก 10
  5. 5. 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.javaimport 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();662305 Information Technology Laboratory II- Chapter 13 หน ้า 5 จาก 10
  6. 6. playBtn.setEnabled(true); } }}ผลลัพธ์ การใช้งาน Timer และ JPanel สําหรับสร้างคลาส Clockการทดลองที่ 13-03// File Name : Clock.javaimport 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;662305 Information Technology Laboratory II- Chapter 13 หน ้า 6 จาก 10
  7. 7. 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 );662305 Information Technology Laboratory II- Chapter 13 หน ้า 7 จาก 10
  8. 8. 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 {662305 Information Technology Laboratory II- Chapter 13 หน ้า 8 จาก 10
  9. 9. angle1 = angle1 - 6; Second = Second + 1; } repaint(); }}// File Name : Lab13_03.javaimport 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); } }}662305 Information Technology Laboratory II- Chapter 13 หน ้า 9 จาก 10
  10. 10. ผลลัพธ์662305 Information Technology Laboratory II- Chapter 13 หน ้า 10 จาก 10

×