试图找出我的图形程序无法正常工作的原因

时间:2016-01-05 21:47:14

标签: java

我无法弄清楚为什么我的图形程序的渲染功能没有显示我的矩形。另外,如果我将bufferstrategy更改为' 3'我得到了时髦的行为。目前,我的项目有两个名为Main的主要类,第二个名为UniversalJframeUniversalJfame类应该被称为显示,至少你可以把它想象为显示。请记住,我还是java编程的新手。

public class Main extends Canvas implements Runnable{

    public int w = 200;
    public int h = 200;
    public String t = "Hello";
    private boolean running = false;


    private UniversalJframe frame;
    private Thread thread;

    private BufferStrategy bs;
    private Graphics g;

    private PauseTest pause;

    public void run(){
        System.out.println("Run Method");

        while(running){
            render();
            tick();
            pause.pause();
        }

        stop();
    }

    public synchronized void  start(){
        if(running) { return; }

        System.out.println("Starting Main Program");
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running) { return; }

        System.out.println("Stopping");
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void render(){
        bs = frame.getCanvas().getBufferStrategy();
        if(bs == null){
            frame.getCanvas().createBufferStrategy(2);
            return;
        }

        g = bs.getDrawGraphics();

        //Draw Here

        g.setColor(Color.GREEN);
        g.fillRect(10, 10, 10, 10);

        //End Draw

        bs.show();
        g.dispose();

    }

    public void tick(){

    }

    public Main(){
        frame = new UniversalJframe(h, w, t, this);
        pause = new PauseTest();
        pause.setDuration(500000);
        start();
    }

    public static void main(String args[]){
        new Main(); 
        System.out.println("Running Main Program");
    }
}

public class UniversalJframe extends Canvas {

    private static final long serialVersionUID = 1L;

    private JFrame jFrame;
    private Canvas canvas;

    int height = 200; int width = 200;
    String title = "";
    Main obj;


    public UniversalJframe(int height, int width, String title, Main obj){
        this.height = height;
        this.width = width;
        this.obj = obj;
        this.title = title;

        init();
    }

    public Canvas getCanvas(){
        return canvas;
    }

    private void init(){
        jFrame = new JFrame(title);
        jFrame.setSize(new Dimension(width, height));
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jFrame.setResizable(false);
        jFrame.add(obj);
        jFrame.setLocationRelativeTo(null);

        jFrame.setVisible(true);

        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));

        jFrame.add(canvas);
        jFrame.pack();

    }

}

1 个答案:

答案 0 :(得分:0)

您正在使用Canvas,这是一个旧的AWT类。

这是一个使用Java Swing的简单图形应用程序的有趣示例。

Moving Eyes

眼球跟随绘图板周围的光标。

这是代码。您可以使用JFrame并将JPanel绘制为任何图形应用程序的基础。

package com.ggl.testing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MovingEyes implements Runnable {

    private static final int drawingWidth = 400;
    private static final int drawingHeight = 400;
    private static final int eyeballHeight = 150;
    private static final int eyeballWidthMargin = 125;
    private static final int eyeballOuterRadius = 50;
    private static final int eyeballInnerRadius = 20;

    private DrawingPanel drawingPanel;

    private Eye[] eyes;

    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MovingEyes());
    }

    public MovingEyes() {
        this.eyes = new Eye[2];
        this.eyes[0] = new Eye(new Point(eyeballWidthMargin, eyeballHeight));
        this.eyes[1] = new Eye(new Point(drawingWidth - eyeballWidthMargin,
                eyeballHeight));
    }

    @Override
    public void run() {
        frame = new JFrame("Moving Eyes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawingPanel = new DrawingPanel();
        frame.add(drawingPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = -2977860217912678180L;

        public DrawingPanel() {
            this.addMouseMotionListener(new EyeballListener());
            this.setBackground(Color.WHITE);
            this.setPreferredSize(new Dimension(drawingWidth, drawingHeight));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.BLACK);

            for (Eye eye : eyes) {
                drawCircle(g, eye.getOrigin(), eyeballOuterRadius);
                fillCircle(g, eye.getEyeballOrigin(), eyeballInnerRadius);
            }
        }

        private void drawCircle(Graphics g, Point origin, int radius) {
            g.drawOval(origin.x - radius, origin.y - radius, radius + radius,
                    radius + radius);
        }

        private void fillCircle(Graphics g, Point origin, int radius) {
            g.fillOval(origin.x - radius, origin.y - radius, radius + radius,
                    radius + radius);
        }

    }

    public class Eye {
        private final Point origin;
        private Point eyeballOrigin;

        public Eye(Point origin) {
            this.origin = origin;
            this.eyeballOrigin = origin;
        }

        public Point getEyeballOrigin() {
            return eyeballOrigin;
        }

        public void setEyeballOrigin(Point eyeballOrigin) {
            this.eyeballOrigin = eyeballOrigin;
        }

        public Point getOrigin() {
            return origin;
        }

    }

    public class EyeballListener extends MouseMotionAdapter {

        private final double eyeballDistance = eyeballOuterRadius
                - eyeballInnerRadius - 5;

        @Override
        public void mouseMoved(MouseEvent event) {
            Point p = event.getPoint();
            for (Eye eye : eyes) {
                Point origin = eye.getOrigin();
                double theta = Math.atan2((double) (p.y - origin.y),
                        (double) (p.x - origin.x));
                int x = (int) Math.round(Math.cos(theta) * eyeballDistance)
                        + origin.x;
                int y = (int) Math.round(Math.sin(theta) * eyeballDistance)
                        + origin.y;
                eye.setEyeballOrigin(new Point(x, y));
            }

            drawingPanel.repaint();
        }

    }

}