Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Задание №4
- Напишите программу, выводящую окошко с 6 кнопками: ВЫШЕ, НИЖЕ, ЛЕВЕЕ, ПРАВЕЕ,
- СВЕТЛЕЕ, ТЕМНЕЕ.
- В окошке нарисован отрезок, проходящий под наклоном примерно через центр окошка.
- Нажимая на кнопки, двигаем отрезок вверх-вниз-влево-вправо, либо делаем его светлее/темнее.
- Также в окошке выводится надпись о том, какая кнопка была нажата в последний раз.
- Продвинутые программисты могут добавить кнопки «Вращение по часовой стрелке» и
- «Вращение против часовой стрелки».
- */
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.Line2D;
- public class Main
- { public static void main(String[] args)
- {
- new MyFrame();
- }
- }
- class MyFrame extends JFrame
- {
- public MyFrame()
- {
- super("This Frame");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- MyPanel butPanel = new MyPanel();
- Container contBut = getContentPane();
- contBut.add(butPanel);
- setLocation(500,350);
- setSize(700,300);
- setVisible(true);
- }
- }
- class MyPanel extends JPanel implements ActionListener
- {
- JButton but1,but2,but3,but4,but5,but6;
- double x1 = 50,y1=50,x2=100,y2=250;
- int rc=255,gc=100,bc=100;
- public MyPanel() {
- but1 = new JButton("ВЫШЕ");
- but2 = new JButton("НИЖЕ");
- but3 = new JButton("ЛЕВЕЕ");
- but4 = new JButton("ПРАВЕЕ");
- but5 = new JButton("СВЕТЛЕЕ");
- but6 = new JButton("ТЕМНЕЕ");
- add(but1);
- add(but2);
- add(but3);
- add(but4);
- add(but5);
- add(but6);
- but6.addActionListener(this);
- but5.addActionListener(this);
- but4.addActionListener(this);
- but3.addActionListener(this);
- but2.addActionListener(this);
- but1.addActionListener(this);
- }
- public void paintComponent(Graphics graf) {
- super.paintComponent(graf);
- Graphics2D g = (Graphics2D) graf;
- BasicStroke pen = new BasicStroke(6);
- g.setStroke(pen);
- try
- {
- Color c = new Color(rc, gc, bc);
- Line2D myLine = new Line2D.Double(x1, y1, x2, y2);
- g.setColor(c);
- g.draw(myLine);
- }
- catch(IllegalArgumentException ex)
- {
- if ((bc>255)||(gc>255))
- {
- g.drawString("DO DARKER", 300, 150);
- }
- if ((bc<0)||(gc<0))
- {
- g.drawString("DO LIGHTER",300,150);
- }
- }
- }
- public void actionPerformed(ActionEvent event) throws IllegalArgumentException
- {
- Object source = event.getSource();
- if (source == but4)
- {
- x1=x1+50;
- x2=x2+50;
- }
- if (source == but3)
- {
- x1=x1-50;
- x2=x2-50;
- }
- if (source == but2)
- {
- y1=y1+10;
- y2=y2+10;
- }
- if (source == but1)
- {
- y1=y1-10;
- y2=y2-10;
- }
- if (source==but5)
- {
- if ((bc>255)||(gc>255)) throw new IllegalArgumentException("do light");
- gc=gc+10;
- bc=bc+10;
- }
- if (source==but6)
- {
- if ((bc<0)||(gc<0)) throw new IllegalArgumentException("do dark");
- gc=gc-10;
- bc=bc-10;
- }
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment