package bdzhidao;
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供新建網(wǎng)站建設(shè)、新建做網(wǎng)站、新建網(wǎng)站設(shè)計(jì)、新建網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、新建企業(yè)網(wǎng)站模板建站服務(wù),十年新建做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
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)閉程序,如果沒有該語句則最后屏幕會變成綠色
}
repaint();
}
}
//縮小就只需要把相關(guān)數(shù)據(jù)更改一下就OK了,希望對你有幫助!
//其實(shí)就是利用線程不斷重新畫圖
可使用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.io.BufferedReader;IMPORT java.io.InputStreamReader;public class Square { public static void main(String[] args) { int r; try {BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("請輸入要打印的行數(shù):"); String str=in.readLine(); r=Integer.valueOf(str).intValue(); } catch(Exception e) { System.out.println("您輸入的數(shù)據(jù)有誤。"); return; } for (int i=1;i=5;i++) { for(int j=1;j=5;j++) { System.out.print("*"); } System.out.println(); } }}
按照你的要求,編寫的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é)果
按照你的要求編寫的輸出菱形和正方形的程序如下:
public class U {
public static void main(String[] args) {
//設(shè)置菱形和正方形大小
final int N=3;
//輸出菱形
for (int i = 1; i = 2*N-1; i++) {
for (int j = 1; j = Math.abs(N - i); j++) {
System.out.print(" ");
}
for (int k = 1; k = N-Math.abs(i-N); k++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
//輸出正方形
for (int i = 1; i = N; i++) {
for (int j = 1; j = N; j++) {
if(j==1 || j==N || i==1 || i==N)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
運(yùn)行結(jié)果:
*
* *
* * *
* *
*
***
* *
***
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("請輸入正方形的大?。?);
int line = s.nextInt();; // 正方形的邊長
for (int i = 1; i = line; i++) { // 行
for (int j = 1; j = line; j++) { // 列
if (i == 1 || i == line) { // 如果是第一行或者是最后一行則輸出星號
System.out.print("* ");
} else if (j == 1 || j == line) { // 如果是第一列或者是最后一列則輸出星號
System.out.print("* ");
} else {
System.out.print(" "); // 如果不是第一行、最后一行、第一列、最后一列則輸出空格
}
}
System.out.print("\n"); // 換行
}
}