PaweU

javalab 10

Jan 19th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.awt.*;
  2.  
  3. public class Branch implements XmasShape {
  4. int xpos;
  5. int ypos;
  6. // int x[]={0, 70, 140, 210, 280, 280};
  7. // int y[]={250, 220, 250, 220, 250, 0};
  8. int x[]={0, 0, 70, 140, 210, 280};
  9. int y[]={0, 250, 220, 250, 220, 250};
  10. double scale;
  11.  
  12. Branch (int xpos, int ypos, double scale) {
  13. this.xpos = xpos;
  14. this.ypos = ypos;
  15. this.scale = scale;
  16. }
  17.  
  18.  
  19. @Override
  20. public void render(Graphics2D g2d) {
  21. // ustaw kolor wypełnienia
  22. GradientPaint grad = new GradientPaint(0,0,new Color(46, 204, 113),0,100, new Color(0,0,0));
  23. g2d.setPaint(grad);
  24. g2d.fillPolygon(x,y,x.length);
  25. g2d.scale(-1,1);
  26. g2d.fillPolygon(x,y,x.length);
  27. }
  28.  
  29. @Override
  30. public void transform(Graphics2D g2d) {
  31. g2d.translate(xpos, ypos);
  32. g2d.scale(scale,scale);
  33. }
  34. }
  35.  
  36.  
  37. import java.awt.*;
  38.  
  39. public class Bubble implements XmasShape {
  40. int x;
  41. int y;
  42. double scale = .2;
  43. Color fillColor = new Color(223, 230, 233);
  44. Color lineColor = new Color(178, 190, 195);
  45.  
  46. Bubble (int x, int y) {
  47. this.x = x;
  48. this.y = y;
  49. }
  50.  
  51.  
  52. @Override
  53. public void render(Graphics2D g2d) {
  54. // ustaw kolor wypełnienia
  55. g2d.setColor(fillColor);
  56. g2d.fillOval(0,0,100,100);
  57. // ustaw kolor obramowania
  58. g2d.setColor(lineColor);
  59. g2d.drawOval(0,0,100,100);
  60. }
  61.  
  62. @Override
  63. public void transform(Graphics2D g2d) {
  64. g2d.translate(x,y);
  65. g2d.scale(scale,scale);
  66. }
  67. }import java.awt.*;
  68.  
  69. public class Bubble implements XmasShape {
  70. int x;
  71. int y;
  72. double scale = .2;
  73. Color fillColor = new Color(223, 230, 233);
  74. Color lineColor = new Color(178, 190, 195);
  75.  
  76. Bubble (int x, int y) {
  77. this.x = x;
  78. this.y = y;
  79. }
  80.  
  81.  
  82. @Override
  83. public void render(Graphics2D g2d) {
  84. // ustaw kolor wypełnienia
  85. g2d.setColor(fillColor);
  86. g2d.fillOval(0,0,100,100);
  87. // ustaw kolor obramowania
  88. g2d.setColor(lineColor);
  89. g2d.drawOval(0,0,100,100);
  90. }
  91.  
  92. @Override
  93. public void transform(Graphics2D g2d) {
  94. g2d.translate(x,y);
  95. g2d.scale(scale,scale);
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. import javax.swing.*;
  106. import java.awt.*;
  107. import java.awt.geom.AffineTransform;
  108. import java.util.ArrayList;
  109. import java.util.List;
  110.  
  111. public class DrawPanel extends JPanel {
  112.  
  113. List<XmasShape> shapes = new ArrayList<>();
  114.  
  115. DrawPanel(){
  116. setBackground(new Color(255, 234, 167));
  117. setOpaque(true);
  118. }
  119.  
  120. DrawPanel addBubble (int x, int y){
  121. Bubble b = new Bubble(x, y);
  122. shapes.add(b);
  123. return this;
  124. }
  125.  
  126. DrawPanel addBranch (int x, int y, double scale){
  127. Branch b = new Branch(x, y, scale);
  128. shapes.add(b);
  129. return this;
  130. }
  131.  
  132.  
  133. public void paintComponent(Graphics g){
  134. super.paintComponent(g);
  135. for(XmasShape s:shapes){
  136. s.draw((Graphics2D)g);
  137. }
  138. }
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145. import javax.swing.*;
  146.  
  147. public class main {
  148.  
  149. public static void main(String[] args) {
  150. // write your code here
  151. JFrame frame = new JFrame("Choinka");
  152. DrawPanel panel = new DrawPanel();
  153. panel.addBubble(10, 10);
  154. panel.addBubble(30, 30);
  155. panel.addBranch(400,320, 1);
  156. panel.addBranch(400,280, 0.7);
  157.  
  158. frame.setContentPane(panel);
  159. frame.setSize(1000, 700);
  160. frame.setLocationRelativeTo(null);
  161. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  162. frame.setResizable(true);
  163. frame.setVisible(true);
  164. }
  165. }
Add Comment
Please, Sign In to add comment