Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import java.awt.event.MouseMotionListener;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10.  
  11. class MyCurve{
  12.     Color color;
  13.     ArrayList<Integer> ptx;
  14.     ArrayList<Integer> pty;
  15.     MyCurve(){
  16.         ptx = new ArrayList<>();
  17.         pty = new ArrayList<>();
  18.        
  19.         int r = (int)(Math.random()*256);
  20.         int g = (int)(Math.random()*256);
  21.         int b = (int)(Math.random()*256);
  22.         color = new Color(r,g,b);
  23.     }
  24.     void addPoint(int x, int y) {
  25.         ptx.add(x);
  26.         pty.add(y);
  27.     }
  28.     void draw(Graphics g)   {
  29.         g.setColor(color);
  30.         for(int i=0; i<ptx.size()-1; i++) {
  31.             int x1 = ptx.get(i);
  32.             int y1 = pty.get(i);
  33.             int x2 = ptx.get(i+1);
  34.             int y2 = pty.get(i+1);
  35.             g.drawLine(x1,y1, x2, y2);
  36.         }
  37.     }
  38.    
  39. }
  40.  
  41. class MyGraphics2DPanel extends JPanel
  42.                         implements MouseMotionListener,
  43.                                    MouseListener
  44. {
  45.     ArrayList <MyCurve> curves;
  46.    
  47.     MyGraphics2DPanel(){
  48.         setBackground(Color.GREEN);
  49.         addMouseMotionListener(this);
  50.         addMouseListener(this);
  51.         curves = new ArrayList<>();
  52.     }
  53.     @Override
  54.     public void paintComponent(Graphics g){
  55.         super.paintComponent(g);
  56.  
  57.         for(MyCurve c : curves){
  58.             c.draw(g);
  59.         }      
  60.     }
  61.     public void mousePressed(MouseEvent e) {
  62.         MyCurve c = new MyCurve();
  63.         c.addPoint(e.getX(), e.getY());
  64.         curves.add(c);
  65.     }
  66.     public void mouseDragged(MouseEvent e) {
  67.         if(curves.size()<1) return;
  68.         curves.get(curves.size()-1).addPoint(e.getX(), e.getY());
  69.         repaint();
  70.     }
  71.     public void mouseReleased(MouseEvent e) {
  72.         if(curves.size()<1) return;
  73.         curves.get(curves.size()-1).addPoint(e.getX(), e.getY());
  74.         repaint();
  75.     }
  76.    
  77.     public void mouseMoved(MouseEvent e) {  }
  78.     public void mouseClicked(MouseEvent e) {}
  79.     public void mouseEntered(MouseEvent e) {}
  80.     public void mouseExited(MouseEvent e) {}   
  81. }
  82. public class SwingGraphics2D extends JFrame{
  83.     public static void main(String[] args) {
  84.         new SwingGraphics2D();
  85.     }
  86.     SwingGraphics2D()   {
  87.         setSize(400,400);
  88.         setTitle("Graphics2D");
  89.        
  90.         add(new MyGraphics2DPanel());
  91.        
  92.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  93.         setVisible(true);
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement