Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. package komunikator;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.ArrayList;
  6.  
  7.  
  8. class Okno extends JFrame{
  9. Okno(String nazwa){
  10. super(nazwa); //metoda wywołuje konstruktor nadklasy
  11. setResizable(false);
  12. setSize(400,400);
  13. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. setLayout(new BorderLayout());
  15.  
  16.  
  17. }
  18. }
  19.  
  20. class Rysowanie extends Canvas{
  21.  
  22.  
  23. public Color color;
  24.  
  25.  
  26.  
  27. Rysowanie(){
  28. super();
  29. addKeyListener(new KeyAdapter() {
  30. public void keyPressed(KeyEvent ke) {
  31.  
  32. points.add(new Point(x,y));
  33. ke.getKeyCode();
  34.  
  35. if(ke.getKeyCode() == KeyEvent.VK_LEFT){
  36. x=x-3;
  37. }
  38. if(ke.getKeyCode() == KeyEvent.VK_RIGHT){
  39. x=x+3;
  40. }
  41. if(ke.getKeyCode() == KeyEvent.VK_UP){
  42. y=y-3;
  43. }
  44. if(ke.getKeyCode() == KeyEvent.VK_DOWN){
  45. y=y+3;
  46. }
  47. repaint(); //metoda wywołująca paint
  48. }
  49. });
  50.  
  51. addMouseListener(new MouseAdapter(){
  52. public void mousePressed(MouseEvent me) {
  53. x = me.getX();
  54. y = me.getY();
  55. points.add(new Point(x,y));
  56. repaint();
  57. //System.out.println("Czy cos sie wyswietla");
  58. }
  59. });
  60. }
  61. public void paint(Graphics g){
  62. Graphics2D g2 = (Graphics2D) g;
  63. g.drawOval(2, 2, 2, 2);
  64. g2.setColor(color);
  65.  
  66. int x2, y2;
  67. for(Point p:points){
  68. x2=(int)p.getX();
  69. y2=(int)p.getY();
  70. g2.fillOval(x2, y2, 10, 10);
  71. }
  72.  
  73. }
  74.  
  75. private int x, y;
  76. ArrayList<Point> points = new ArrayList<Point>();
  77.  
  78. }
  79.  
  80.  
  81. public class Komunikator extends JFrame{
  82.  
  83. public static void main(String[] args) {
  84.  
  85. Okno o = new Okno ("Panel z przyciskami");
  86. //Rysowanie r = new Rysowanie ("Panel z rysowaniem");
  87.  
  88. JPanel p1 = new JPanel(); //panel z prawej strony z przyciskami
  89. JPanel p2 = new JPanel(); //panel
  90. //add(new JButton("Central"), BorderLayout.CENTER);
  91.  
  92. JCheckBox zolty = new JCheckBox("Żółty");
  93. JCheckBox czerwony = new JCheckBox("Czerwony");
  94.  
  95. p1.add(zolty);
  96. p1.add(czerwony);
  97. p2.setBackground(Color.red);
  98.  
  99. Rysowanie r = new Rysowanie();
  100.  
  101. o.add(p1, BorderLayout.EAST);
  102. o.add(r, BorderLayout.CENTER);
  103.  
  104. o.setVisible(true);
  105.  
  106. zolty.addActionListener(new ActionListener()
  107. {
  108. @Override
  109. public void actionPerformed(ActionEvent e){
  110.  
  111. r.color= Color.YELLOW;
  112.  
  113. }
  114.  
  115. });
  116.  
  117. czerwony.addActionListener(new ActionListener()
  118. {
  119. @Override
  120. public void actionPerformed(ActionEvent e){
  121. r.color=Color.RED;
  122. }
  123. });
  124.  
  125.  
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement