LineArt.java(array)

// LineArt.java

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

class LineArt2 {
    public static void main(String[] args) {
    JFrame frame = new JFrame();

    frame.setTitle("Line Art2");
    frame.setSize(640, 480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MyPanel panel = new MyPanel();
    frame.getContentPane().add(panel);

    frame.setVisible(true);
    }
}

class MyPanel extends JPanel implements Runnable {
   
    Ball [] ballA = new Ball[10];
    Ball [] ballB = new Ball[10];
   
    int xa1 = 100;
    int xa2 = 150;
    int ya1 = 100;
    int ya2 = 125;
    int va1 = 10;
    int va2 = 5;
    int vb1 = 5;
    int vb2 = 5;
       
    public MyPanel() {
    setBackground(Color.white);


    for(int i=0; i<10; i++){
        ballA[i]=new Ball(xa1+va1*i,ya1+va2*i,va1,va2,0,0,630,450);
        ballB[i]=new Ball(xa2+vb1*i,vb1+vb2*i,vb1,vb2,0,0,630,450);
    }



/*    ball1 = new Ball(100,100,10,5,0,0,630,450);//va
    ball2 = new Ball(110,105,10,5,0,0,630,450);
    ball3 = new Ball(120,110,10,5,0,0,630,450);
    ball4 = new Ball(130,115,10,5,0,0,630,450);
    ball5 = new Ball(140,120,10,5,0,0,630,450);
   
    ball6 = new Ball(150,125,5,5,0,0,630,450);//vb
    ball7 = new Ball(155,130,5,5,0,0,630,450);
    ball8 = new Ball(160,135,5,5,0,0,630,450);
    ball9 = new Ball(165,140,5,5,0,0,630,450);
    ball10 = new Ball(170,145,5,5,0,0,630,450);
  */ 
    Thread refresh = new Thread(this);
    refresh.start();
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
   
   


            //g.fillOval(ball1.getX(), ball1.getY(), 10, 10 );

                g.setColor(Color.red);
               
                for(int i=0;i<10;i++){
                    ballA[i].forward();
                    ballB[i].forward();
                    g.drawLine(ballA[i].getX(), ballA[i].getY(), ballB[i].getX(), ballB[i].getY());
                }
                                                           
       
    }

    public void run() {
    while(true) {
        repaint();
        try {
        Thread.sleep(10);
        //Thread.sleep(50);
        }
        catch(Exception e) {
        }
    }
    }
}

class Ball {
    private int x;
    private int y;
    private int vx;
    private int vy;
    private int left;
    private int right;
    private int top;
    private int bottom;

    public Ball(int x, int y, int vx, int vy, int l, int t, int r, int b) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    right = r;
    left = l;
    top = t;
    bottom = b;
    }

    public void forward() {
    x = x + vx;
    y = y + vy;

    if (x < left || x > right) {
        vx = -vx;
    }
    if (y < top || y > bottom) {
        vy = -vy;
    }
    }

    public int getX() {
    return x;
    }

    public void setX(int x) {
    this.x = x;
    }

    public int getY() {
    return y;
    }

    public void setY(int y) {
    this.y = y;
    }
}

 

 

 

 

 

 

 

最終更新:2013年01月30日 15:00
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。