标题:[求助]JAVA高级问题!!----怕就不要来啦!
只看楼主
main
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2004-6-17
 问题点数:0 回复次数:1 
[求助]JAVA高级问题!!----怕就不要来啦!

JAVa问题???

<画一个正方型和一个圆

圆在那个正方形中运动

而且要设置那个圆和那个正方形的色彩??????>

搜索更多相关主题的帖子: JAVA 高级 
2004-12-23 13:16
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
得分:0 

呵呵,我写了一个,楼主可以参考一下,其实这种题目不难的。

/** * This is a simple Java2D program. * @author jellen */

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

class Ball { private int x = (int)(390 * Math.random()) + 21; private int y = (int)(390 * Math.random()) + 21; private static final int w = 30; private static final int h = 30; private int dx = 3; private int dy = 3; public void draw(Graphics2D g) { g.fill(new Ellipse2D.Double(x, y, w, h)); }

public void move() { x += dx; y += dy;

if(x < 20) { x = 20; dx = -dx; }

if(y < 20) { y = 20; dy = -dy; }

if(x + w >= 420) { x = 420 - w; dx = -dx; }

if(y + h >= 420) { y = 420 - h; dy = -dy; } } }

class MyPanel extends JPanel implements Runnable { private Rectangle2D rect = new Rectangle2D.Double(20, 20, 400, 400); private Ball ball = new Ball();

public MyPanel() { Thread t = new Thread(this); t.start(); } public void paintComponent(Graphics g) { super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.BLUE); g2.fill(rect);

g2.setColor(Color.RED); ball.draw(g2); }

public void run() { while(true) { try { ball.move(); Thread.sleep(5); repaint(); } catch(InterruptedException e) { } } } }

public class Move { public static void main(String[] args) { JFrame me = new JFrame("Move Test"); me.setSize(450, 470); me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); me.setResizable(false); me.getContentPane().add(new MyPanel()); me.setVisible(true); } }


再见,理想!
2004-12-23 15:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-9919-1-1.html




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

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