Как программу связать со списками - Java
Формулировка задачи:
Задача теперь усложняется.....Есть программа ADD и DELETE круга и квадрата с помощью кнопок.Помогите разобраться мне и сделать так чтобы было два списка КРУГ и КВАДРАТ и вся программа работала ТАК,при нажатие на списочек КРУГ добавлялись только круги, а при нажатии на список КВАДРАТ там в консоли добавлялись и удалялись только квадраты.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Go { public JFrame window = new JFrame(); public JButton[] bt = new JButton[4]; public String[] btNames = {"box", "oval", "delBox", "delOval"}; public ArrayListclassBox = new ArrayList (); public ArrayList classOval = new ArrayList (); public Go() { makeWindow(); makeButtons(); } private void makeWindow() { JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(700, 500)); panel.setOpaque(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().setBackground(Color.black); window.setResizable(false); window.add(panel); window.pack(); window.setLayout(null); window.setLocationRelativeTo(null); window.setVisible(true); } private void makeButtons() { for (int i = 0; i < bt.length; i++) { bt[i] = new JButton(); bt[i].setBounds(20+i*100, 20, 90, 25); bt[i].setText(btNames[i]); bt[i].setName(i+""); bt[i].setForeground(Color.BLACK); bt[i].setOpaque(false); bt[i].setContentAreaFilled(true); bt[i].setBorderPainted(true); bt[i].setFocusPainted(false); bt[i].setLayout(null); bt[i].addActionListener(new ActionListener() { int num = -1; public void actionPerformed(ActionEvent e) { if (Integer.parseInt(((JButton) e.getSource()).getName()) == 0) { num++; BaseEnemy Type = new BaseEnemy(Integer.parseInt(((JButton) e.getSource()).getName())); classBox.add(Type); window.add(Type); window.repaint(); } else if (Integer.parseInt(((JButton) e.getSource()).getName()) == 1) { num++; BaseEnemy Type = new BaseEnemy(Integer.parseInt(((JButton) e.getSource()).getName())); classOval.add(Type); window.add(Type); window.repaint(); } else if (Integer.parseInt(((JButton) e.getSource()).getName()) == 2) { if (num < classBox.size() - 1) { num++; classBox.get(num).fpsTimer.stop(); classBox.get(num).fpsTimer.removeActionListener(this); window.remove(classBox.get(num)); window.repaint(); } } else if (Integer.parseInt(((JButton) e.getSource()).getName()) == 3) { if (num < classOval.size() - 1) { num++; classOval.get(num).fpsTimer.stop(); classOval.get(num).fpsTimer.removeActionListener(this); window.remove(classOval.get(num)); window.repaint(); } } } }); window.add(bt[i]); } } public class BaseEnemy extends JLabel { private static final long serialVersionUID = 1L; int x = new Random().nextInt(700 - 100) + 100; int y = new Random().nextInt(500 - 100) + 100; int w = new Random().nextInt(50 - 20) + 20; int h = new Random().nextInt(50 - 20) + 20; int[] characterXY = {x,y}; Timer fpsTimer; int type; public BaseEnemy(int n) { setBounds(x, y, w, h); type = n; fpsTimer = new Timer(15, new ActionListener() { int[] arrSpeed = {1,1,1,1}; int[] speedControlXY = {1,1}; int[] speedXY = {arrSpeed[new Random().nextInt(arrSpeed.length)], arrSpeed[new Random().nextInt(arrSpeed.length)]}; public void actionPerformed(ActionEvent e) { characterXY[0] += speedXY[0]; characterXY[1] += speedXY[1]; setLocation(characterXY[0], characterXY[1]); if (characterXY[0] + getWidth() > 700) { characterXY[0] = 700 - getWidth(); speedControlXY[0] *= -1; speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0]; } else if (characterXY[0] < 0) { characterXY[0] = 0; speedControlXY[0] *= -1; speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0]; } else if (characterXY[1] < 0) { characterXY[1] = 0; speedControlXY[1] *= -1; speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1]; } else if (characterXY[1] + getHeight() > 500) { characterXY[1] = 500 - getHeight(); speedControlXY[1] *= -1; speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1]; } } }); fpsTimer.start(); } public void paintComponent(Graphics g) { if (type == 0) { g.setColor(new Color(120,190,255)); g.fillRect(0, 0, getWidth(), getHeight()); } else if (type == 1) { g.setColor(new Color(20,190,155)); g.fillOval(0, 0, getWidth(), getHeight()); } repaint(); } } public static void main(String[] args) { new Go(); } }
Решение задачи: «Как программу связать со списками»
textual
Листинг программы
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Go { public JFrame window = new JFrame(); public String[][] names = {{"add", "delete"}, {"box", "oval"}}; public JButton[] bt = new JButton[names[0].length]; public JComboBox<String> comboBox = new JComboBox<String>(); public ArrayList<ArrayList<BaseEnemy>> arrEnemy = new ArrayList<ArrayList<BaseEnemy>>(); public Go() { makeWindow(); makeComboBox(); makeButtons(); } private void makeWindow() { JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(800, 600)); panel.setOpaque(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().setBackground(Color.black); window.setResizable(false); window.add(panel); window.pack(); window.setLayout(null); window.setLocationRelativeTo(null); window.setVisible(true); } private void makeButtons() { for (int i = 0; i < bt.length; i++) { bt[i] = new JButton(); bt[i].setBounds(20+i*100, 20, 90, 25); bt[i].setText(names[0][i]); bt[i].setName(i+""); bt[i].setForeground(Color.BLACK); bt[i].setOpaque(false); bt[i].setContentAreaFilled(true); bt[i].setBorderPainted(true); bt[i].setFocusPainted(false); bt[i].setLayout(null); bt[i].addActionListener(new ActionListener() { int b = -1; int o = -1; public void actionPerformed(ActionEvent e) { if (Integer.parseInt(((JButton) e.getSource()).getName()) == 0) { BaseEnemy Type = new BaseEnemy(comboBox.getSelectedIndex()); window.add(Type); window.repaint(); if (new String("box").equals(comboBox.getSelectedItem())) { b++; arrEnemy.get(0).add(Type); } else if (new String("oval").equals(comboBox.getSelectedItem())) { o++; arrEnemy.get(1).add(Type); } } else if (Integer.parseInt(((JButton) e.getSource()).getName()) == 1) { if (new String("box").equals(comboBox.getSelectedItem()) && b < arrEnemy.get(0).size() - 1) { b++; arrEnemy.get(0).get(b).fpsTimer.stop(); arrEnemy.get(0).get(b).fpsTimer.removeActionListener(this); window.remove(arrEnemy.get(0).get(b)); window.repaint(); } else if (new String("oval").equals(comboBox.getSelectedItem()) && o < arrEnemy.get(1).size() - 1) { o++; arrEnemy.get(1).get(o).fpsTimer.stop(); arrEnemy.get(1).get(o).fpsTimer.removeActionListener(this); window.remove(arrEnemy.get(1).get(o)); window.repaint(); } } } }); window.add(bt[i]); } } public void makeComboBox() { for (int i = 0; i < names[1].length; i++) { arrEnemy.add(new ArrayList<BaseEnemy>()); comboBox.addItem(names[1][i]); } comboBox.setFont(new Font("Times New Roman", Font.BOLD, 18)); comboBox.setSelectedIndex(0); comboBox.setBounds(20, 55, 190, 30); ((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT); comboBox.setFocusable(false); window.add(comboBox); window.revalidate(); window.repaint(); } public class BaseEnemy extends JLabel { private static final long serialVersionUID = 1L; int x = new Random().nextInt(700 - 100) + 100; int y = new Random().nextInt(500 - 100) + 100; int w = new Random().nextInt(50 - 20) + 20; int h = new Random().nextInt(50 - 20) + 20; int[] characterXY = {x,y}; Timer fpsTimer; int type; public BaseEnemy(int n) { setBounds(x, y, w, h); type = n; fpsTimer = new Timer(15, new ActionListener() { int[] arrSpeed = {3,6,9,12}; int[] speedControlXY = {1,1}; int[] speedXY = {arrSpeed[new Random().nextInt(arrSpeed.length)], arrSpeed[new Random().nextInt(arrSpeed.length)]}; public void actionPerformed(ActionEvent e) { characterXY[0] += speedXY[0]; characterXY[1] += speedXY[1]; setLocation(characterXY[0], characterXY[1]); if (characterXY[0] + getWidth() > 700) { characterXY[0] = 700 - getWidth(); speedControlXY[0] *= -1; speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0]; } else if (characterXY[0] < 0) { characterXY[0] = 0; speedControlXY[0] *= -1; speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0]; } else if (characterXY[1] < 0) { characterXY[1] = 0; speedControlXY[1] *= -1; speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1]; } else if (characterXY[1] + getHeight() > 500) { characterXY[1] = 500 - getHeight(); speedControlXY[1] *= -1; speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1]; } } }); fpsTimer.start(); } public void paintComponent(Graphics g) { if (type == 0) { g.setColor(new Color(120,190,255)); g.fillRect(0, 0, getWidth(), getHeight()); } else if (type == 1) { g.setColor(new Color(20,190,155)); g.fillOval(0, 0, getWidth(), getHeight()); } repaint(); } } public static void main(String[] args) { new Go(); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д