Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.awt.Dimension;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.geom.Ellipse2D;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Scanner;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JPanel;
  17.  
  18. class ShapeExample extends JPanel {
  19.      
  20.     public Graphics2D g2d = null;
  21.     private static final long serialVersionUID = -3717207634048171363L;
  22.     private List<Float[]> _punkty = new ArrayList<Float[]>();
  23.     JButton liniowaB = new JButton("Skala liniowa");
  24.     JButton logarytmicznaB = new JButton("Skala logarytm...");
  25.     static Boolean czyLogarytmicznaSkalaJest=false;
  26.     public void buttony(){
  27.         add(liniowaB);
  28.         add(logarytmicznaB);
  29.     liniowaB.setBounds(2,2,200,40);
  30.     liniowaB.setVisible(true);
  31.     logarytmicznaB.setBounds(5,50,200,40);
  32.     logarytmicznaB.setVisible(true);
  33.  
  34.     liniowaB.addActionListener(new ActionListener(){
  35.     public void actionPerformed(ActionEvent e)
  36.     {
  37.     czyLogarytmicznaSkalaJest=false; repaint();
  38.     }
  39.     });
  40.  
  41.     logarytmicznaB.addActionListener(new ActionListener(){
  42.     public void actionPerformed(ActionEvent e)
  43.     {
  44.     czyLogarytmicznaSkalaJest=true; repaint();
  45.     }
  46.     });
  47.     }
  48.  
  49.      
  50.      public ShapeExample(List<Float[]> _punkty2){
  51.          this._punkty=_punkty2;
  52.          buttony();
  53.      }
  54.  
  55.       public void paintComponent(Graphics g) {
  56.           clear(g);
  57.             g2d = (Graphics2D)g;
  58.             g.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
  59.             g.drawLine(this.getWidth()/2,0,this.getWidth()/2,this.getHeight());// y-axis
  60.             for(Float[] kolo : _punkty){
  61.             if(!czyLogarytmicznaSkalaJest){
  62.                 g2d.draw(new Ellipse2D.Float(this.getWidth()/2+kolo[0]-(float)(((this.getWidth()+this.getHeight())/200)*kolo[2])/2,
  63.                                          this.getHeight()/2+kolo[1]-(float)(((this.getWidth()+this.getHeight())/200)*kolo[2])/2,
  64.                                          (float)(((this.getWidth()+this.getHeight())/200)*kolo[2]),
  65.                                          (float)(((this.getWidth()+this.getHeight())/200)*kolo[2])));
  66.             }
  67.             else {
  68.                 g2d.draw(new Ellipse2D.Float(this.getWidth()/2+kolo[0]-(float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])/2,
  69.                          this.getHeight()/2+kolo[1]-(float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])/2,
  70.                          (float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2]),
  71.                          (float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])));  
  72.                 }
  73.             }
  74.         int n = _punkty.size();
  75.         int i = 1;
  76.         while(i < n){
  77.             Float[] a = _punkty.get(i-1);
  78.             Float[] b = _punkty.get(i);
  79.             g.drawLine((int)(this.getWidth()/2+a[0]), (int)(this.getHeight()/2+a[1]), (int)(this.getWidth()/2+b[0]), (int)(this.getHeight()/2+b[1]));
  80.             ++i;
  81.         }
  82.       }
  83.  
  84.       // super.paintComponent clears offscreen pixmap,
  85.       // since we're using double buffering by default.
  86.  
  87.       protected void clear(Graphics g) {
  88.         super.paintComponent(g);
  89.       }
  90.  
  91.  
  92.      
  93. }
  94. class Start{
  95.  
  96.     public static void GUI(List<Float[]> _punkty){
  97.         JFrame _mainWindow = new JFrame("Okienko do rysowania");
  98.        
  99.         _mainWindow.setMinimumSize(new Dimension(600,600));
  100.         _mainWindow.setBackground(Color.white);
  101.            
  102.        
  103.        
  104.        
  105.        
  106.        
  107.        
  108.         _mainWindow.setContentPane(new ShapeExample(_punkty));
  109.         _mainWindow.pack();
  110.         _mainWindow.setVisible(true);
  111.        
  112.     }
  113.  
  114.  
  115.     public static void main(String[] args) {
  116.         Scanner wczytaj = new Scanner(System.in);
  117.         int n=0;
  118.         n = wczytaj.nextInt();
  119.          List<Float[]> _punkty = new ArrayList<Float[]>();
  120.         int i = 0;
  121.         float a,b,c;
  122.         while(i<n){
  123.             a = new Float(wczytaj.next());
  124.             b = new Float(wczytaj.next());
  125.             c = new Float(wczytaj.next());
  126.            
  127.             Float[] x = {a,b,c};
  128.             _punkty.add(x);
  129.             ++i;
  130.         }
  131.         GUI(_punkty);
  132.            
  133.  
  134.     }
  135.  
  136. }
Add Comment
Please, Sign In to add comment