YauhenMardan

Untitled

Mar 12th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import javax.swing.event.ChangeEvent;
  6. import javax.swing.event.ChangeListener;
  7. import java.awt.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12. import java.io.IOException;
  13.  
  14. public class BallDialog extends JDialog {
  15.  
  16. //const
  17. public static int SCREEN_WIDTH=200;
  18. public static int SCREEN_HEIGHT=200;
  19. public static int IMAGE_WIDTH=30;
  20. public static int IMAGE_HEIGHT=30;
  21. public static String IMAGE_FILE_NAME="img_311846.png";
  22. public static int TIMER_SPEED=10;
  23. public static int MIN_SPEED=10;
  24. public static int MAX_SPEED=310;
  25. public static int INT_SPEED=10;
  26. public static String[] items={"Inward","Backward"};
  27. //gui
  28. private JSlider slider;
  29. private JPanel drawPanel;
  30. private JPanel menuPanel;
  31. private JPanel mainPanel;
  32. private JComboBox comboBox;
  33. private Timer timer;
  34. private JMenu menu;
  35. private JMenuBar menuBar;
  36. //
  37. private BufferedImage dbImage =null;
  38. private BufferedImage image;
  39. private int currentSpeed=MAX_SPEED/2;
  40. private int direction=1;
  41.  
  42. BallDialog(JFrame owner){
  43. super(owner,true);
  44. //panel
  45. mainPanel=new JPanel(new BorderLayout());
  46. menuPanel=new JPanel(new BorderLayout());
  47. //slider
  48. slider=new JSlider(MIN_SPEED,MAX_SPEED,INT_SPEED);
  49. slider.setValue(currentSpeed);
  50. menuPanel.add(slider,BorderLayout.EAST);
  51. //combobox
  52. comboBox=new JComboBox(items);
  53. menuPanel.add(comboBox,BorderLayout.WEST);
  54. //panel
  55. drawPanel =new JPanel(){
  56. @Override
  57. public void paintComponent(Graphics g){
  58. super.paintComponent(g);
  59. g.drawImage(dbImage,0,0,this);
  60. }
  61. };
  62. drawPanel.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
  63. drawPanel.setDoubleBuffered(true);
  64. drawPanel.setFocusable(true);
  65. drawPanel.setBackground(Color.white);
  66. //image
  67. readImage(IMAGE_FILE_NAME);
  68. //timer
  69. timer =new Timer(TIMER_SPEED, new ActionListener() {
  70. int elapsedTime=0;
  71. @Override
  72. public void actionPerformed(ActionEvent e) {
  73. if(elapsedTime==60){
  74. elapsedTime=0;
  75. }
  76. int width= drawPanel.getWidth();
  77. int height= drawPanel.getHeight();
  78. int diam=Math.min(width,height);
  79. dbImage = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
  80. Graphics dbStartGraphics= dbImage.getGraphics();
  81. dbStartGraphics.setColor(Color.WHITE);
  82. dbStartGraphics.fillRect(0,0,width,height);
  83. int x=getCircleX(diam/2,diam/2-IMAGE_WIDTH,elapsedTime);
  84. int y=getCircleY(diam/2,diam/2-IMAGE_HEIGHT,elapsedTime);
  85. dbStartGraphics.drawImage(image,x-IMAGE_WIDTH/2,y-IMAGE_HEIGHT/2,IMAGE_WIDTH,IMAGE_HEIGHT,null);
  86. elapsedTime++;
  87. repaint();
  88. }
  89. });
  90. timer.start();
  91. //window
  92. setContentPane(drawPanel);
  93. setResizable(true);
  94. setMinimumSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
  95. pack();
  96. //
  97. slider.addChangeListener(new ChangeListener() {
  98. @Override
  99. public void stateChanged(ChangeEvent e) {
  100. currentSpeed=slider.getValue();
  101. timer.setDelay(MAX_SPEED/currentSpeed*TIMER_SPEED);
  102. }
  103. });
  104. comboBox.addActionListener(new ActionListener() {
  105. @Override
  106. public void actionPerformed(ActionEvent e) {
  107. if(comboBox.getSelectedIndex()==0){
  108. direction=1;
  109. } else{
  110. direction=-1;
  111. }
  112. }
  113. });
  114. }
  115. void readImage(String filename){
  116. try{
  117. File file=new File(filename);
  118. image = ImageIO.read(file);
  119. }catch (IOException e){
  120. JOptionPane.showMessageDialog(this, "Cannot open image.", "Waring", JOptionPane.WARNING_MESSAGE);
  121. }
  122. }
  123. int getCircleX(int o,int r,int time){
  124. Double x=r*Math.cos(direction*(Math.PI/30*time-Math.PI/2))+o;
  125. return x.intValue();
  126. }
  127. int getCircleY(int o,int r,int time){
  128. Double y=r*Math.sin(direction*(Math.PI/30*time-Math.PI/2))+o;
  129. return y.intValue();
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment