import java.util.Scanner
宣州網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,宣州網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為宣州上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的宣州做網(wǎng)站的公司定做!
public class abc {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("輸入邊長(zhǎng):");
double a = input.nextDouble();
double area = a*a;
double length=4*a;
System.out.println(area);
System.out.println(length);
按照你的要求,編寫(xiě)的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();
}
}
運(yùn)行結(jié)果
隨便寫(xiě)寫(xiě)??? 未測(cè)試
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的總周長(zhǎng)是:"?+?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);//當(dāng)正方形移到屏幕里面時(shí)關(guān)閉程序,如果沒(méi)有該語(yǔ)句則最后屏幕會(huì)變成綠色
}
repaint();
}
}
//縮小就只需要把相關(guān)數(shù)據(jù)更改一下就OK了,希望對(duì)你有幫助!
//其實(shí)就是利用線程不斷重新畫(huà)圖
可使用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("畫(huà)矩形");
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);
}
}