Прорисовка флага на java
Формулировка задачи:
Всем привет, нужно нарисовать флаг, после вводя длину и ширину в JTextField и нажатием на кнопку изменить размер флага. Как это можно сделать?
Кто-нибудь подскажите пожалуйста.
Листинг программы
- package SolutionWork;
- import java.awt.*;
- import javax.swing.*;
- public class SolutionFlag extends JFrame {
- public SolutionFlag() {
- setBounds(0, 0, 300, 200);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- new SolutionFlag();
- JPanel window = new JPanel();
- FlowLayout fl = new FlowLayout();
- window.setLayout(fl);
- JButton button = new JButton("Razmer");
- JTextField dlina = new JTextField(10);
- JTextField shirina = new JTextField(10);
- window.add(button);
- window.add(dlina);
- window.add(shirina);
- JFrame frame = new JFrame("New");
- frame.setContentPane(window);
- frame.setSize(400, 100);
- frame.setVisible(true);
- new SolutionFlag();
- }
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(Color.BLACK);
- g.fillRect(40, 40, 200, 40);
- g.setColor(Color.RED);
- g.fillRect(40, 80, 200, 40);
- g.setColor(Color.YELLOW);
- g.fillRect(40, 120, 200, 40);
- }
- }
Решение задачи: «Прорисовка флага на java»
textual
Листинг программы
- package SolutionWork;
- /**
- * Created by Chingiz on 15.03.2016.
- */
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- public class SimpleDraw extends Applet implements ActionListener {
- Graphics gr;
- Button b;
- TextField dl;
- TextField sh;
- public void init() {
- dl = new TextField(10);
- sh = new TextField(10);
- b = new Button("Draw!");
- add(b);
- add(dl);
- add(sh);
- b.addActionListener(this);
- gr = getGraphics();
- }
- public void actionPerformed(ActionEvent ae) {
- super.paint(gr);
- String s = dl.getText();
- String s1 = sh.getText();
- gr.setColor(Color.BLACK);
- gr.fillRect(40, Integer.parseInt(s)/3, Integer.parseInt(s), Integer.parseInt(s1));
- gr.setColor(Color.RED);
- gr.fillRect(40, Integer.parseInt(s)/3, Integer.parseInt(s), Integer.parseInt(s1));
- gr.setColor(Color.YELLOW);
- gr.fillRect(40, Integer.parseInt(s)/3, Integer.parseInt(s), Integer.parseInt(s1));
- }
- /*public SimpleDraw() {
- setBounds(0, 0, 300, 200);
- setVisible(true);
- }
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(Color.BLACK);
- g.fillRect(40, 40, 200, 40);
- g.setColor(Color.RED);
- g.fillRect(40, 80, 200, 40);
- g.setColor(Color.YELLOW);
- g.fillRect(40, 120, 200, 40);
- }*/
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д