Applet 7 image_j_panel
Upcoming SlideShare
Loading in...5
×
 

Applet 7 image_j_panel

on

  • 476 views

image_j_panel

image_j_panel

Statistics

Views

Total Views
476
Views on SlideShare
476
Embed Views
0

Actions

Likes
0
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Applet 7 image_j_panel Applet 7 image_j_panel Document Transcript

    • การใชงาน Image และ JPanel sscพื้นฐานการใชงาน ImageIconตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/image-jpanel/testImageIcon.html)testImageIcon.html <html> ! <body> <h1>Test ImageIcon</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testImageIcon.class" height="350" width="600"> ! ! </applet> ! </body> </html>testImageIcon.javaขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import javax.swing.*; public class testImageIcon extends JApplet { ! public void init () { ! } ! public void paint(Graphics g) { ! } }ขั้นตอนที่ 2 ประกาศตัวแปร ImageIcon private ImageIcon image1, image2, image3;ขั้นตอนที่ 3 สราง ImageIcon ขึ้นมา ! public void init() ! { ! ! image1 = new ImageIcon( getClass().getResource("/htb-8.png") ); ! ! image2 = new ImageIcon( getClass().getResource("/htb-16.png") ); ! ! image3 = new ImageIcon( getClass().getResource("/htb-35.png") ); ! }ขั้นตอนที่ 4 แสดงผล ImageIcon ออกมา ! public void paint( Graphics g ) { ! ! image1.paintIcon( this, g, 0, 0 ); ! ! image2.paintIcon( this, g, 160, 0 ); ! ! image3.paintIcon( this, g, 320, 0 ); Note :: ................................................................ ! ! g.setFont(new Font("Tahoma",Font.BOLD, 14)); ! ! g.setColor(Color.BLUE); ! ! g.drawString("Width = " + image1.getIconWidth() + " pixel", 10, 180); ! ! g.drawString("Height = " + image1.getIconHeight() + " pixel", 10, 200); Note :: ................................................................ ! ! g.drawString("Width = " + image2.getIconWidth() + " pixel", 170, 180); ! ! g.drawString("Height = " + image2.getIconHeight() + " pixel", 170, 200); ! ! g.drawString("Width = " + image3.getIconWidth() + " pixel", 330, 260); ! ! g.drawString("Height = " + image3.getIconHeight() + " pixel", 330, 280); ! }ขั้นตอนที่ 5 ทดลอง Compile และ Run ผล Page 1 of 5
    • การใชงาน ImageIcon และ JPanel แบบ Compositionตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/image-jpanel/testImageJPanel.html) JPanel คือ ................................................. ............................................................................. ............................................................................. ............................................................................. ............................................................................. .............................................................................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(getClass().getResource(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()); ! } }Class Picture มีไวเพื่อ .........................................................................................................................................................testImageJPanel.html <html> ! <body> <h1>Test Image and JPanel</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testImageJPanel.class" height="350" width="500"> ! ! </applet> ! </body> </html>testImageJPanel.javaขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import javax.swing.*; import java.awt.event.*; public class testImageJPanel extends JApplet implements ActionListener{ ! public void init() { ! } ! public void paint( Graphics g ) { ! } ! public void actionPerformed(ActionEvent e) { ! } }ขั้นตอนที่ 2 ประกาศคาตางๆ ! private Picture image1, image2, image3; ! private JPanel guiPanel, graphicsPanel; ! private JButton btn1, btn2, btn3, btnShow, btnClear; ! private boolean isShow = false; ! private int imageNo; Page 2 of 5
    • ขั้นตอนที่ 3 สรางคาเริ่มตน ! public void init() { ! ! Container c = getContentPane(); ! ! c.setLayout(new FlowLayout()); ! ! guiPanel = new JPanel(); ! ! guiPanel.setPreferredSize(new Dimension(500, 50)); ! ! guiPanel.setBorder( BorderFactory.createBevelBorder(0)); ! ! btn1 = new JButton("Flower1"); ! ! btn1.addActionListener(this); ! ! guiPanel.add(btn1); ! ! btn2 = new JButton("Flower2"); ! ! btn2.addActionListener(this); ! ! guiPanel.add(btn2); ! ! btn3 = new JButton("Flower3"); ! ! btn3.addActionListener(this); ! ! guiPanel.add(btn3); ! ! btnShow = new JButton("Show"); ! ! btnShow.addActionListener(this); ! ! guiPanel.add(btnShow); ! ! btnClear = new JButton("Clear"); ! ! btnClear.addActionListener(this); ! ! guiPanel.add(btnClear); ! ! graphicsPanel = new JPanel(); ! ! graphicsPanel.setPreferredSize(new Dimension(520, 260)); ! ! graphicsPanel.setBorder(BorderFactory.createBevelBorder(0)); ! ! c.add(guiPanel); ! ! c.add(graphicsPanel); ! ! loadImage(); ! } ! public void loadImage() ! { ! ! image1 = new Picture( "/htb-8.png" ); ! ! image2 = new Picture( "/htb-16.png" ); ! ! image3 = new Picture( "/htb-35.png" ); ! } ! 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; ! ! ! } ! ! } ! }ขั้นตอนที่ 4 สรางในสวนของการตรวจสอบการกระทำกับปุมตางๆ ! public void actionPerformed(ActionEvent e) { ! ! if (e.getSource() == btn1) imageNo = 1; ! ! else if (e.getSource() == btn2) imageNo = 2; ! ! else if (e.getSource() == btn3) imageNo = 3; ! ! else if (e.getSource() == btnShow) { ! ! ! if (isShow == true) { btnShow.setText("Not show"); } ! ! ! else { btnShow.setText("Show"); } ! ! ! isShow = !isShow; ! ! } ! ! else { ! ! ! imageNo = 0; ! ! ! image1.clear( graphicsPanel ); ! ! } ! ! repaint(); ! }ขั้นตอนที่ 5 ทดลอง Compile และ Run ผล Page 3 of 5
    • การใชงาน ImageIcon และ JPanel แบบ Inheritance กับ Timerตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/image-jpanel/testAnimation.html)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 = 400; ! private int height = 400; ! private Timer time; ! private ImageIcon image[] = new ImageIcon[5]; ! private int imageNo = 0; ! private Color color; ! public Animation() { ! ! super(); ! ! setPreferredSize( new Dimension(width, height)); ! ! setBorder( BorderFactory.createBevelBorder(0)); ! ! time = new Timer(500, 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(getClass().getResource("/images/htb-"+n+".png")); ! ! } ! } ! public void play() { ! ! time.start(); ! } ! public void stop() { ! ! time.stop(); ! } ! public void actionPerformed(ActionEvent e) { ! ! nextImage(); ! ! Graphics g = getGraphics(); ! ! g.setColor(color); ! ! g.fillRect( 1, 1, width, height); ! ! image[imageNo].paintIcon( this, getGraphics(), 1, 1); ! } ! public void nextImage() { ! ! imageNo++; ! ! if (imageNo == 5) { imageNo = 0; } ! } ! protected void paintComponent(Graphics g) { ! ! super.paintComponent(g); ! } }Method loadImage มีไวเพื่อ ................................................................................................................................................ที่ Method actionPerformed มีการทำงานอะไรบาง ............................................................................................................................................................................................................................................................................................................. Page 4 of 5
    • testAnimation.html <html> ! <body> <h1>Test Animation</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testAnimation.class" height="400" width="400"> ! ! </applet> ! </body> </html>testAnimation.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testAnimation extends JApplet implements ActionListener { ! private Animation display; ! private JButton playBtn, stopBtn; ! public void init() { ! ! Container c = getContentPane(); ! ! c.setLayout(new FlowLayout()); ! ! ! ! playBtn = new JButton("Play"); ! ! playBtn.addActionListener(this); ! ! c.add(playBtn); ! ! ! ! stopBtn = new JButton("Stop"); ! ! stopBtn.addActionListener(this); ! ! c.add(stopBtn); ! ! ! ! display = new Animation(); ! ! 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(); ! ! ! playBtn.setEnabled(true); ! ! } ! } } Page 5 of 5