A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小石头39910 中级黑马   /  2013-5-17 09:26  /  1733 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Sword 于 2013-5-20 12:53 编辑

用gui设计一个程序 一个框框里面有个圆圈(红色),背景是绿色
还请高手有详细的说明,我对画图这方面非常、、、、、嘿嘿

点评

楼主的这个题目好熟悉。。。 当时是 MFC 课程考试  发表于 2013-5-20 18:43

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

5 个回复

倒序浏览
本帖最后由 Sword 于 2013-5-18 01:19 编辑

代码和注释如下:
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import javax.swing.JFrame;


  5. public class Test {

  6.         /**
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.        //System.out.println("Hello World");
  12.         
  13.         new MyFrame();
  14.         }

  15. }
  16. class MyFrame extends JFrame{
  17.            
  18.           int x=200;
  19.           int y=200;
  20.           //离屏画布
  21.           Image offs;
  22.           //离屏画笔
  23.           Graphics offg;
  24.          

  25.            public MyFrame()
  26.            {
  27.                   //设置窗体的名称
  28.                    setTitle("我的GUI小程序");
  29.                    //设置窗体起始的左上角的位置和宽度高度
  30.                    setBounds(100, 100, 800,600);
  31.                    //设置界面可显示
  32.                    setVisible(true);
  33.                    setDefaultCloseOperation(EXIT_ON_CLOSE);
  34.                   
  35.            }
  36.            //设置初始坐标和结束坐标
  37.            int startA=0;
  38.            int endA=360;
  39.            //设置起始位置
  40.            int arc_x=200;
  41.            int arc_y=100;
  42.             
  43.            
  44.            
  45.            //Graphics设置画笔
  46.            public void paint(Graphics g)
  47.            {
  48.                    if(offs==null)
  49.                    {
  50.                           
  51.                            //初始化离屏画布
  52.                            offs=createImage(800,600);
  53.                            //初始化离幕画笔
  54.                            offg=offs.getGraphics();
  55.                            
  56.                    }
  57.                   
  58.                    //画背景
  59.                    offg.setColor(Color.green);
  60.                    offg.fillRect(0, 0, 800, 600);
  61.            
  62.                    //画矩形
  63.                    offg.setColor(Color.red);
  64.                    offg.fillRect(x, y, 80, 50);
  65.                   
  66.                    //画圆
  67.                    offg.fillArc(arc_x, arc_y, 50,50, startA, endA);
  68.                    g.drawImage(offs, 0, 0, null);
  69.                   
  70.            System.out.println("Paint方法");
  71.                   
  72.            }
  73. }
复制代码
运行的结果如下,不知道是否符合要求:

GUI.png (25.07 KB, 下载次数: 0)

GUI.png

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

回复 使用道具 举报
Sword 发表于 2013-5-18 01:17
代码和注释如下:运行的结果如下,不知道是否符合要求:

{:soso_e179:}
回复 使用道具 举报
Sword 发表于 2013-5-18 01:17
代码和注释如下:运行的结果如下,不知道是否符合要求:

亲,是在空心的矩形里面画一个实心的圆哦!
回复 使用道具 举报
  1. package com.mygame;

  2. import java.awt.Color;
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.Image;

  6. import javax.swing.JFrame;

  7. public class MyGUI extends JFrame{
  8.    
  9.         //起始坐标
  10.     int x = 170;
  11.     int y = 65;
  12.     //矩形的宽度和高度
  13.         int weight = 100;
  14.         int height = 60;
  15.        
  16.         //离屏画布
  17.     Image offs;
  18.     //离屏画笔
  19.     Graphics offg;
  20.    
  21.         public static void main(String[] args) {
  22.                 // TODO Auto-generated method stub
  23.          
  24.                  new MyGUI();
  25.         }

  26.        
  27.         public MyGUI()
  28.         {
  29.                 super("MyGUI");
  30.                 setSize(350,500);
  31.                
  32.                  //设置界面可显示
  33.                 setVisible(true);
  34.                 //设置窗体起始的左上角的位置和宽度高度
  35.         setBounds(100, 100, 800,600);
  36.         //设置界面可显示
  37.         setVisible(true);
  38.         //关闭窗体
  39.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  40.                
  41.         }
  42.        
  43.         public void paint(Graphics g)
  44.         {
  45.                 if(offs==null)
  46.         {
  47.                
  48.                 //初始化离屏画布
  49.                 offs=createImage(800,600);
  50.                 //初始化离幕画笔
  51.                 offg=offs.getGraphics();
  52.                
  53.         }
  54.                 //画背景
  55.         offg.setColor(Color.green);
  56.         offg.fillRect(0, 0, 800, 600);
  57.         
  58.         //画矩形
  59.         offg.setColor(Color.black);
  60.         offg.drawRect(x, y, weight, height);
  61.         
  62.         //画圆
  63.         offg.setColor(Color.RED);
  64.         offg.fillOval(x+weight/2-30, y, 60,height);
  65.         g.drawImage(offs, 0, 0, null);
  66.         
  67.         }
  68. }
复制代码
改了一下,运行结果如下:

GUI小程序.png (22.08 KB, 下载次数: 0)

GUI小程序.png
回复 使用道具 举报
如果没问题了,请把分类改成“已解决”,谢谢合作
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马