У меня возникла огромная проблема в Java, создающая графический интерфейс пользователя, который вводит данные пользователя и создает круг на основе этого ввода. Я беру данные от пользователя и сохраняю значения в getUserX, getUserY, getRadius и circleColor, однако я понятия не имею, как передать эти переменные в массив, который используется компонентом paint для создания круга.
Как правильно взять значение переменных getUserX, getUserY и getRadius и поместить их в массив circleValues? Как взять circleColor, взятый из пользовательского ввода, и поместить его как значение page.setColor для созданного круга?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CircleMaker
{
public static void main (String[] args)
{
BuildsFrame();
}
public static void BuildsFrame()
{
JFrame frame = new JFrame ("Circle Drawer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab ("Intro", new IntroPanel());
tp.addTab("Your Circle!", new CirclePanel());
frame.getContentPane().add(tp);
frame.pack();
frame.setVisible(true);
}
}
class IntroPanel extends JPanel
{
public IntroPanel()
{
final JTextField xCoorTF, yCoorTF, radiusTF;
int userXcoor, userYcoor, userRadius;
JButton makeButton = new JButton("Create!");
final JColorChooser colorChooser;
setLayout (new FlowLayout());
setBackground (Color.gray);
setPreferredSize (new Dimension(700, 500));
JLabel l1 = new JLabel ("Enter your desired coordinates and radius for your circle.");
JLabel l2 = new JLabel ("Please type the X coordinate in the first box, the");
JLabel l3 = new JLabel ("Y coordinates in the second box,");
JLabel l4 = new JLabel ("and the radius of your circle");
JLabel l5 = new JLabel ("in the final box.");
xCoorTF = new JTextField(5);
yCoorTF = new JTextField(5);
radiusTF = new JTextField(5);
colorChooser = new JColorChooser();
colorChooser.setBorder(BorderFactory.createTitledBorder("Choose Circle Color:"));
makeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int getUserX = Integer.parseInt(xCoorTF.getText());
int getUserY = Integer.parseInt(yCoorTF.getText());
int getRadius = Integer.parseInt(radiusTF.getText());
Color circleColor = colorChooser.getColor();
System.out.println(getUserX);
System.out.println(getUserY);
System.out.println(getRadius);
}
});
add (l1);
add (l2);
add (l3);
add (l4);
add (l5);
add (xCoorTF);
add (yCoorTF);
add (radiusTF);
add (colorChooser);
add (makeButton);
}
}
class CirclePanel extends JPanel
{
int[] circleValues = {50, 50, 100};
circleValues[0] = getUserX;
circleValues[1] = getUserY;
circleValues[2] = getRadius;
public CirclePanel()
{
setBackground (Color.WHITE);
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.BLACK);
page.fillOval((circleValues[0] - 2), (circleValues[1] - 2), (circleValues[2] + 4), (circleValues[2] + 4));
page.setColor (Color.GRAY);
page.fillOval (circleValues[0], circleValues[1], (circleValues[2]), (circleValues[2]));
}
}
В отличие от многих других языков, команды не могут "плавать в пространстве" на Java.
Вам нужно будет увеличить объем этих данных, а также переместить заливку массива circleValues в сегмент кода, который будет выполнен.