Java 在 Eclipse 中遇到未知错误 - 不会绘制对象

标签 java eclipse swing jpanel drawing

我一直在尝试将我的旧 Java 程序修改为数字逻辑模拟器,但在某个点之后它停止绘制我初始化的对象。它会绘制白色背景,但仅此而已。 请原谅一些违规行为,我最终会让它做的事情比我目前试图做的多得多——现在它甚至不画电线(我已经解决了)。我想做的就是“生成”一个简单的、工作的输入输出电路。

最初,所有内容都返回了名为“未知来源”的错误,但在我切换到 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)

最佳答案

gates = new ArrayList<OutPin>(); //Unused currently
...
OutPin b = gates.get(0); // line 48

gates 为空,没有元素号 0。这就是您收到 IndexOutOfBoundsException 的原因。您可能打算改为执行 outputs.get(0)

关于Java 在 Eclipse 中遇到未知错误 - 不会绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634063/

相关文章:

java - Swing 组件不可见

java - 我们可以从 java 中的另一个类更改 JLabel 吗?

java - 返回有 2 个子节点的节点数

eclipse - Eclipse的 “clean project”和Maven的 “mvn clean”在m2e中的区别

java - 致命异常 : AsyncTask #2 An error occured while executing doInBackground() IndexOutOfBoundsException: Invalid index 1, 大小为 1

java - 如何配置 eclipse 以使用额外参数运行我的 java 程序?

eclipse - 如何在Eclipse Juno中运行Servlet?

java - 仅当遇到第一个字符时才将荧光笔应用于行,直到 JtextArea 中的 LineEndOffset

java - 对包含带数字的字符串的文件名数组进行排序

java - Spring Boot请求映射正则表达式