Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- public class Branch implements XmasShape {
- int xpos;
- int ypos;
- // int x[]={0, 70, 140, 210, 280, 280};
- // int y[]={250, 220, 250, 220, 250, 0};
- int x[]={0, 0, 70, 140, 210, 280};
- int y[]={0, 250, 220, 250, 220, 250};
- double scale;
- Branch (int xpos, int ypos, double scale) {
- this.xpos = xpos;
- this.ypos = ypos;
- this.scale = scale;
- }
- @Override
- public void render(Graphics2D g2d) {
- // ustaw kolor wypełnienia
- GradientPaint grad = new GradientPaint(0,0,new Color(46, 204, 113),0,100, new Color(0,0,0));
- g2d.setPaint(grad);
- g2d.fillPolygon(x,y,x.length);
- g2d.scale(-1,1);
- g2d.fillPolygon(x,y,x.length);
- }
- @Override
- public void transform(Graphics2D g2d) {
- g2d.translate(xpos, ypos);
- g2d.scale(scale,scale);
- }
- }
- import java.awt.*;
- public class Bubble implements XmasShape {
- int x;
- int y;
- double scale = .2;
- Color fillColor = new Color(223, 230, 233);
- Color lineColor = new Color(178, 190, 195);
- Bubble (int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public void render(Graphics2D g2d) {
- // ustaw kolor wypełnienia
- g2d.setColor(fillColor);
- g2d.fillOval(0,0,100,100);
- // ustaw kolor obramowania
- g2d.setColor(lineColor);
- g2d.drawOval(0,0,100,100);
- }
- @Override
- public void transform(Graphics2D g2d) {
- g2d.translate(x,y);
- g2d.scale(scale,scale);
- }
- }import java.awt.*;
- public class Bubble implements XmasShape {
- int x;
- int y;
- double scale = .2;
- Color fillColor = new Color(223, 230, 233);
- Color lineColor = new Color(178, 190, 195);
- Bubble (int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public void render(Graphics2D g2d) {
- // ustaw kolor wypełnienia
- g2d.setColor(fillColor);
- g2d.fillOval(0,0,100,100);
- // ustaw kolor obramowania
- g2d.setColor(lineColor);
- g2d.drawOval(0,0,100,100);
- }
- @Override
- public void transform(Graphics2D g2d) {
- g2d.translate(x,y);
- g2d.scale(scale,scale);
- }
- }
- import javax.swing.*;
- import java.awt.*;
- import java.awt.geom.AffineTransform;
- import java.util.ArrayList;
- import java.util.List;
- public class DrawPanel extends JPanel {
- List<XmasShape> shapes = new ArrayList<>();
- DrawPanel(){
- setBackground(new Color(255, 234, 167));
- setOpaque(true);
- }
- DrawPanel addBubble (int x, int y){
- Bubble b = new Bubble(x, y);
- shapes.add(b);
- return this;
- }
- DrawPanel addBranch (int x, int y, double scale){
- Branch b = new Branch(x, y, scale);
- shapes.add(b);
- return this;
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- for(XmasShape s:shapes){
- s.draw((Graphics2D)g);
- }
- }
- }
- import javax.swing.*;
- public class main {
- public static void main(String[] args) {
- // write your code here
- JFrame frame = new JFrame("Choinka");
- DrawPanel panel = new DrawPanel();
- panel.addBubble(10, 10);
- panel.addBubble(30, 30);
- panel.addBranch(400,320, 1);
- panel.addBranch(400,280, 0.7);
- frame.setContentPane(panel);
- frame.setSize(1000, 700);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setResizable(true);
- frame.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment