Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. public class UsoThreads {
  2.  
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5.  
  6. Marco miMarco=new Marco();
  7. miMarco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8. miMarco.setVisible(true);
  9.  
  10. }
  11.  
  12. public Marco() {
  13. setBounds(400,300,600,400);
  14. setTitle("Rebota Pelota");
  15. LaminaPelota miLamina=new LaminaPelota();
  16. add(miLamina,BorderLayout.CENTER);
  17. LaminaBotones milamina2=new LaminaBotones();
  18. add(milamina2,BorderLayout.SOUTH);
  19. }
  20.  
  21. private double x=10;
  22. private double y=10;
  23. private static final int TAMX=15;
  24. private static final int TAMY=15;
  25. private double speedX=1;
  26. private double speedY=1;
  27.  
  28. public void mover(Rectangle2D limite) {
  29. x+=speedX;
  30. y+=speedY;
  31.  
  32. if(x<limite.getMinX()) {
  33. x=limite.getMinX();
  34. speedX=-speedX;
  35. }
  36.  
  37. if(x+TAMX>limite.getMaxX()) {
  38. x=limite.getMaxX()-TAMX;
  39. speedX=-speedX;
  40. }
  41.  
  42. if(y<limite.getMinY()) {
  43. y=limite.getMinY();
  44. speedY=-speedY;
  45. }
  46.  
  47. if(y+TAMY>limite.getMaxY()) {
  48. y=limite.getMaxY()-TAMY;
  49. speedY=-speedY;
  50. }
  51.  
  52. }
  53.  
  54. public Ellipse2D dibujarPelota() {
  55. return new Ellipse2D.Double(x,y,TAMX,TAMY);
  56. }
  57.  
  58. private ArrayList<Pelota>arrayPelota=new ArrayList<Pelota>();
  59.  
  60. public void agregarPelota(Pelota b) {
  61. arrayPelota.add(b);
  62. }
  63.  
  64. public void paintComponent(Graphics g){
  65. super.paintComponent(g);
  66. Graphics2D g2=(Graphics2D) g;
  67.  
  68. for(Pelota e:arrayPelota) {
  69. g2.fill(e.dibujarPelota());
  70. }
  71.  
  72.  
  73. }
  74.  
  75. public LaminaBotones() {
  76. JButton dale=new JButton("Dale");
  77. JButton salir=new JButton("Salir");
  78. dale.addActionListener(new ActionListener() {
  79.  
  80. @Override
  81. public void actionPerformed(ActionEvent e) {
  82. // TODO Auto-generated method stub
  83.  
  84. comienza();
  85. }
  86.  
  87. });
  88.  
  89. salir.addActionListener(new ActionListener() {
  90.  
  91. @Override
  92. public void actionPerformed(ActionEvent e) {
  93. // TODO Auto-generated method stub
  94. System.exit(0);
  95. }
  96.  
  97.  
  98. });
  99. add(dale);
  100. add(salir);
  101. }
  102.  
  103. public void comienza() {
  104. Pelota pelota=new Pelota();
  105. lamina.agregarPelota(pelota);
  106. for(int i=1;i<=3000;i++) {
  107. pelota.mover(lamina.getBounds());
  108. lamina.paint(lamina.getGraphics());
  109.  
  110. }
  111. }
  112.  
  113. private LaminaPelota lamina;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement