java程序:编写一个程序,让一个小球在JFrame中滚动,当碰边缘时则选择一个角度返回.

JAVA 编程题~

第三题的答案

package image;

import java.awt.EventQueue;

public class Test {

private JFrame frame;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Test() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 466);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.setForeground(Color.BLACK);
panel.setBounds(10, 10, 414, 138);
frame.getContentPane().add(panel);

JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);

JButton btnNewButton = new JButton("New button");
panel.add(btnNewButton);

JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GREEN);
panel_1.setBounds(10, 158, 414, 259);
frame.getContentPane().add(panel_1);

JButton btnNewButton_3 = new JButton("New button");
panel_1.add(btnNewButton_3);

JButton btnNewButton_2 = new JButton("New button");
panel_1.add(btnNewButton_2);
}
}

import java.awt.Frame;import java.awt.Graphics;import java.awt.Panel;public class Demo {public static void main(String[] args) {Frame w = new Frame();w.setSize(300, 400);CPanel mp = new CPanel();w.add(mp);w.setVisible(true);Thread t = new Thread(mp);t.start();}}class CPanel extends Panel implements Runnable {private static final long serialVersionUID = 3474337559197220434L;int x = 30;int y = 30;boolean down = true;public void paint(Graphics g) {g.fillOval(x, y, 20, 20);}public void run() {try {while (true) {if (down) {y++;if (y >= 340) {down = false;}} else {y--;if (y <= 20) {down = true;}}repaint();Thread.sleep(1);}} catch (InterruptedException e) {e.printStackTrace();}}}

05年写的,你修改一下吧

/*
* 一个在窗体中来回运动的圆.java
*
* Created on 2005年10月5日, 下午1:02
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

package javaapplication1;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.applet.*;
import java.awt.*;
import java.math.*;
/**
*
* @author Bachelorlrz
*/
public class 在窗体中来回运动的圆 extends java.applet.Applet implements java.lang.Runnable {
int cx,cy,c1x,c1y; //圆的坐标
int cw,ch; //圆的宽高
int bx,by,bw,bh; //背景的坐标和宽高
int dx,dx1; //圆的运动
int r,rx,ry; //圆的半径和位置
int r1,r1x,r1y;
Thread u_thread;

/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
rx=150; ry=160;
bw=500; bh=400;
r1x=bw/2-cw*2; r1y=bh/2-ch;
r=60; r1=200;
cx =rx; cy =ry;
c1x=r1x; c1y=r1y;
cw=30; ch=30;
bx=2; by=2;
dx=1; dx1=2;
// TODO start asynchronous download of heavy resources
}

// TODO overwrite start(), stop() and destroy() methods

public void update(java.awt.Graphics g) {

super.paint(g);
g.setColor(Color.red);
g.drawRect(bx,by,bw,bh);
g.setColor(Color.BLACK);
g.fillRect(bx+2,by+2,bw-4,bh-4);
g.drawString("在窗体中来回运动的圆", bw/2-60, bh/2);
if (cx<rx-r || cx>rx+r) {
dx = -dx;
}
if (c1x<r1x-r1 || c1x>r1x+r1) {
dx1 = -dx1;
}

cx =cx+dx;
cy =(int)(dx*Math.sqrt(r*r-(cx-rx)*(cx-rx)))+ry;
c1x =c1x+dx1;
c1y =(int)(dx1/2*Math.sqrt(r1*r1-(c1x-r1x)*(c1x-r1x))/2);

// g.drawArc(cx, cy, cw, ch, 0, 360);

for(int i=0;i<8;i++){
if (i%5 == 0){
g.setColor(Color.black);
}else if ( i%5== 1) {
g.setColor(Color.GREEN);
}else if(i%5==2){
g.setColor(Color.RED);
}else if( i%5 ==3){
g.setColor(Color.pink);
}else {
g.setColor(Color.orange);
}
g.drawLine(bx,by, cx+10,cy+i+10);
g.drawLine(bx+bw,by+bh,cx+10,cy+i+10);
g.drawLine(bx+bw,by, cx+10,cy+i+10);
g.drawLine(bx,by+bh, cx+10,cy+i+10);

g.drawLine(bx,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by+bh,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx+bw,by,-cx+bw-bx-cw+10,cy+i+10);
g.drawLine(bx,by+bh,-cx+bw-bx-cw+10,cy+i+10);

g.drawLine(bx,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,c1x+10,c1y+r1y+i+10);
g.drawLine(bx+bw,by, c1x+10,c1y+r1y+i+10);
g.drawLine(bx,by+bh, c1x+10,c1y+r1y+i+10);

g.drawLine(bx,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx+bw,by, r1x+r1+cw-c1x+10,-c1y+r1y+i+10);
g.drawLine(bx,by+bh,r1x+r1+cw-c1x+10,-c1y+r1y+i+10);

g.drawArc(cx+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(-cx+bw-bx-cw+i, cy+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(c1x+i, c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
g.drawArc(r1x+r1+cw-c1x+i, -c1y+r1y+i, cw-i*2, ch-i*2, 0, 360);
}
}
public void start(){
if (u_thread == null)
{
u_thread = new Thread(this);
u_thread.start();
}
}
public void run() {
while(true){
repaint();
try{
u_thread.sleep(10);
}
catch (InterruptedException e){
return;
}
}
}
}

。使用JAVA编写一个程序求出一个整数的所有因子的和
答:以下是一个使用Java编写的程序,用于求出一个整数的所有因子的和:在这个程序中,首先从用户那里获取一个整数输入。然后,使用一个循环来遍历从1到该整数的所有整数。对于每个整数,如果它是该整数的因子(即该整数除以该整数的余数为0),则将该整数加到总和中。最后,输出所有因子的和。例如,如果用户...

java编写一个奶茶点单程序代码怎么写?
答:import java.util.Scanner;public class MilkTeaOrder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("欢迎来到奶茶店!");System.out.println("请选择您的口味:");System.out.println("1. 原味奶茶");System.out.println("2. 珍珠...

(用JAVA语言)编写一个程序用选择法对数组a[]={20,10,50,40,30,70,60...
答:public class Demo { public static void main(String args[]) { int arr[] = {20,10,50,40,30,70,60,80,90,100};System.out.println("数组由大到小排序前:");for(int num : arr) { System.out.print(num + "、");} //使用冒泡排序法,将数组由大到小进行排序 for(int i = ...

用Java编写一个程序,要求如下:
答:1.内部使用 C 的 longjmp 机制让出一个协程。 因此,如果一个 C 函数 foo 调用了一个 API 函数, 而这个 API 函数让出了(直接或间接调用了让出函数)。 由于 longjmp 会移除 C 栈的栈帧, Lua 就无法返回到 foo 里了。2.为了回避这类问题, 碰到 API 调用中调用让出时,除了那些抛出错误...

编写一个JAVA程序读取0到50(包含两者)范围内的任意多个整数,并且计算每...
答:public class Test { public static void main(String[] args) { Map<Integer, Integer> map = new HashMap<>(); Scanner scanner = new Scanner(System.in); System.out.println("请输入0到50范围的数(输入-1结束)"); int num; // 输入 -1 则输入结束 while ((...

编写java程序
答:import java.util.*;public class Test{ public static void main(String[] args){ Scanner scan=new Scanner(System.in); System.out.println("请输入一个整数"); int x=scan.nextInt(); int y=0; if(x<0) y=-1+2*x; else if(x==0) y=-1; else...

编写一个Java应用程序,该程序中有2个类:Trangle、和Circle,分别用来描述...
答:Circle {private double radius;private double perimeter;private double area;public Circle(double radius) {this.radius = radius;this.perimeter = 2 * Math.PI * radius;this.area = Math.PI * radius * radius;}public double getRadius() {return radius;}public double getPerimeter(...

输入5种水果的英文名称,用java编写一个程序,输出这些水果名称按照字典出 ...
答:import java.util.Arrays;import java.util.Scanner;public class CompareTest {/** * 一、思路: * 1、建立一个String数组,用来存储用户输入的水果名字。 * 2、按字典出现的先后顺序即比较字符串大小。可以使用到String对象的compareTo()方法。 * 3、对比大小后,升序输出。 * 二、实现: * 1、...

Java编写一个程序,在屏幕上打印出如下图形?
答:打印杨辉三角图形的Java程序如下 public class H{ public static void main(String[] args) { int i,j,N=7;int[][] a=new int[N][N];for(i=0;i<N;i++){ a[i][0]=1;a[i][i]=1;} for(i=2;i<N;i++)for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j]...

求解JAVA编程题:编写一个程序选择法对数组a[]={20,10,50,40,30,70,6...
答:要交换的第一个位置。param j 要交换的第二个位置。/ public static void swap(final int[] ints, final int i, final int j){ int count = ints.length;// 数组的长度 // 如果数组的长度小于等于1、i与j相等、i或j比数组的长度大或小于0,则没有交换的意义,直接返回。if ((count <...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网