JAVA :: MVC
MyJPanel .java
1 package com.day03; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 7 import javax.swing.JPanel; 8 9 import com.day03.vo.*; 10 11 public class MyJPanel extends JPanel { 12 // @Override 13 private DataWork dw; 14 15 public MyJPanel() { 16 setBackground(Color.BLACK); 17 dw = new DataWork(this); 18 dw.start(); 19 } 20 21 public void paint(Graphics g) { 22 23 super.paint(g); 24 showStar(g); 25 showAllStar(g); 26 showYue(g); 27 28 } 29 30 public void showYue(Graphics g) { 31 32 // Yue yue = dw.get 33 // g.setColor(new Color(yue.getR(),star.getB(),star.getG())); 34 // g.fillOval(100, 100, 100, 100); 35 36 // 320566303 37 } 38 39 public void showAllStar(Graphics g) { 40 Star[] stars = dw.getStars(); 41 for (Star star : stars) { 42 g.setColor(new Color(star.getR(), star.getB(), star.getG())); 43 g.setFont(new Font(star.getFontType(), star.getFontBoln(), star 44 .getFontSize())); 45 g.drawString(star.getStarname(), star.getX(), star.getY()); 46 } 47 } 48 49 public void showStar(Graphics g) { 50 Star star = dw.getStar(); 51 g.setColor(new Color(star.getR(), star.getB(), star.getG())); 52 g.setFont(new Font(star.getFontType(), star.getFontBoln(), star 53 .getFontSize())); 54 g.drawString(star.getStarname(), star.getX(), star.getY()); 55 56 } 57 58 }
MyFrame
1 package com.day03; 2 3 import javax.swing.JFrame; 4 import javax.swing.JPanel; 5 6 public class MyFrame extends JFrame { 7 8 private int w; 9 private int h; 10 private JPanel jPanel; 11 12 public MyFrame() { 13 this.w = 400; 14 this.h = 600; 15 jPanel = new MyJPanel(); 16 17 } 18 19 public void showFrame() { 20 setDefaultCloseOperation(3); 21 setSize(w, h); 22 setTitle("Levi->python->C->objective c"); 23 add(jPanel); 24 setVisible(true); 25 26 } 27 28 public static void main(String[] args) { 29 MyFrame mf = new MyFrame(); 30 mf.showFrame(); 31 32 } 33 }
Star.java
1 package com.day03.vo; 2 3 import java.awt.Font; 4 import java.util.Random; 5 6 public class Star { 7 private int r; 8 private int b; 9 private int g; 10 private String fontType; 11 private int fontBoln; 12 private int fontSize; 13 private final String starname = "*"; 14 private int x; 15 private int y; 16 17 public Star() { 18 // r=(int) (Math.random()*256);//0-1 double type number 19 Random rd = new Random(); 20 r = rd.nextInt(256); 21 b = rd.nextInt(256); 22 g = rd.nextInt(256); 23 fontType = "华文琥珀"; 24 fontBoln = 1; 25 fontSize = 24; 26 x = rd.nextInt(400); 27 y = rd.nextInt(600); 28 29 } 30 31 public void update() { 32 Random rd = new Random(); 33 r = rd.nextInt(256); 34 b = rd.nextInt(256); 35 g = rd.nextInt(256); 36 } 37 38 public int getR() { 39 return r; 40 } 41 42 public void setR(int r) { 43 this.r = r; 44 } 45 46 public int getB() { 47 return b; 48 } 49 50 public void setB(int b) { 51 this.b = b; 52 } 53 54 public int getG() { 55 return g; 56 } 57 58 public void setG(int g) { 59 this.g = g; 60 } 61 62 public String getFontType() { 63 return fontType; 64 } 65 66 public void setFontType(String fontType) { 67 this.fontType = fontType; 68 } 69 70 public int getFontBoln() { 71 return fontBoln; 72 } 73 74 public void setFontBoln(int fontBoln) { 75 this.fontBoln = fontBoln; 76 } 77 78 public int getFontSize() { 79 return fontSize; 80 } 81 82 public void setFontSize(int fontSize) { 83 this.fontSize = fontSize; 84 } 85 86 public int getX() { 87 return x; 88 } 89 90 public void setX(int x) { 91 this.x = x; 92 } 93 94 public int getY() { 95 return y; 96 } 97 98 public void setY(int y) { 99 this.y = y; 100 } 101 102 public String getStarname() { 103 return starname; 104 } 105 106 }
DateWork.java
1 package com.day03.vo; 2 3 import javax.swing.JPanel; 4 5 public class DataWork extends Thread { 6 private Star[] stars = new Star[100]; 7 private Star star; 8 private JPanel jPanel; 9 10 public DataWork(JPanel jPanel) { 11 int i; 12 for (i = 0; i < stars.length; ++i) { 13 stars[i] = new Star(); 14 } 15 16 star = new Star(); 17 this.jPanel = jPanel; 18 } 19 20 public Star[] getStars() { 21 return stars; 22 } 23 24 public JPanel getjPanel() { 25 return jPanel; 26 } 27 28 public void run() { 29 while (true) { 30 star.update(); 31 32 for (Star s : stars) { 33 s.update(); 34 } 35 36 // for(int i=0;i<stars.length;++i) 37 // stars[i].update(); 38 try { 39 sleep(10); 40 } catch (InterruptedException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 jPanel.repaint(); 45 46 } 47 } 48 49 public Star getStar() { 50 return star; 51 } 52 53 public void setStar(Star star) { 54 this.star = star; 55 } 56 }