如何创建Graphics对象以在JFrame上绘制形状

时间:2016-01-14 12:00:58

标签: java swing graphics graphics2d

我想制作一个程序来制作一个射弹(一个在2D射弹中飞行的球)。在main中我调用一个Shoot()函数,其参数是一个Graphics对象,但我不知道如何创建对象以便它在我的JFrame对象上绘制。请帮帮我。

import java.lang.Math;
import java.applet.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;

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

public class Projectile extends JPanel{
    public static int ScreenH=1500;
    public static int ScreenW=3000;
    public static int Xcor=0;
    public static int Ycor=0;
    public static int ballRadius=20;
    public static int prevXcor = 0;
    public static int prevYcor = 0;
    public static int newXcor = 0;
    public static int newYcor = 0;
    public static int Time = 0;
    public static int Angle = 45;
    public static int Velocity = 10;
    public static double Acceleration = 9.8;

    public static void InitGraphics(){
        JFrame jframe = new JFrame();
        jframe.setTitle("Projectile");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(ScreenW, ScreenH);
        jframe.setResizable(false);
        jframe.setVisible(true);
        jframe.add(new Projectile());
    }

    public static void drawCenteredCircle(Graphics g, int x, int y) {
          int a = x;
          int b = 1000 - y;
          g.fillOval(a,b, ballRadius, ballRadius);
        }

    public static void move() throws InterruptedException {
        Thread.sleep(20);
        Time += 20;
        double XVelocity = Math.sin(Velocity);
        double YVelocity = Math.cos(Velocity);
        int X = (int) (XVelocity*Time);
        int Y = (int) ((YVelocity*Time) + (0.5*Acceleration*Time*Time));
        newXcor = X;
        newYcor = Y;
    }

    public static void repaint(Graphics g) {
        g.setColor(Color.white);
        drawCenteredCircle(g, prevXcor, prevYcor);

        g.setColor(Color.red);
        drawCenteredCircle(g, newXcor, newYcor);

        prevXcor = newXcor;
        prevYcor = newYcor;
    }

    public static void Shoot(Graphics g) throws InterruptedException {
        while ( (newXcor < (ScreenW - (4*ballRadius))) && (newYcor < (ScreenH - (4*ballRadius)))) {
            move();
            repaint(g);
        }
    }


    public static void main(String[]args) {
        InitGraphics();
        Shoot();
    }
}

1 个答案:

答案 0 :(得分:0)

JFrame就是窗口,你需要为它添加一个JComponent。 JComponents包含一个protected void paintComponent(Graphics g)方法,您需要覆盖它,如下所示:

    JFrame frame = new JFrame();
    JComponent canvas = new JComponent() {
        protected void paintComponent(Graphics g) {
            //call repaint(g) here instead of this
            g.setColor(Color.RED);
            g.fillRect(0, 0, getWidth(), getHeight());
        };
    };
    frame.add(canvas);

您可能需要清除重绘中的背景