201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

时间:2024-01-03 12:13:02

实验十二  图形程序设计

实验时间 2018-11-14

1、实验目的与要求

(1) 掌握Java GUI中框架创建及属性设置中常用类的API;

创建空框架

. 在Java中,常采用框架(Frame)创建初始界面,即GUI的顶层窗口

. AWT库中有一个基于对等体的Frame类。
. 该类的Swing版本为JFrame,JFrame是Frame子类。

框架定位与框架属性
定位:
  常用Component类的setLocation和setBounds方法
常用属性
  Title:框架标题
  IconImage:框架图标

(4)确定框架大小
通过调用Toolkit类的方法来得到屏幕尺寸信息。
Toolkit kit = Toolkit.getDefaultToolkit(); //生成Toolkit对象
Dimension screenSize = kit.getScreenSize();//返回Dimension对象的屏幕大小
int screenWidth = screenSize.width;//获得Dimension对象屏幕的宽度
int screenHeight=screenSize.height; ;//获得Dimension对象屏幕的高度创建框架时的常用类及API

java.awt.Component 1.0
– boolean isVisible() 检查组件是否可见
– void setVisible(boolean b) 设置组件可见
– void setSize(int width,int height) 把组件缩放到指定宽度和高度
– void setBounds(int x,int y,int width,int height) 移动并缩放组件
– Dimension getSize() 得到组件的大小
– void setSize(Dimension d) 把组件缩放到指定的大小

(2) 掌握Java GUI中2D图形绘制常用类的API;

 java.awt.geom.Retangle2D.Double 1.2
– Retangle2D.Double(double x,double y,double w,double h)
 java.awt.geom.Retangle2D.Float 1.2
– Retangle2D.Float(float x,float y,float w, float h)
 java.awt.geom.Ellipse2D.Double 1.2
– Ellipse2D.Double(double x,double y,double w,double h)

 java.awt.geom.Point2D.Double 1.2
– Point2D.Double(double x,double y)
 java.awt.geom.Line2D.Double 1.2
– Line2D.double(Point2D start,Point2D end)
– Line2D.double(double startX,doublestartY,double endX,double endY)
– fill(Graphics2D rec) 填充颜色
– draw(Graphics2D rec) 绘制图形rec

 java.awt.Graphic2D 1.2
– void drawString(String str, int x, int y);
– void draw(Shape

(3) 了解Java GUI中2D图形中字体与颜色的设置方法;

Graphics2D类的setPaint方法(Graphics类为setColor方法)用来设置颜色。
例:g2.setPaint(Color.RED);
g2.drawString(“Set Color”,100,100);
(1)Color类中定义的13种标准颜色:BLACK、BLUE、CYAN、DARK_GRAY、GRAY、GREEN、LIGHT_GRAY、MAGENTA、ORANGE、PINK、RED、WHITE、YELLOW

 通过指定红绿蓝三色比例,用Color类对象来复合成一种新的颜色。
 Color构造器如下:
Color(int redness,int greenness,int blueness)
其中参数取值为0--255

(3)常用API教材428页)
 java.awt.Color 1.0
– void Color(int r,int g,int b)
 java.awt.Graphics 1.0
– void setColor(Color c)
– Color getColor( ));
 java.awt.Graphic2D 1.2
– void setPaint(paint p)
– void fill(Shape s)
 java.awt.Cmponent 1.0
– void setBackground(Color c)
void setForeground(Color c)

(2)字体风格
 Font.PLAIN
 Font.BOLD
 Fond.ITALIC
 Fond.BOLD + Font.ITALIC
(3)设置字体
 Font serif=new Font(“Serif”,Font.BOLD,14);
 g2.setFont(serif);

(4)字体应用的常用API(教材434页-435页)
 java.awt.Font 1.0
– Font(String name,int style,int Size)
– String getFontName()
– String getFamily()
– String getName()
– Retangle2D getStringBounds(String s,FontRenderContext context)

(4) 了解Java GUI中2D图像的载入方法。

(1)在Java应用程序中,一旦图像保存在本地或因特网的某个位置上,就可以将它们直接读入到java应用程序中。

String filename = “…”;
Image image = ImageIcon(filename).getImage();

(2)完成将一个图像文件加载到程序中,再调用Graphics类提供的DrawImage()显示它。
public void paintComponent(Graphics g)
{

g.drawImage(image, x, y, null);
}

 java.awt.Graphics 1.0
– boolean drawImage(Image img,int x,int y,ImageObserver observer)
– boolean drawImage(Image img,int x,int y,int width,int height,ImageObserverobserver)
– void copyArea(int x,int y,int width,int height,int dx,int dy)
2、实验内容和步骤

实验1: 导入第10章示例程序,测试程序并进行代码注释。

测试程序1:

l 运行下列程序,观察程序运行结果。

import javax.swing.*;

public class SimpleFrameTest

{

public static void main(String[] args)

{

JFrame  frame = new JFrame();

frame.setBounds(0, 0,300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

l 在elipse IDE中调试运行教材407页程序10-1,结合程序运行结果理解程序;与上面程序对比,思考异同;

l 掌握空框架创建方法;

l 了解主线程与事件分派线程概念;

l 掌握GUI顶层窗口创建技术。

 package SimpleFrame;

 import java.awt.*;
import javax.swing.*; /**
* @version 1.33 2015-05-12
* @author Cay Horstmann
*/
public class SimpleFrameTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
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);
}
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

测试程序2:

l 在elipse IDE中调试运行教材412页程序10-2,结合程序运行结果理解程序;

l 掌握确定框架常用属性的设置方法。

 package sizedFrame;

 import java.awt.*;
import javax.swing.*; /**
* @version 1.34 2015-06-16
* @author Cay Horstmann
*/
public class SizedFrameTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new SizedFrame();
frame.setTitle("SizedFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
} class SizedFrame extends JFrame
{
public SizedFrame()
{
// get screen dimensions Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width; // set frame width, height and let platform pick screen location setSize(screenWidth / 2, screenHeight / 2);
setLocationByPlatform(true); // set frame icon Image img = new ImageIcon("icon.gif").getImage();
setIconImage(img);
}
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

测试程序3:

l 在elipse IDE中调试运行教材418页程序10-3,结合运行结果理解程序;

l 掌握在框架中添加组件;

l 掌握自定义组件的用法。

 package notHelloWorld;

 import javax.swing.*;
import java.awt.*; /**
* @version 1.33 2015-05-12
* @author Cay Horstmann
*/
public class NotHelloWorld
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new NotHelloWorldFrame();
frame.setTitle("NotHelloWorld");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
} /**
* A frame that contains a message panel
*/
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
add(new NotHelloWorldComponent());
pack();
}
} /**
* A component that displays a message.
*/
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); }
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

测试程序4:

l 在elipse IDE中调试运行教材424 -425页程序10-4,结合程序运行结果理解程序;

l 掌握2D图形的绘制方法。

 package draw;

 import java.awt.*;
import java.awt.geom.*;
import javax.swing.*; /**
* @version 1.33 2007-05-12
* @author Cay Horstmann
*/
public class DrawTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new DrawFrame();
frame.setTitle("DrawTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
} /**
* A frame that contains a panel with drawings
*/
class DrawFrame extends JFrame
{
public DrawFrame()
{
add(new DrawComponent());
pack();
}
} /**
* A component that displays rectangles and ellipses.
*/
class DrawComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400; public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g; // draw a rectangle double leftX = 100;
double topY = 100;
double width = 200;
double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect); // draw the enclosed ellipse Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse); // draw a diagonal line g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); // draw a circle with the same center double centerX = rect.getCenterX();
double centerY = rect.getCenterY();
double radius = 150; Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.draw(circle);
} public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

测试程序5:

l 在elipse IDE中调试运行教材432页-433程序10-5,结合程序运行结果理解程序;

l 了解2D图形中字体的设置的方法;

 package font;

 import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*; /**
* @version 1.34 2015-05-12
* @author Cay Horstmann
*/
public class FontTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new FontFrame();
frame.setTitle("FontTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
} /**
* A frame with a text message component
*/
class FontFrame extends JFrame
{
public FontFrame()
{
add(new FontComponent());
pack();
}
} /**
* A component that shows a centered message in a box.
*/
class FontComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g; String message = "Happy every day!"; Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f); // measure the size of the message FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context); // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY();
double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
} public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

测试程序6:

l 在elipse IDE中调试运行教材436页-437程序10-6,结合程序运行结果理解程序;

l 了解2D图形图像的显示方法。

 package image;

 import java.awt.*;
import javax.swing.*; /**
* @version 1.34 2015-05-12
* @author Cay Horstmann
*/
public class ImageTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new ImageFrame();
frame.setTitle("ImageTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
} /**
* A frame with an image component
*/
class ImageFrame extends JFrame
{
public ImageFrame()
{
add(new ImageComponent());
pack();
}
} /**
* A component that displays a tiled image
*/
class ImageComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200; private Image image; public ImageComponent()
{
image = new ImageIcon("blue-ball.gif").getImage();
} public void paintComponent(Graphics g)
{
if (image == null) return; int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null); // draw the image in the upper-left corner g.drawImage(image, 0, 0, null);
// tile the image across the component for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);
} public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}

201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结

实验2:课后完成PTA平台题目集:2018秋季西北师范大学面向对象程序设计(Java)练习题集(ch6-ch9)

实验总结:

 通过这次的课程设计的学习,我学到了Java中开发图形应用程序的常用控件、 熟悉了JAVA SWING中的容器、组件、布局以及事件监视器和事件接口的使用。通过该实验掌握了JAVA图形界面程序设计的方法。

对如何创建框架,以及如何生成一个绘制图形的面板 ,以及如何在面板上绘制图形有了一定的了解,以及如何设置窗口中的字体,以及窗口的背景,还有字体大小字体颜色有了一定了解,通过对源码的查看,对如何设计窗口,以及窗口中的一些属性有了进一步的理解。