代码改变世界

Programmingbydoing

2019-08-15 06:47  钱先生  阅读(2625)  评论(0编辑  收藏  举报

http://www.programmingbydoing.com/

 

1. Modulus Animation

 public static void modulusAnimation() throws InterruptedException {
        int repeats = 5;
        int steps_per_seconds = 10;
        for(int i=0; i<repeats*11; i++){
            if(i%11==0){
                System.out.println(",oOo.....");
            } else if (i%11==1){
                System.out.println("..oOo....");
            }else if (i%11==2){
                System.out.println("...oOo...");
            }else if (i%11==3){
                System.out.println("....oOo..");
            }else if (i%11==4){
                System.out.println(".....oOo.");
            }else if (i%11==5){
                System.out.println("......oOo");
            }else if (i%11==6){
                System.out.println(".........oO");
            }else if (i%11==7){
                System.out.println("o.......o");
            }else if (i%11==8){
                System.out.println("Oo.......");
            }else if (i%11==9){
                System.out.println("oOo......");
            }else if (i%11==10){
                System.out.println(".oOo.....");
            }else if (i%11==11){
                System.out.println("..oOo....");
            }
            
            Thread.sleep(1000/steps_per_seconds);
        }
    }
View Code

 

2.Using swing for input

 public static void inputBox(){
        String name = JOptionPane.showInputDialog("What's your name?");
        String input = JOptionPane.showInputDialog("How old are you");
        int age = Integer.parseInt(input);

        System.out.println("Hello, " + name);
        System.out.println("Next year, you will be " + (age+1));
        System.exit(0);
    }
View Code

 

3. A frame with a Panel with writing on it

import javax.swing.*;
import java.awt.*;


public class Prog613 {
    public static void main(String[] args) {
        Frame613 f = new Frame613();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

class Frame613 extends JFrame {
    public Frame613(){
        setTitle("613 rocks");
        setSize(300,200);
        setLocation(100,200);
        
        Panel613 panel = new Panel613();
        Container cp = getContentPane();
        cp.add(panel);
    }
}

class Panel613 extends JPanel{ 
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString("Hi, ",75, 100);
    }
    
}
View Code

 

4. Graphics Demo1

import javax.swing.*;
import java.awt.*;

public class GraphicsDemo1 extends Canvas {
    public static void main(String[] args) {
        JFrame win = new JFrame("GraphicsDemo1");
        win.setSize(600,800);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GraphicsDemo1 canvas = new GraphicsDemo1();
        win.add(canvas);
        win.setVisible(true);
    }
    
    
    public void paint(Graphics g){
        g.setColor(Color.green);
        g.drawRect(50,20,100,200);
        g.fillOval(160,20,100,200);
        g.setColor(Color.blue);
        g.fillRect(200,400,200,20);
        g.drawOval(200,430,200,100);
        
        
        g.setColor(Color.black);
        g.drawString("Graphics are pretty neat. ",500,100);
        int x = getWidth()/2;
        int y = getHeight()/2;
        g.drawString("The first letter of this string is at (" + x + ","+y+")",x,y);
        
    }
    
}
View Code