国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

構(gòu)造圖像,線段,矩形,橢圓,圓角矩形

2019-11-14 10:57:08
字體:
供稿:網(wǎng)友
package thinking;import java.awt.*;import javax.swing.*;public class My extends JFrame{public My(){setLayout(new GridLayout(2,3,5,5));add(new FigurePanel(FigurePanel.LINE));//FigurePanel是自己定義的類,調(diào)用FigurePanel中的構(gòu)造方法add(new FigurePanel(FigurePanel.RECTANGLE));add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));add(new FigurePanel(FigurePanel.OVAL,true));add(new FigurePanel(FigurePanel.RECTANGLE,true));add(new FigurePanel(FigurePanel.ROUND_RECTANGLE,true));}public static void main(String[] args){My frame = new My();frame.setTitle("xx");frame.setSize(600,300);frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);frame.setLocationRelativeTo(null);frame.setVisible(true);}}  class FigurePanel extends JPanel { //構(gòu)建FigurePanel類          // 定義常量    public static final int LINE = 1;    public static final int RECTANGLE = 2;    public static final int ROUND_RECTANGLE = 3;    public static final int OVAL = 4;    PRivate int type = 1;    private boolean filled = false;    public FigurePanel()//無參構(gòu)造方法    {      }          public FigurePanel(int type)    {        this.type = type;    }        public FigurePanel(int type, boolean filled)    {          this.type = type;          this.filled = filled;      }            public void paintComponent(Graphics g)  //方法    {          super.paintComponents(g);                    int width = getWidth();        int height = getHeight();                  System.out.println("width = " + width + " heigth = "+ height);          System.out.println("width = " + ((int)width*0.1) + " heigth = "+ height);                        switch (type)  //類型        {          case LINE:  //LINE類型時(shí)的行為        {              g.setColor(Color.YELLOW);              g.drawLine(10, 10, width - 10, height - 10);              g.drawLine(width - 10, 10, 10, height - 10);             break;        }          case RECTANGLE:  //RECTANGLE類型時(shí)的行為        {              g.setColor(Color.BLUE);//顏色            if (filled)  //是否填充            {                  g.fillRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));              }              else              {                  g.drawRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));              }            break;        }          case ROUND_RECTANGLE:  //圓角矩形        {              g.setColor(Color.RED);              if (filled)              {                  g.fillRoundRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height), 40, 40);              }              else              {                  g.drawRoundRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height), 40, 40);              }            break;        }        case OVAL://橢圓        {            g.setColor(Color.BLACK);              if (filled)            {                  g.fillOval((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));              }            else            {                g.drawOval((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));              }        }        }    }          public void setType(int type)    {        this.type = type;        repaint();//repaint方法調(diào)用會(huì)引起paintComponent方法被調(diào)用。repaint方法被調(diào)用以刷新視圖區(qū)域    }    public int getType()    {        return type;    }        public void setFilled(boolean filled)    {        this.filled = filled;         repaint();//repaint方法調(diào)用會(huì)引起paintComponent方法被調(diào)用。repaint方法被調(diào)用以刷新視圖區(qū)域    }        public boolean isFilled()      {          return filled;      }          public Dimension getPreferredSize()    {        return new Dimension(10, 10);      }  }  
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 庆元县| 吉林省| 莲花县| 库尔勒市| 陇川县| 雷州市| 明溪县| 昂仁县| 枝江市| 阜新市| 虎林市| 廊坊市| 石柱| 巢湖市| 湘潭市| 霍山县| 四平市| 澎湖县| 陆川县| 寿宁县| 浦北县| 闻喜县| 云和县| 绍兴县| 桂阳县| 资源县| 哈密市| 大兴区| 建阳市| 五大连池市| 陇川县| 山东省| 壶关县| 崇州市| 沂南县| 嘉义县| 宣化县| 拜泉县| 大洼县| 嘉义县| 巩义市|