重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Scanner
宣州网站建设公司创新互联建站,宣州网站设计制作,有大型网站制作公司丰富经验。已为宣州上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的宣州做网站的公司定做!
public class abc {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("输入边长:");
double a = input.nextDouble();
double area = a*a;
double length=4*a;
System.out.println(area);
System.out.println(length);
按照你的要求,编写的Java程序如下
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class F extends JFrame implements Runnable{
Color[] colors = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.black};
int i=0;
F(){
setTitle("变色正方形");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g){
g.setColor(colors[i]);
g.drawRect(100,100,200,200);
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i7)
i++;
else
i=0;
repaint();
}
}
public static void main(String[] args) {
F f=new F();
Thread t=new Thread(f);
t.start();
}
}
运行结果
随便写写 未测试
package com.leejiliang.HomeworkDemo2;
public class Test {
public static void main(String[] args) {
Square[] squares = new Square[10];
for (int i = 0, k = squares.length; i k; i++) {
squares[i] = new Square(i + 1);
}
System.out
.println("所有Square的总周长是:" + Square.getTotalPerimeter(squares));
Square sq1 = new Square(20);
Square sq2 = null;
try {
sq2 = (Square) sq1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println("sq1"
+ (sq1.compareTo(sq2) == 1 ? ""
: (sq1.compareTo(sq2) == 0 ? "=" : "")) + "sq2");
}
}
class Square implements Cloneable, ComparableSquare {
private double side;
public Square(double s1) {
this.side = s1;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public int compareTo(Square o) {
if (this.side o.getSide()) {
return 1;
} else if (this.side o.getSide()) {
return -1;
} else {
return 0;
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public double getPerimeter() {
return side * 4;
}
public static double getTotalPerimeter(Square[] squares) {
double totalPerimeter = 0.0;
for (int i = 0, k = squares.length; i k; i++) {
totalPerimeter += squares[i].getPerimeter();
}
return totalPerimeter;
}
}
package bdzhidao;
import java.awt.*;
import javax.swing.*;
public class Square extends JFrame{
public static void main(String[] args){
JFrame frame=new JFrame("可变正方形");
Spanel jp=new Spanel();
frame.add(jp);
frame.setSize(1366,768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocation(0,0);
frame.setVisible(true);
}
}
class Spanel extends JPanel{
int x=640;
int Width=8;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(10,200,40));
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
g.drawRect(x-=20,x-=20,Width+=10,Width+=10);
if(x=0){
System.exit(0);//当正方形移到屏幕里面时关闭程序,如果没有该语句则最后屏幕会变成绿色
}
repaint();
}
}
//缩小就只需要把相关数据更改一下就OK了,希望对你有帮助!
//其实就是利用线程不断重新画图
可使用Graphics 的fillRect绘制正方形,代码如下:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Rect extends JFrame{
Rect(){
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawRect(80, 80, 50, 50);
g.fillRect(150, 150, 50, 50);
}
public static void main(String[] args) {
new Rect();
}
}
import java.awt.Graphics;
import javax.swing.JFrame;
public class aa extends JFrame {
public static void main(String[] args) {
new aa();
}
public aa() {
this.pack();
this.setTitle("画矩形");
this.setSize(300, 200);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
this.setSize(300, 200);
g.drawRect(60, 60, 50, 30);
}
}