我想用 Java 做一个非常简单的“画图”。现在我遇到了麻烦,因为我不知道如何使用 JRaddioButtons 中的 actionListener 来更改我想要绘制的线条的颜色。
我显示我的代码,如果太大,抱歉:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Dibujar extends JComponent {
private Point inicioArrastre;
private Point finArrastre;
private ArrayList<Shape> lineas = new ArrayList<Shape>();
public Dibujar() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse
inicioArrastre = new Point(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse
finArrastre = new Point(e.getX(), e.getY());
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
lineas.add(linea);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse
finArrastre = new Point(e.getX(), e.getY());
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
lineas.add(linea);
inicioArrastre = new Point(finArrastre.x, finArrastre.y);
repaint();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
for (Shape linea : lineas) { // dibuja todos las elipses
g2.draw(linea);
}
}
private Line2D.Float crearLinea(int x1, int y1, int x2, int y2) {
return new Line2D.Float(x1, y1, x2, y2);
}
public static void main(String[] a3d) {
Dibujar dibu = new Dibujar();
JFrame ventana = new JFrame("Dibujar");
ventana.setJMenuBar(dibu.createMenuBar());
ventana.setSize(400, 300);
ventana.setLocationRelativeTo(null);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.add(new Dibujar());
ventana.setVisible(true);
}
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Select a Color",
KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("RED");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
rbMenuItem.addActionListener(null);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("BLUE");
rbMenuItem.setMnemonic(KeyEvent.VK_B);
group.add(rbMenuItem);
menu.add(rbMenuItem);
return menuBar;
}
}
非常感谢!