java - 圖形程式設計

來源:互聯網
上載者:User

標籤:ovf   螢幕   bsh   pac   cas   pcr   exit   pga   class   

 

一、建立架構

架構(frame),沒有包含其他視窗的視窗

swing 基於 awt, 建立架構第一步,需要匯入swing 與 awt 的包

 

import java.awt.*;import javax.swing.*;     //swing 在java的擴充包中/** * 檔案名稱:SimpleFrameTest.java
* 功能:瞭解架構搭建部署
* 步驟:
* 1. 定義子類SimpleFrame,構造器中大小設定為300 * 200 象素
* 2. 在SimpleFrameTest中的main方法中建立一個SimpleFrame對象frame
* 3. 定義使用者關閉這個架構時的響應動作,預設是隱藏,不是關閉
* 4. 顯示架構,預設不顯示
*/public class SimpleFrameTest{ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { SimpleFrame frame = new SimpleFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}class SimpleFrame extends JFrame{ private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public SimpleFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); }}

 

 

 

二、架構大小
import java.awt.*;import javax.swing.*;/** *  檔案名稱: SizedFrameTest.java *  功能:增加架構屬性,添加標題
*
*
*/public class SizedFrameTest{ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new SizedFrame(); frame.setTitle("SizedFrame");    //設定架構標題名稱 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}class SizedFrame extends JFrame{ public SizedFrame() {
    //得到架構大小
    //步驟:
    // 1. 調用Toolkit類的靜態方法getDefaultTookit得到一個Tookit對象
    // 2. 這個方法以Dimension方式返回螢幕尺寸
    // 3. Dimension 同時用公有變數width 與 hight 儲存螢幕寬度與高度
Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; // 使用架構屬性中設定大小將螢幕設定為一半
    // 讓系統定位架構 setSize(screenWidth / 2, screenHeight / 2); setLocationByPlatform(true); // 設定架構表徵圖    Image img = new ImageIcon("icon.gif").getImage(); setIconImage(img); }}

 

 

 

三、在組件中顯示資訊
import javax.swing.*;import java.awt.*;/** * 檔案名稱:NotHelloWorld * 功能:在組件中顯示資訊 * 步驟: *   1.  */public class NotHelloWorld{   public static void main(String[] args)   {      EventQueue.invokeLater(new Runnable()         {            public void run()            {               JFrame frame = new NotHelloWorldFrame();               frame.setTitle("NotHelloWorld");               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);               frame.setVisible(true);            }         });   }}/** *  建構函式中建立一個新的面板,且加入到架構中 */class NotHelloWorldFrame extends JFrame{   public NotHelloWorldFrame()   {      add(new NotHelloWorldComponent());      pack();   }}/** *  Graphics 畫筆繼承自JCompoent, 也可繼承自JPanel */class NotHelloWorldComponent extends JComponent{   public static final int MESSAGE_X = 75;   public static final int MESSAGE_Y = 100;   private static final int DEFAULT_WIDTH = 300;   private static final int DEFAULT_HEIGHT = 200;   public void paintComponent(Graphics g)   {      g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);   }      public Dimension getPreferredSize()     {         return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);     }}    

 

 

 

四、使用畫筆畫一個坦克
import javax.swing.*;import java.awt.*;/** *  功能: 畫出坦克 *  步驟 *    1. 建立坦克類Tank *    2. 建立我的坦克類Hero 繼承 Tank *    3. 建立我的面板MyPanel 繼承 JPanel *    4. 在TankGame1類中增加MyPanel *     */public class TankGame1 extends JFrame{        //建立一個面板對象mp    MyPanel mp = null;                public static void main(String[] args)    {        TankGame1 mtg = new TankGame1();        }        //建構函式    public TankGame1()    {        mp = new MyPanel();        this.add(mp);                //增加進面板中        this.setSize(400, 300);  // 大小            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);     //顯示    }    }//我的面板class MyPanel extends JPanel{    //定義一個我的坦克    Hero hero = null;        //建構函式    public MyPanel()    {        hero = new Hero(100,100);    }        //重新paint    public void paint(Graphics g)    {        super.paint(g);                //定下即時區域        g.fillRect(0, 0, 400, 300);                        //調用函數drawTank畫出我的坦克        this.drawTank(hero.getX(), hero.getY(), g, 0, 1);            }        //畫出坦克的函數    public void drawTank(int x, int y, Graphics g, int direct, int type)    {                //判斷是什麼類型的坦克        switch(type)        {        case 0:            g.setColor(Color.cyan);            break;        case 1:            g.setColor(Color.yellow);        }                //判斷方向        switch(direct)        {        //向上        case 0:            //畫出我的坦克            //1.畫出左邊的矩形            g.fill3DRect(x, y, 5, 30,false);                        //2. 現出右邊的矩形            g.fill3DRect(x+15, y, 5, 30,false);                        //3. 畫出中間矩形            g.fill3DRect(x+5, y+5, 10, 20,false);                        //4. 畫出圓形            g.fillOval(x+5, y+10, 10, 10);                        //5. 畫出線            g.drawLine(x+10, y+15, x+10, y);                    }    }}//坦克類class Tank{    int x = 0;        //表示坦克的橫座標    int y = 0;        //表示坦克的縱座標        Tank(int x, int y)    {        this.x = x;        this.y = y;    }        public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    }//我的坦克class Hero extends Tank{    public Hero(int x, int y)    {        super(x, y);    }}

 

 

 

java - 圖形程式設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.