多媒体包括绘图

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

paint()
• 要在容器上繪圖,在您的程式裡頭必須實 作一個 paint() ,並且提供一個 Graphics 類 別的物件供繪圖之用,
repaint(), update()
• public void repaint() 本元件重新繪圖。
• public void repaint(int x, int y, int width, int height) 本元件重新繪圖。(x,y) 左上角座標,width 寬,height 高。
• public void update(Graphics g) 以 g 物件更新本元件。
repain事件處理模式範例
這個程式是有一個Button,Button上面寫著請按我,下面有一行字”Button not yet clicked”,當我們按下去之後,下面的字串就會改變成”Button clicked”:
• public abstract void drawLine(int x1, int y1, int x2, int y2) 以起點座標 (x1,y1) 至終點 (x2,y2) 畫直線。
• public void drawRect(int x, int y, int width, int height) 畫一個長方形。左上角座標為 (x,y),寬為 width,高為 height。
color = Color.lightGray; c.setBackground(color); c.repaint(); } public static void main( String args[] ) { MyColor2 app = new MyColor2(); } }
【執行結果】
繪線及方圓
• public void fillRect(int x, int y, int width, int height) 畫並填滿一個長方形。左上角座標為 (x,y),寬為 width, 高為 height。
• public abstract void drawOval(int x, int y, int width, int height) 畫一個橢圓。中心座標為 (x,y),長徑(寬)為 width,短徑 (高)為 height。若長短徑值相同即為圓。
g.setColor(Color.RED);
g.drawString("我喜歡Java!!", 100,100);
}
public static void main(String[] args)
{ MyWindow3 frm = new MyWindow3();
frm.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.blue);
}
public void paint(Graphics g)
{ g.setFont(new Font("Serif", Font.BOLD, 32));
private String str;
public void init()
{ str = new String("Button not yet clicked.");
btn = new tton("按下我");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent ae)
設定顏色
• public final static Color black = new Color(0,0,0); • public final static Color blue = new Color(0,0,255); • public final static Color cyan = new Color(0,255,255); • public final static Color darkGray = new Color(64,64,64); • public final static Color gray = new Color(128,128,128); • public final static Color green = new Color(0,255,0); • public final static Color lightGray = new Color(192,192,192); • public final static Color magenta = new Color(255,0,255); • public final static Color orange = new Color(255,200,0); • public final static Color pink = new Color(255,175,175); • public final static Color red = new Color(255,0,0); • public final static Color white = new Color(255,255,255); • public final static Color yellow = new Color(255,255,0);
• public Color(int rgb) 建立一個新的 rgb 所構成的 Color 物件。 rgb 第 0-7 位元表藍色, 第 8-15 位元表綠色, 第 16-23 位 元表紅色。
• public Color(int r, int g, int b) 建立一個新的 r 紅 g 綠 b 藍所構成的 Color 物件。 r、g、b 介於 0 (含) 與 255 (含) 之間。
第 8-15 位 元表綠色,第 16-23 位元表紅色。
下列兩個抽象方法 getColor() 及 setColor() 屬於 Graphics 類別,它的衍生類別必須實作這兩個方法。 • public abstract void setColor(Color c) 設定顏色為 c。 • public abstract Color getColor() 傳回 Color 物件。
} public void paint( Graphics g ) { Color color = Color.black;
g.setColor(color); g.fillRect(25, 45, 100, 20); g.drawString("RGB值:" + color.getRed() + "(紅), " + color.getGreen() + "(綠)," + color.getBlue()+"(藍), " , 130,60); } public static void main( String args[] ) { MyColor myColor = new MyColor(); } }
多媒體
多媒體包括繪圖 (graphics) 、影像 (image)聲 音 (sound) 、,以及視聽 (video) 等。
Graphics類別
• Graphics 為抽象類別,是所有繪圖圖形或 字型的 基礎類別,它允許您在元件上製作 圖形或字型
Graphics方法
• 當 applet 程式 啟動時執行完 init()、start() 之後,會自動執行 paint() ,後來若要更改 畫面必須產生事件,例如改變 applet 視窗 大小等。若要強迫執行 paint() ,您可執行 repaint() ,它會先執行 update() 清除畫面 之後再呼 叫 paint() 。
在 Color 類別中有下列方法可取得 顏色的成分
• public int getBlue() 傳回藍色成分,介於 0-255 間。 • public int getGreen() 傳回綠色成分,介於 0-255 間。 • public int getRed() 傳回紅色成分,介於 0-255 間。 • public int getRGB() 傳回紅綠藍色成分,第 0-7 位元表藍色,
//TestButton.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class TestButtom extends Applet implements ActionListener
{ private Button btn;
{ str = "Button clicked.";
repaint();
}
public void paint(Graphics g)
{ g.drawString(str,50,50);
}
}
可以使用下列三個建構子,自己建立一個顏 色物件
• public Color(float r, float g, float b) 建立一個新的 r 紅 g 綠 b 藍所構成的 Color 物件。 r、g、b 介於 0.0 (含) 與 1.0 (含) 之間。
}
}
【執行結果】
MyColor.java
import java.awt.*; import javax.swing.*; public class MyColor extends JFrame { public MyColor()
{ super("MyColor: Using colors"); setSize(300, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);
【程式MyWindow3.java】
import java.awt.*;
import javax.swing.*;
public class MyWindow3 extends JFrame
{ public MyWindow3()
// Constructor
{ super("底色為藍色的視窗");
setBounds(50, 100, 400, 150);
private Color color = Color.lightGray; private Container c; public MyColor2() { super("MyColor2: 使用JColorChooser元件");
c = getContentPane(); c.setLayout(new FlowLayout()); changeColor = new JButton("改變背景顏色"); changeColor.addActionListener(this); c.add( changeColor ); setSize(300, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { color = JColorChooser.showDialog(this, "選色", color); if (color == null)
• public void repaint(long tm) 本元件重新繪圖,在 tm 毫秒之內。
• public void repaint(long tm, int x, int y, int width, int height) 本元件重新繪圖。(x,y) 左上角座標,width 寬,height 高, 在 tm 毫秒之內。
• MyColor2.java 使用 JColorChooser 元件讓 您挑選一 個顏色,並將該顏色設為版面的 底色。您在 "改變背景顏色" 的 按鈕上按一 下滑鼠左鍵,產生一個按鈕事件,接著就 執行 actionPerformed() 方法。
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyColor2 extends JFrame implements ActionListener { private JButton changeColor;
相关文档
最新文档