我一直在尝试将我的旧Java程序修改为数字逻辑模拟器,但在某一点之后它停止绘制我初始化的对象。它会画出白色背景,但就是这样。 原谅一些违规行为,我最终会让它做的比我现在做的要多得多 - 现在它甚至没有画出电线(我已经解决了)。我所要做的就是“产生”一个简单的工作输入输出电路。
最初,它返回的所有内容都被称为“Unknown Source”,但在我切换到JDK后,所有错误都归因于EventDispatchThread或EventQueue。这是我的代码,抱歉这个烂摊子:
CircuitLogic.java(主类):启动JFrame,几乎不会修改以前的程序。
import javax.swing.JFrame;
public class CircuitLogic{
public static void main( String[] args ){
JFrame application = new JFrame( "Circuit Board" );
Board broad = new Board();
application.add(broad);
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.setSize( 800, 600 ); // set frame size
application.setVisible( true ); // display frame
}
}
Board.java:
import java.util.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class Board extends JPanel
implements MouseListener, MouseMotionListener
{
/** // I don't know what this stuff does, Eclipse added it.
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<InPin> inputs;
private ArrayList<OutPin> gates; //Unused currently
private ArrayList<OutPin> outputs;
// double buffering
private Image backBuffer;
private Graphics gBackBuffer;
boolean isInitialized;
public Board()
{
isInitialized=false;
// handle mouse and mouse motion events
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
void init()
{
// Initial state
inputs = new ArrayList<InPin>();
gates = new ArrayList<OutPin>(); //Unused currently
outputs = new ArrayList<OutPin>();
inputs.add(new InPin(10,10));
outputs.add(new OutPin(500,10));
InPin a = inputs.get(0);
OutPin b = gates.get(0); // line 48
b.attach_in(a);
// create the back buffer
backBuffer = createImage(getSize().width, getSize().height);
gBackBuffer = backBuffer.getGraphics();
}
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // clears drawing area; tried removing this
if (!isInitialized) {
isInitialized=true;
init(); // line 62
}
gBackBuffer.setColor(Color.white);
gBackBuffer.clearRect(0, 0, 800, 700);
for (int i=0; i<inputs.size(); i++) {
inputs.get(i).draw(gBackBuffer);
}
for (int i=0; i<outputs.size(); i++) {
outputs.get(i).check();
outputs.get(i).draw(gBackBuffer);
}
g.drawImage(backBuffer, 0, 0, null);
} // end method paintComponent
public void mouseClicked( MouseEvent e ){
}
public void mousePressed( MouseEvent e ){
if (e.isMetaDown()) return; // ignore right button
for (int i=0; i<inputs.size(); i++) {
InPin p=inputs.get(i);
if (p.hitTest(e.getX(), e.getY())) {
p.toggle();
repaint();
return;
}
}
}
public void mouseReleased( MouseEvent e )
{
// runs a check from the end, backwards
for (int i=0; i<outputs.size(); i++) {
OutPin q=outputs.get(i);
q.check(); // line 102
repaint();
}
}
public void mouseEntered( MouseEvent e ){
}
public void mouseExited( MouseEvent e ){
}
public void mouseMoved( MouseEvent e ){
}
public void mouseDragged( MouseEvent e ){
}
}
InPin.java:
import java.awt.*;
public class InPin{
public int x;
public int y;
public boolean is_on;
public InPin(int xx, int yy){
x = xx;
y = yy;
is_on = false;
}
public InPin(InPin src) { // copy constructor
x = src.x;
y = src.y;
is_on = src.is_on;
}
public void draw(Graphics g){
int[] xpts = {x,x+40,x+60,x+40,x};
int[] ypts = {y,y,y+10,y+20,y+20};
if (is_on) {
g.setColor(Color.green);
}
else{
g.setColor(Color.red);
}
g.fillPolygon(xpts,ypts,5);
}
public boolean hitTest(int mx, int my){
return ((x <= mx && mx <= x + 40) && (y <= my && my <= y+20)); //checks if "main rectangle" is clicked
}
public boolean check(){
return (is_on);
}
public void toggle(){
is_on = !is_on;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int newx){
x = newx;
}
public void setY(int newy){
y = newy;
}
}
OutPin.java:
import java.awt.*;
public class OutPin extends InPin{
public InPin[] ins; // trust me, making this an array will pay off
public OutPin(int xx, int yy) {
super(xx, yy);
ins = new InPin[1];
}
public void draw(Graphics g){
if (is_on) {
g.setColor(Color.green);
}
else{
g.setColor(Color.red);
}
g.fillRect(x,y,50,25);
}
public void attach_in(InPin in){
ins[0] = in;
}
public boolean hitTest(int mx, int my){ //practically unused
return ((x <= mx && mx <= x + 50) && (y <= my && my <= y+25));
}
public boolean check(){
is_on = (ins[0].check()); // line 27
return (is_on);
}
}
错误消息: (运行应用程序并单击一次后)
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at Board.init(Board.java:48)
at Board.paintComponent(Board.java:62)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1572)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495)
at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
at javax.swing.JComponent.paint(JComponent.java:1042)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1973)
at java.awt.Window.paint(Window.java:3912)
at javax.swing.RepaintManager$4.run(RepaintManager.java:835)
at javax.swing.RepaintManager$4.run(RepaintManager.java:807)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
at javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at OutPin.check(OutPin.java:27)
at Board.mouseReleased(Board.java:102)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
gates = new ArrayList<OutPin>(); //Unused currently
...
OutPin b = gates.get(0); // line 48
gates
,为空,没有元素编号0.这就是为什么你得到IndexOutOfBoundsException
的原因。您可能打算改为outputs.get(0)
。