Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package komunikator;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.ArrayList;
- class Okno extends JFrame{
- Okno(String nazwa){
- super(nazwa); //metoda wywołuje konstruktor nadklasy
- setResizable(false);
- setSize(400,400);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- }
- }
- class Rysowanie extends Canvas{
- public Color color;
- Rysowanie(){
- super();
- addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent ke) {
- points.add(new Point(x,y));
- ke.getKeyCode();
- if(ke.getKeyCode() == KeyEvent.VK_LEFT){
- x=x-3;
- }
- if(ke.getKeyCode() == KeyEvent.VK_RIGHT){
- x=x+3;
- }
- if(ke.getKeyCode() == KeyEvent.VK_UP){
- y=y-3;
- }
- if(ke.getKeyCode() == KeyEvent.VK_DOWN){
- y=y+3;
- }
- repaint(); //metoda wywołująca paint
- }
- });
- addMouseListener(new MouseAdapter(){
- public void mousePressed(MouseEvent me) {
- x = me.getX();
- y = me.getY();
- points.add(new Point(x,y));
- repaint();
- //System.out.println("Czy cos sie wyswietla");
- }
- });
- }
- public void paint(Graphics g){
- Graphics2D g2 = (Graphics2D) g;
- g.drawOval(2, 2, 2, 2);
- g2.setColor(color);
- int x2, y2;
- for(Point p:points){
- x2=(int)p.getX();
- y2=(int)p.getY();
- g2.fillOval(x2, y2, 10, 10);
- }
- }
- private int x, y;
- ArrayList<Point> points = new ArrayList<Point>();
- }
- public class Komunikator extends JFrame{
- public static void main(String[] args) {
- Okno o = new Okno ("Panel z przyciskami");
- //Rysowanie r = new Rysowanie ("Panel z rysowaniem");
- JPanel p1 = new JPanel(); //panel z prawej strony z przyciskami
- JPanel p2 = new JPanel(); //panel
- //add(new JButton("Central"), BorderLayout.CENTER);
- JCheckBox zolty = new JCheckBox("Żółty");
- JCheckBox czerwony = new JCheckBox("Czerwony");
- p1.add(zolty);
- p1.add(czerwony);
- p2.setBackground(Color.red);
- Rysowanie r = new Rysowanie();
- o.add(p1, BorderLayout.EAST);
- o.add(r, BorderLayout.CENTER);
- o.setVisible(true);
- zolty.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e){
- r.color= Color.YELLOW;
- }
- });
- czerwony.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e){
- r.color=Color.RED;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement