我认为这个问题没有被明确提出,所以我将尝试解释:
我正在尝试绘制一个全加器,每个门都被绘制为一个单独的子类;所以加法器是一类,它将调用异或门并放置它们,然后是与门等等。
自己绘制门(并在加法器方法中创建组件)效果很好,但是当我尝试在加法器中调用它们时,我收到了编译器的抱怨。
转换为形状会创建 ClassCastException;而且我不知道为什么编译器不喜欢绘制它们。
这是我所有课程的链接。
Adder.java 包加法器;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import javax.swing.JComponent;
import javax.swing.JFrame;
import andGate.*;
public class Adder
{
private final int HEIGHT = 700;
private final int WIDTH = 1200;
private int xLeft = 0;
private int yTop = 0;
/**
* Constructs a gate with a corner at (0,0)
*/
public Adder()
{
new Adder(xLeft, yTop);
}
/**
* Constructs a gate with the given top left corner
* @param x the x co-ordinate of the top left point
* @param y the y co-ordinate of the top left point
*/
public Adder(int x, int y)
{
xLeft = x;
yTop = y;
}
/**
* Draw the gate
* @param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
AndGate and = new AndGate();
g2.draw(and);
}
}
AdderComponent.java 包加法器;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import andGate.*;
@SuppressWarnings("serial")
public class AdderComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Adder ad = new Adder(20, 20);
ad.draw(g2);
}
}
AdderVeiwer.java 包加法器;
import javax.swing.JFrame;
import andGate.*;
public class AdderVeiwer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(1200, 700);
frame.setTitle("Full Adder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
AdderComponent ad = new AdderComponent();
frame.add(ad);
}
}
AndGate.java 封装andGate;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
public class AndGate
{
private final int WIDTH = 300;
private final int HEIGHT = 100;
private int xLeft = 0;
private int yTop = 0;
/**
* Constructs a gate with a corner at (0,0)
*/
public AndGate()
{
new AndGate(xLeft, yTop);
}
/**
* Constructs a gate with the given top left corner
* @param x the x co-ordinate of the top left point
* @param y the y co-ordinate of the top left point
*/
public AndGate(int x, int y)
{
xLeft = x;
yTop = y;
}
/**
* Draw the gate
* @param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
//Main shape
Point2D.Double p1 = new Point2D.Double(xLeft + WIDTH * 1/3, yTop);
Point2D.Double p2 = new Point2D.Double(xLeft + WIDTH * 1/3, yTop + HEIGHT);
Point2D.Double p3 = new Point2D.Double(xLeft + WIDTH * 7/12, yTop);
Point2D.Double p4 = new Point2D.Double(xLeft + WIDTH * 7/12, yTop + HEIGHT);
Line2D.Double verticle = new Line2D.Double(p1, p2);
Line2D.Double horizontal1 = new Line2D.Double(p1, p3);
Line2D.Double horizontal2 = new Line2D.Double(p2, p4);
Arc2D.Double arc = new Arc2D.Double(xLeft + WIDTH * 4/12, yTop, xLeft + WIDTH * 4/12,
yTop + HEIGHT * 19/24, 270, 180, Arc2D.OPEN);
//Output wire
Point2D.Double p5 = new Point2D.Double(xLeft + WIDTH * 11/15, yTop + HEIGHT * 1/2);
Point2D.Double p6 = new Point2D.Double(xLeft + WIDTH, yTop + HEIGHT * 1/2);
Line2D.Double out = new Line2D.Double(p5, p6);
//Input wire 1
Point2D.Double p7 = new Point2D.Double(xLeft, yTop + HEIGHT * 1/3);
Point2D.Double p8 = new Point2D.Double(xLeft + WIDTH * 1/3, yTop + HEIGHT * 1/3);
Line2D.Double in1 = new Line2D.Double(p7, p8);
//Input wire 2
Point2D.Double p9 = new Point2D.Double(xLeft, yTop + HEIGHT * 2/3);
Point2D.Double p10 = new Point2D.Double(xLeft + WIDTH * 1/3, yTop + HEIGHT * 2/3);
Line2D.Double in2 = new Line2D.Double(p9, p10);
//Draw
g2.draw(verticle);
g2.draw(horizontal1);
g2.draw(horizontal2);
g2.draw(arc);
g2.draw(out);
g2.draw(in1);
g2.draw(in2);
}
}
AndGateComponent.java 封装andGate;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class AndGateComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
AndGate ag = new AndGate(20, 20);
ag.draw(g2);
}
}
AndGateViewer 封装和Gate;
import javax.swing.JFrame;
public class AndGateViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(1200, 700);
frame.setTitle("And Gate");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
AndGateComponent ag = new AndGateComponent();
frame.add(ag);
}
}