201711671132《java实用教程》第十四章学习总结
一、教材学习内容总结
(1)教材学习中的问题和解决过程
(2)代码调试内容总结(课后习题),无问题
1.import java.awt.*;
import javax.swing.*;
class MyCanvas extends Canvas {
static int pointX[]=new int[5],
pointY[]=new int[5];
public void paint(Graphics g) {
g.translate(200,200) ; //进行坐标变换,将新的坐标原点设置为(200,200)。
pointX[0]=0;
pointY[0]=-120;
double arcAngle=(72*Math.PI)/180;
for(int i=1;i<5;i++) {
pointX[i]=(int)(pointX[i-1]*Math.cos(arcAngle)-pointY[i-1]*Math.sin(arcAngle));
pointY[i]=(int)(pointY[i-1]*Math.cos(arcAngle)+pointX[i-1]*Math.sin(arcAngle));
}
g.setColor(Color.red);
int starX[]={pointX[0],pointX[2],pointX[4],pointX[1],pointX[3],pointX[0]};
int starY[]={pointY[0],pointY[2],pointY[4],pointY[1],pointY[3],pointY[0]};
g.drawPolygon(starX,starY,6);
}
}
public class E {
public static void main(String args[]) {
JFrame f=new JFrame();
f.setSize(500,450);
f.setVisible(true);
MyCanvas canvas=new MyCanvas();
f.add(canvas,"Center");
f.validate();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
2.import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.red) ;
Graphics2D g_2d=(Graphics2D)g;
QuadCurve2D quadCurve=
new QuadCurve2D.Double(2,10,51,90,100,10);
g_2d.draw(quadCurve);
quadCurve.setCurve(2,100,51,10,100,100);
g_2d.draw(quadCurve);
}
}
public class E {
public static void main(String args[]) {
JFrame f=new JFrame();
f.setSize(500,450);
f.setVisible(true);
MyCanvas canvas=new MyCanvas();
f.add(canvas,"Center");
f.validate();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
3.import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.red) ;
Graphics2D g_2d=(Graphics2D)g;
CubicCurve2D cubicCurve=
new CubicCurve2D.Double(0,70,70,140,140,0,210,70);
g_2d.draw(cubicCurve);
}
}
public class E {
public static void main(String args[]) {
JFrame f=new JFrame();
f.setSize(500,450);
f.setVisible(true);
MyCanvas canvas=new MyCanvas();
f.add(canvas,"Center");
f.validate();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
4. import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
class Flower extends Canvas
{ public void paint(Graphics g)
{ Graphics2D g_2d=(Graphics2D)g;
//花叶两边的曲线:
QuadCurve2D
curve_1=new QuadCurve2D.Double(200,200,150,160,200,100);
CubicCurve2D curve_2=
new CubicCurve2D.Double(200,200,260,145,190,120,200,100);
//花叶中的纹线:
Line2D line=new Line2D.Double(200,200,200,110);
QuadCurve2D leaf_line1=
new QuadCurve2D.Double(200,180,195,175,190,170); QuadCurve2D leaf_line2=
new QuadCurve2D.Double(200,180,210,175,220,170); QuadCurve2D leaf_line3=
new QuadCurve2D.Double(200,160,195,155,190,150); QuadCurve2D leaf_line4=
new QuadCurve2D.Double(200,160,214,155,220,150);
//利用旋转来绘制花朵:
AffineTransform trans=new AffineTransform();
for(int i=0;i<6;i++)
{ trans.rotate(60*Math.PI/180,200,200);
g_2d.setTransform(trans);
GradientPaint gradient_1=
new GradientPaint(200,200,Color.green,200,100,Color.yellow);
g_2d.setPaint(gradient_1);
g_2d.fill(curve_1);
GradientPaint gradient_2=new
GradientPaint(200,145,Color.green,260,145,Color.red,true);
g_2d.setPaint(gradient_2);
g_2d.fill(curve_2);
Color c3=new Color(0,200,0); g_2d.setColor(c3);
g_2d.draw(line);
g_2d.draw(leaf_line1); g_2d.draw(leaf_line2);
g_2d.draw(leaf_line3); g_2d.draw(leaf_line4);
}
//花瓣中间的花蕾曲线:
QuadCurve2D center_curve_1=
new QuadCurve2D.Double(200,200,190,185,200,180); AffineTransform trans_1=new AffineTransform();
for(int i=0;i<12;i++)
{ trans_1.rotate(30*Math.PI/180,200,200);
g_2d.setTransform(trans_1);
GradientPaint gradient_3=
new GradientPaint(200,200,Color.red,200,180,Color.yellow);
g_2d.setPaint(gradient_3);
g_2d.fill(center_curve_1);
}
}
}
public class E {
public static void main(String args[]) {
JFrame f=new JFrame();
f.setSize(500,450);
f.setVisible(true);
Flower canvas=new Flower();
f.add(canvas,"Center");
f.validate();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
5.import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
class Moon extends Canvas
{ public void paint(Graphics g)
{ Graphics2D g_2d=(Graphics2D)g;
Ellipse2D ellipse1=
new Ellipse2D. Double (20,80,60,60),
ellipse2=
new Ellipse2D. Double (40,80,80,80);
g_2d.setColor(Color.white);
Area a1=new Area(ellipse1),
a2=new Area(ellipse2);
a1.subtract(a2); //"差"
g_2d.fill(a1);
}
}
public class E {
public static void main(String args[]) {
JFrame f=new JFrame();
f.setSize(500,450);
f.setVisible(true);
Moon moon=new Moon();
moon.setBackground(Color.black);
f.add(moon,"Center");
f.validate();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}(3)无疑问