| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2379 人关注过本帖
标题:java中fillrect填充图形为什么无法显示
只看楼主 加入收藏
kaosaier
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:124
专家分:303
注 册:2016-7-23
结帖率:88.24%
收藏
 问题点数:0 回复次数:0 
java中fillrect填充图形为什么无法显示
程序代码:
/**

 * Created by Kaosaier on 7/3/2017.

 */

import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PongBall {
    private final int TABLE_WIDTH = 300;
    private final int TABLE_HEIGHT = 400;
    private final int RACKET_Y = 340;
    private final int RACKET_HEIGHT = 20;
    private final int RACKET_WIDTH = 80;
    private final int BALL_SIZE = 16;
    private final int BWIDTH = 100;
    private final int BHEIGHT = 20;
    private final int B1y = 400;
    private final int B1x = 0;

    private Frame f = new Frame("PongBall");
    Random rand = new Random();
    private int ySpeed = 10;
    private double xyRate = rand.nextDouble() - 0.5;
    private int xSpeed = (int)(ySpeed * xyRate * 2);
    private int ballX = rand.nextInt(200) + 20;
    private int ballY = rand.nextInt(10) + 20;
    private int racketX = rand.nextInt(200);
    private MyCanvas tableArea = new MyCanvas();
    Timer timer;
    private boolean isLose = false;
    public void init()
    {
        tableArea.setPreferredSize(new Dimension(TABLE_WIDTH , TABLE_HEIGHT));
        f.add(tableArea);
        KeyAdapter keyProcessor = new KeyAdapter()
        {
            public void keyPressed(KeyEvent ke)
            {
                if (ke.getKeyCode() == KeyEvent.VK_LEFT)
                {
                    if (racketX > 0)
                        racketX -= 10;
                }
                if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
                {
                    if (racketX < TABLE_WIDTH - RACKET_WIDTH)
                        racketX += 10;
                }
            }
        };
        f.addKeyListener(keyProcessor);
        tableArea.addKeyListener(keyProcessor);
        ActionListener taskPerformer = evt ->
        {
            if (ballX <= 0 || ballX == TABLE_WIDTH - BALL_SIZE)
            {
                xSpeed = -xSpeed;
            }
            if (ballY >= RACKET_Y - BALL_SIZE + 5)
            {
                timer.stop();
                isLose = true;
                tableArea.repaint();
            }
            else if (ballY  <= 0 || ballY >= RACKET_Y - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)
            {
                ySpeed = -ySpeed;
            }
            ballY += ySpeed;
            ballX += xSpeed;
            tableArea.repaint();
        };
        timer = new Timer(60, taskPerformer);
        timer.start();
        f.pack();
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        f.setVisible(true);
    }
    public static void main(String[] args)
    {
        new PongBall().init();
    }
    class MyCanvas extends Canvas
    {
        public void paint(Graphics g)
        {
            if (isLose)
            {
                g.setColor(new Color(255, 0, 0));
                g.drawString("game over:-(" , 75 ,200);
            }
            else
            {
                g.setColor(new Color(0 , 2 , 225));
                g.fillRect(0 , 400 , BWIDTH , BHEIGHT);
                g.setColor(new Color(241, 255, 0));
                g.fillRect(0 , 0 , 300 , 400);
                g.setColor(new Color(0, 0, 0));
                g.fillOval(ballX , ballY , BALL_SIZE, BALL_SIZE);
                g.setColor(new Color(165, 163, 200));
                g.fillRect(racketX , RACKET_Y, RACKET_WIDTH , RACKET_HEIGHT);
            }
        }
    }
}

最后的几个画的地方g.setColor(new Color(0 , 2 , 225));不能运行,没出错误,只是画不出来,看不到
                g.fillRect(0 , 400 , BWIDTH , BHEIGHT);
搜索更多相关主题的帖子: java private int new Color 
2017-07-03 19:38
快速回复:java中fillrect填充图形为什么无法显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017703 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved