Graphics对象强制转换为Graphics2D对象后才能在屏幕上画图或文本

问题描述

想法问一下,上次有许多朋友说的,父类对象不可以直接强制转换为子类对象如A类、B类,BextendsA的Aa=newA();Bb=newB();b=(B)a;//把父类对象强制转换为子类对象,编译无错,执行时出错。但是为什么书的Graphics对象强制转换为Graphics2D对象后才能在屏幕上画图或文本?Graphics2D类是Graphics类的子类,用来绘制2D图形,这句话的意思是把Graphics类的对象g,强制转换成Graphics2D类的对象g2d。importjava.awt.*;importjava.applet.*;importjava.awt.geom.*;importjava.util.*;publicclassRandomShapesextendsApplet{privateShapeshape;publicvoidinit(){shape=newRectangle2D.Double(-1.0,-1.0,1.0,1.0);}publicvoidpaint(Graphicsg){Graphics2Dg2d=(Graphics2D)g;//★★★这句话是什么意思,为什么有这样的用法?AffineTransformidentity=newAffineTransform();Randomrand=newRandom();intwidth=getSize().width;intheight=getSize().height;g2d.setColor(Color.BLACK);g2d.fillRect(0,0,width,height);for(intn=0;n<300;n++){g2d.setTransform(identity);g2d.translate(rand.nextInt()%width,rand.nextInt()%height);g2d.rotate(Math.toRadians(360*rand.nextDouble()));g2d.scale(60*rand.nextDouble(),60*rand.nextDouble());g2d.setColor(newColor(rand.nextInt()));g2d.fill(shape);}}}

解决方案

解决方案二:
请问,父类对象到底能不能转换为子类对象?
解决方案三:
还有那个在struts1.x中的execute()方法中的四个参数就有ActionFormform,在方法体中就要把form这个父类对象,强制转换为自己设计的myForm类,myFormmyform=(myForm)form;才能获得传递的提交表单数据。form是ActionForm来的,"RegForm也继承了ActionForm",一定要强制类型转换才行!这里父类对象强制转换为子类对象,为什么我们在直接写Aa=newA();Bb=newB();b=(B)a;就错了呢?

时间: 2024-09-17 04:47:34

Graphics对象强制转换为Graphics2D对象后才能在屏幕上画图或文本的相关文章

asp-无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

问题描述 无法将类型为"System.Int32"的对象强制转换为类型"System.Array" 修改Edit.cshtml上部分字段,点击保存跳转到Index.cshtml. 但现在点击保存,就跳出以下错误. **这是Model ** using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnota

visual studio-IIS7上出现错误无法将类型为 的 COM 对象强制转换为接口类型 。

问题描述 IIS7上出现错误无法将类型为 的 COM 对象强制转换为接口类型 . 求助:使用visual studio 2015编写设计网站,使用VS的调试工具进行调试可以正常运行.但是部署到IIS7上出现错误无法将类型为"System.__ComObject"的 COM 对象强制转换为接口类型"OpcRcw.Da.IOPCServer".此操作失败的原因是对 IID 为"{39C13A4D-011E-11D0-9675-0020AFD8ADB3}&quo

无法将类型为“System.Web.UI.WebControls.HiddenField”的对象强制转换为类型的错误的解决_实用技巧

本文章将解决: 1.解释什么是System.Web.UI.LiteralControl 2.为什么会经常发生法将类型为"System.Web.UI.WebControls.HiddenField"的对象强制转换为类型--的错误 3.GridView控件中查找模板列的控件(解释Cells和Controls) 在用GridView控件绑定数据时,经常会要将某列转换为模板列,如: 我们在页面中拉一控件GridView,名字:GVData,将其中某列转换为模板列,在这里我们转换的是列"

西门子-OPC-无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

问题描述 OPC-无法将类型为"System.Int32"的对象强制转换为类型"System.Array" 在用VB.NET编写OPC读写西门子S7400程序时候,碰到以上句柄问题,请问该问题如何解决? Public Shared MyItemServerHandles2 As System.Array Private Sub PLC_AsyncWrite(ByVal NumItemsW As Integer, ByRef ItemValuesW() As Syst

无法将类型为“System.Windows.Forms.BindingSource”的对象强制转换为类型“System.Data.DataTable”

问题描述 c#无法将类型为"System.Windows.Forms.BindingSource"的对象强制转换为类型"System.Data.DataTable"代码:System.Data.DataTabledataSource=newSystem.Data.DataTable();if(base.DataSourceisDataView){dataSource=((DataView)base.DataSource).ToTable();}else{dataSo

microsoft office-Word.Application的 COM 对象强制转换为接口类型时报错

问题描述 Word.Application的 COM 对象强制转换为接口类型时报错 asp.net的项目中有个导出word的功能,每当执行 doc = appWord.Documents.Add(ref templateFile, ref objfalse, ref objDocType, ref objtrue); 到这句的时候报错,catch到一个异常 此时,进程里已经产生一个word进程. 在处理异常的时候, doc.Close(ref doNotSaveChanges, ref miss

我引用speech框架,编译没有错,执行的时候报错!无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型。。。。。。

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Speech.Synthesis;namespaceBank{public

无法将类型为“System.Security.Principal.WindowsPrincipal”的对象强制转换为类型“Business.PhilePrincipal”

问题描述 无法将类型为"System.Security.Principal.WindowsPrincipal"的对象强制转换为类型"Business.PhilePrincipal".说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.InvalidCastException:无法将类型为"System.Security.Principal.WindowsPrin

一个判断题 ,父类对象到底能不能强制转换为子类对象

问题描述 父类对象到底能不能强制转换为子类对象请给个明确的回答,到底是能还是不能! 解决方案 解决方案二:应该不行吧,做个试验不就得了吗?解决方案三:BextendsA;CextendsA;Aa1=newA();Bb1=(B)a1;//classcastexceptionAa2=newC();Bb2=(B)a2;//classcastexceptionAa3=newB();Bb3=(B)a3;//OK解决方案四:[Quote=引用2楼haydenwang8287的回复:]Aa3=newB();B