从超类到超类获取浮动属性
acquiring float properties from supclass to super
我正在为学校创建一个类似应用程序的绘画项目,在我当前的代码中,我有几个子类和一个超级类。 super 应该保存要绘制的形状数组,每个形状对象都应该是它自己的子类,我稍后必须将其放入数组并从应用程序调用。我必须使用 JDesktopPane 和 JInternalFrame,我不能使用 Arraylists,而且我目前一直在尝试将我的 RectDraw 子类的 Float 转换为我的 super。在最终将工具嵌套在名为 MyShapes 的超类中之前,所有这一切。欢迎任何帮助。我不常使用 jdesktopPane,而且我不擅长转换。
public class myShapes {
public void paint(Graphics g) {
graphSettings = (Graphics2D)g;
graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphSettings.setStroke(new BasicStroke(4));
Iterator<Color> strokeCounter = shapeStroke.iterator();
Iterator<Color> fillCounter = shapeFill.iterator();
Iterator<Float> transCounter = transPercent.iterator();
for (Shape s : shapes){
graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transCounter.next()));
graphSettings.setPaint(strokeCounter.next());
graphSettings.draw(s);
graphSettings.setPaint(fillCounter.next());
graphSettings.fill(s);
}
if (drawStart != null && drawEnd != null){
graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f));
graphSettings.setPaint(Color.LIGHT_GRAY);
Shape aShape = null;
if (currentAction == 2){
RectDraw drawRectangle = new RectDraw();
aShape = drawRectangle(x1, y1, x2, y2);
}
else if (currentAction == 3){
CircleDraw drawEllipse = new CircleDraw();
aShape = drawEllipse(x1, y1, x2, y2);
}
else if (currentAction == 4) {
LineDraw drawLine = new LineDraw();
aShape = drawLine(x1, y1, x2, y2);
}
graphSettings.draw(aShape);
}
}
}
这些是我的子类
package mainPackage;
import java.awt.geom.Rectangle2D;
public class RectDraw extends myShapes {
public Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {
int RDx, RDy, RDwidth, RDheight;
RDx = Math.min(x1, x2);
RDy = Math.min(y1, y2);
RDwidth = Math.abs(x1 - x2);
RDheight = Math.abs(y1 - y2);
return new Rectangle2D.Float(RDx, RDy, RDwidth, RDheight);
}
}
除了名字,其他完全一样
public class CircleDraw extends myShapes {
public Ellipse2D.Float drawEllipse(int x1, int y1, int x2, int y2){
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
return new Ellipse2D.Float(x, y, width, height);
}
}
public class LineDraw extends myShapes {
public Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
return new Line2D.Float(x1, y1, x2, y2);
}
}
我不断收到 cannot be resolved to a variable
RectDraw drawRectangle = new RectDraw();
aShape = drawRectangle(x1, y1, x2, y2);
基于第一行drawRectangle是RectDraw的一个实例;
在第二行中,您将参考名称 "drawRectangle" 与 RectDraw 中的同名方法混淆了 class
您必须使用 RectDraw class 的实例调用该方法,如下所示。为清楚起见更改实例名称
RectDraw rectDraw = new RectDraw();
aShape = rectDraw.drawRectangle(x1, y1, x2, y2);
所有形状都存在同样的问题..
此外,您在循环中创建了很多实例,这不是一个好的做法。
而且您的问题太混乱了,请在提问时尽量说清楚:- https://whosebug.com/help/how-to-ask :)
我想补充一点,这不是一个好的 class 层次结构。您的形状应该从基础形状抽象 Class 扩展,然后在您的主要方法中您可以创建一个形状列表。
您无法访问 "super class" 对象,因为形状不是实例变量。我不会使用 myShapes class 但如果你必须的话,你应该将 Shape 方法分离到 Shape.java class 并让 myShapes.java class 处理形状列表创作。
abstract class Shape(){
abstract void draw();
}
那么你的其他子class 形状应该扩展并实现 paint 方法:
public class Rectangle extends Shape(){
//instance variables
public void draw();
}
关于主要方法:
Shape[] myShapesArray = new Shape()[10];
我建议查找工厂模式。 Factory 使用它会很有意义,并清理您的代码。同样限制为数组而不使用列表会使绘图应用程序更难管理。因为您总是需要知道在数组初始化时需要多少项。
我正在为学校创建一个类似应用程序的绘画项目,在我当前的代码中,我有几个子类和一个超级类。 super 应该保存要绘制的形状数组,每个形状对象都应该是它自己的子类,我稍后必须将其放入数组并从应用程序调用。我必须使用 JDesktopPane 和 JInternalFrame,我不能使用 Arraylists,而且我目前一直在尝试将我的 RectDraw 子类的 Float 转换为我的 super。在最终将工具嵌套在名为 MyShapes 的超类中之前,所有这一切。欢迎任何帮助。我不常使用 jdesktopPane,而且我不擅长转换。
public class myShapes {
public void paint(Graphics g) {
graphSettings = (Graphics2D)g;
graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphSettings.setStroke(new BasicStroke(4));
Iterator<Color> strokeCounter = shapeStroke.iterator();
Iterator<Color> fillCounter = shapeFill.iterator();
Iterator<Float> transCounter = transPercent.iterator();
for (Shape s : shapes){
graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transCounter.next()));
graphSettings.setPaint(strokeCounter.next());
graphSettings.draw(s);
graphSettings.setPaint(fillCounter.next());
graphSettings.fill(s);
}
if (drawStart != null && drawEnd != null){
graphSettings.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f));
graphSettings.setPaint(Color.LIGHT_GRAY);
Shape aShape = null;
if (currentAction == 2){
RectDraw drawRectangle = new RectDraw();
aShape = drawRectangle(x1, y1, x2, y2);
}
else if (currentAction == 3){
CircleDraw drawEllipse = new CircleDraw();
aShape = drawEllipse(x1, y1, x2, y2);
}
else if (currentAction == 4) {
LineDraw drawLine = new LineDraw();
aShape = drawLine(x1, y1, x2, y2);
}
graphSettings.draw(aShape);
}
}
}
这些是我的子类
package mainPackage;
import java.awt.geom.Rectangle2D;
public class RectDraw extends myShapes {
public Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {
int RDx, RDy, RDwidth, RDheight;
RDx = Math.min(x1, x2);
RDy = Math.min(y1, y2);
RDwidth = Math.abs(x1 - x2);
RDheight = Math.abs(y1 - y2);
return new Rectangle2D.Float(RDx, RDy, RDwidth, RDheight);
}
}
除了名字,其他完全一样
public class CircleDraw extends myShapes {
public Ellipse2D.Float drawEllipse(int x1, int y1, int x2, int y2){
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
return new Ellipse2D.Float(x, y, width, height);
}
}
public class LineDraw extends myShapes {
public Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
return new Line2D.Float(x1, y1, x2, y2);
}
}
我不断收到 cannot be resolved to a variable
RectDraw drawRectangle = new RectDraw();
aShape = drawRectangle(x1, y1, x2, y2);
基于第一行drawRectangle是RectDraw的一个实例;
在第二行中,您将参考名称 "drawRectangle" 与 RectDraw 中的同名方法混淆了 class
您必须使用 RectDraw class 的实例调用该方法,如下所示。为清楚起见更改实例名称
RectDraw rectDraw = new RectDraw();
aShape = rectDraw.drawRectangle(x1, y1, x2, y2);
所有形状都存在同样的问题..
此外,您在循环中创建了很多实例,这不是一个好的做法。
而且您的问题太混乱了,请在提问时尽量说清楚:- https://whosebug.com/help/how-to-ask :)
我想补充一点,这不是一个好的 class 层次结构。您的形状应该从基础形状抽象 Class 扩展,然后在您的主要方法中您可以创建一个形状列表。
您无法访问 "super class" 对象,因为形状不是实例变量。我不会使用 myShapes class 但如果你必须的话,你应该将 Shape 方法分离到 Shape.java class 并让 myShapes.java class 处理形状列表创作。
abstract class Shape(){
abstract void draw();
}
那么你的其他子class 形状应该扩展并实现 paint 方法:
public class Rectangle extends Shape(){
//instance variables
public void draw();
}
关于主要方法:
Shape[] myShapesArray = new Shape()[10];
我建议查找工厂模式。 Factory 使用它会很有意义,并清理您的代码。同样限制为数组而不使用列表会使绘图应用程序更难管理。因为您总是需要知道在数组初始化时需要多少项。