質問が明確に尋ねられていないと思うので、説明しようと思います:
各ゲートが個別のサブクラスとして描画される全加算器を描画しようとしています。したがって、加算器は1つのクラスであり、XORゲートを呼び出して配置し、次にandゲートなどを配置します。
ゲートを自分で描画する (および adder メソッドでコンポーネントを作成する) ことはうまくいきますが、adder でそれらを呼び出そうとすると、コンパイラから苦情を受けます。
シェイプにキャストすると、ClassCastException が作成されます。そして、コンパイラがそれらの描画を嫌う理由がわかりません。
これが私のすべてのクラスへのリンクです。
Adder.java パッケージ adder;
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 パッケージ adder;
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 パッケージ adder;
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 パッケージ andGate;
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);
}
}