java代码画一个小球,java小球运动代码
关于类的,写一个球的体积的java代码
import java.util.Scanner;
成都创新互联专业为企业提供聂拉木网站建设、聂拉木做网站、聂拉木网站设计、聂拉木网站制作等企业网站建设、网页设计与制作、聂拉木企业网站模板建站服务,十多年聂拉木做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
/**
* 计算球的体积
*
* @author young
*
*/
public class Volume {
public static void main(String[] args) {
System.out.print("请输入r:");
Scanner reader = new Scanner(System.in);
double r = 0, v = 0;
r = reader.nextDouble();
v = 4 * 3.14159 / 3 * r * r * r;
System.out.println("球体积为:" + String.format("%.2f", v));
}
}
滚动的小球 java源代码
//Checkers.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Checkers类
public class Checkers extends JFrame implements ActionListener {
//变量定义
CheckersPanel checkers = new CheckersPanel();
JButton startButton = new JButton("start");
JButton stopButton = new JButton("stop");
//构造函数
public Checkers(){
super("Checkers");
setSize(210,170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout border = new BorderLayout();
pane.setLayout(border);
pane.add(checkers,"Center");
JPanel buttonPanel = new JPanel();
startButton.addActionListener(this);
buttonPanel.add(startButton);
stopButton.addActionListener(this);
stopButton.setEnabled(false);
buttonPanel.add(stopButton);
pane.add(buttonPanel,"South");
setContentPane(pane);
show();
}
//响应用户动作
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == startButton){
checkers.playAnimation();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}else{
checkers.stopAnimation();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
//主函数
public static void main(String[] arguments){
Checkers ck = new Checkers();
}
}
//CheckersPanel类
class CheckersPanel extends JPanel implements Runnable{
//变量定义
private Thread runner;
int xPos = 5;
int xMove = 4;
//播放动画
void playAnimation(){
if (runner ==null);{
runner = new Thread(this);
runner.start();
}
}
//停止动画
void stopAnimation(){
if (runner !=null);{
runner = null;
}
}
//运行
public void run(){
Thread thisThread = Thread.currentThread();
while(runner ==thisThread){
xPos += xMove;
if ((xPos 105)|(xPos 5))
xMove *= -1;
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
//画图形
public void paintComponent(Graphics comp){
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setColor(Color.blue);
comp2D.fillRect(0,0,100,100);
comp2D.setColor(Color.white);
comp2D.fillRect(100,0,100,100);
comp2D.setColor(Color.black);
comp2D.fillOval(xPos,5,90,90);
}
}
在Java中如何用程序画一个圆
使用java画圆要用到绘图类Graphics,下面是实例代码和运行效果:
package com.dikea.demo01;
import java.awt.*;
import javax.swing.*;
// java绘图原理
public class demo_01 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
demo_01 demo01 = new demo_01();
}
public demo_01(){
mp = new MyPanel();
this.add(mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
// 定义一个MyPanel面板,用于绘图区域
class MyPanel extends JPanel{
//覆盖JPanel
// Graphics 是绘图的重要类,可以理解成一支画笔
public void paint(Graphics g){
// 1. 调用父类函数完成初始化任务
// 这句话不可以少
super.paint(g);
// 先画出一个圆圈
g.drawOval(100, 100, 30, 30);
}
}
代码复制进ide编程工具,运行效果如下:
[急]求java小程序:绘制一个小球模拟平抛运动
;
可以这样写:
import javax.swing.*;
import java.awt.*;
class C extends JFrame {
C() {
final Point point = new Point();
add(new JPanel() {
public void paintComponent(Graphics g) {
g.fillOval(point.x, point.y, 20, 20);
point.x = (point.x + 2) % getWidth();
point.y = (point.x * point.x) / 1000; // 造就弧线的简单二次方程。
}
});
setSize(777, 666);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
while (true) {
repaint(point.x, point.y, 50, 50);
try { Thread.sleep(10); } catch (Exception ex) {}
}
}
public static void main(String[] args) {
new C();
}
}
JAVA 编辑 小球在一个框里做连续的碰撞运动
/*
* 一个在窗体中来回运动的圆.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 a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3uhPWrjRdmHTdmWR4nWn40ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHRYnHmYnHRYPW6knH63P1nz" target="_blank" class="baidu-highlight"java.awt.Color/a;
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 (cxrx-r || cxrx+r) {
dx = -dx;
}
if (c1xr1x-r1 || c1xr1x+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;i8;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小球运动代码
文章起源:http://azwzsj.com/article/hcjjid.html