在OS X中不显示Java图形

时间:2015-04-10 03:46:05

标签: java eclipse macos swing

我是一名计算机科学专业的学生,​​他使用2009年中期的MacBook Pro运行OS X Yosemite 10.10.3。我最近在面向对象的编程类中有一个类活动,我们一步一步地创建了一个交互式Java程序,用户只需单击一个足球并观察它以绿色背景踢过球门柱。

然而,即使我的Java代码与我的同学的Windows计算机的代码匹配而没有语法错误,但是我的程序正常运行时存在一些问题,而他们的工作完全正常:

虽然应用程序窗口打开时带有标题和绿色背景,但不会显示足球和球门柱。但是,如果我手动拉伸窗口,它们会显示回来。我已经尝试更改窗口尺寸,看看是否导致它无济于事。

当点击足球时,不是按照预期向球门柱移动,足球和球门柱都会消失并且不会返回。即使我尝试再次手动拉伸窗口,也只显示绿色背景。

我仍然将代码提交给我的导师,这在他的计算机上工作正常(他不理解问题,因为他不使用OS X)。我尝试在另外两个IDE上运行代码,看看Eclipse是否存在问题,但它们都产生了相同的结果。如果这是OS X或计算机专用问题,我怎么能解决这个问题?

这是我目前的代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class Football extends JFrame {

   final int WINDOW_WIDTH = 800;
   final int WINDOW_HEIGHT = 400;

   private int x = 40;                // Ball's X coordinate
   private int y = 300;               // Ball's Y coordinate
   private final int WIDTH = 35;      // Ball's width
   private final int HEIGHT = 60;     // Ball's height

   private final int X_MOVE = 14;     // Pixels to move ball
   private final int Y_MOVE = 4;

   private final int TIME_DELAY = 25; // Time delay
   private Timer timer;               // Timer object

   /**
      init method
   */

   public Football() {
       setTitle("Football");
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);

       // Set Background to a Dark Green
       getContentPane().setBackground(new Color(0, 220, 50));         

       // initTimer();
       addMouseListener(new FbMouseListener());
   }

   public void paint(Graphics g)
   {
      // Call the superclass paint method.
      super.paint(g);  
      // Set the color to Brown
      g.setColor(new Color(129, 74, 25));

      // Draw the football
      g.fillOval(x, y, WIDTH, HEIGHT);

      // Draw the Goalpost
      g.setColor(Color.YELLOW);
      g.fillRect(670, 240, 5, 140);
      g.fillRect(610, 80, 5, 140);
      g.fillRect(740, 120, 5, 140);
      // Need Thicker line
      Graphics2D g2 = (Graphics2D) g;
      g2.setStroke(new BasicStroke(5));
      g2.drawLine(612, 220, 742, 260);
   }

   private class TimerListener implements ActionListener
   {
       public void actionPerformed(ActionEvent e) {
        // Update the ball's position
        y -= Y_MOVE;
        x += X_MOVE;

        // Force a call to the paint method
        repaint();  
    }

   }

   public void initTimer()
   {
       timer = new Timer(TIME_DELAY, new TimerListener());
       timer.start();
   }

   private class FbMouseListener implements MouseListener
   {
       public void mouseClicked(MouseEvent e)
       {
           if (e.getX() >= x && e.getX() <= (x + WIDTH) && e.getY() >= y && e.getY() <= (y + HEIGHT))
           {
               initTimer();
           }
       }

       public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

       }

       public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

       }

       public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

       }

       public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

       }
   }

   public static void main(String[] args)
   {
        Football fb = new Football();
   }

}

我们将不胜感激任何帮助或建议,因为我希望确保这不会影响我创建的任何未来计划。

1 个答案:

答案 0 :(得分:1)

通常,覆盖paint这样的顶级容器JFrame是一个坏主意,JFrame包含一堆可以独立于父级绘制的子组件,这似乎是是这样的

RootPane

如您所见,框架与用户之间至少有3个其他组件

通常,您应该创建一个自定义类,其类似于JPanel,并覆盖它的paintComponent并在那里执行自定义绘制。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Football {

    public static void main(String[] args) {
        new Football();
    }

    public Football() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Football");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new FootballPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class FootballPane extends JPanel {

        public static final int WINDOW_WIDTH = 800;
        public static final int WINDOW_HEIGHT = 400;

        private int x = 40;                // Ball's X coordinate
        private int y = 300;               // Ball's Y coordinate
        private static final int WIDTH = 35;      // Ball's width
        private static final int HEIGHT = 60;     // Ball's height

        private static final int X_MOVE = 14;     // Pixels to move ball
        private static final int Y_MOVE = 4;

        private static  final int TIME_DELAY = 25; // Time delay
        private Timer timer;               // Timer object

        /**
         * init method
         */
        public FootballPane() {

            // Set Background to a Dark Green
            setBackground(new Color(0, 220, 50));

            // initTimer();
            addMouseListener(new FbMouseListener());

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
        }

        @Override
        protected void paintComponent(Graphics g) {
            // Call the superclass paint method.
            super.paintComponent(g); 

            // Set the color to Brown
            g.setColor(new Color(129, 74, 25));

            // Draw the football
            g.fillOval(x, y, WIDTH, HEIGHT);

            // Draw the Goalpost
            g.setColor(Color.YELLOW);
            g.fillRect(670, 240, 5, 140);
            g.fillRect(610, 80, 5, 140);
            g.fillRect(740, 120, 5, 140);
            // Need Thicker line
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(5));
            g2.drawLine(612, 220, 742, 260);
        }

        private class TimerListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                // Update the ball's position
                y -= Y_MOVE;
                x += X_MOVE;

                // Force a call to the paint method
                repaint();
            }

        }

        public void initTimer() {
            timer = new Timer(TIME_DELAY, new TimerListener());
            timer.start();
        }

        private class FbMouseListener implements MouseListener {

            public void mouseClicked(MouseEvent e) {
                if (e.getX() >= x && e.getX() <= (x + WIDTH) && e.getY() >= y && e.getY() <= (y + HEIGHT)) {
                    initTimer();
                }
            }

            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        }

    }

}

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting