作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我在尝试使用工具栏上的按钮来更改我正在绘制的线条的颜色时遇到了很多麻烦。这是我的代码:
public class PaintingGUI {
private static final int FRAME_WIDTH = 960;
private static final int FRAME_HEIGHT = 640;
private final static String NEWLINE = System.lineSeparator();
private JTextArea textArea = new JTextArea();
class MyButtonListener extends JPanel implements MouseListener,
MouseMotionListener, ActionListener {
private int mouseX1 = -1; // the mouse Coordinates when pressed
private int mouseY1 = -1;
private int mouseX2 = -1; // mouse coordinates when released
private int mouseY2 = -1;
Color c;
JButton red, green, blue, yellow, orange, black, pink, purple, dot, line, square, circle, ellipse, text;
public MyButtonListener() {
setOpaque(false);
}
public MyButtonListener(JButton red, JButton green, JButton blue, JButton yellow, JButton orange, JButton black, JButton pink, JButton purple,
JButton dot, JButton line, JButton square, JButton circle, JButton ellipse, JButton text){
// Need to communicate references to the buttons in this class
this.red = red;
this.green = green;
this.blue = blue;
this.yellow = yellow;
this.orange = orange;
this.black = black;
this.pink = pink;
this.purple = purple;
this.dot = dot;
this.line = line;
this.square = square;
this.circle = circle;
this.ellipse = ellipse;
this.text = text;
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if(source == red) {
c = Color.RED;
System.out.println(c);
repaint();
}
if(source == green) {
c = Color.GREEN;
System.out.println("Colour is green");
repaint();
}
if(source == blue) {
c = Color.BLUE;
System.out.println("Colour is blue");
repaint();
}
if(source == yellow) {
c = Color.YELLOW;
System.out.println("Colour is yellow");
repaint();repaint();
}
if(source == orange) {
c = (new Color(255, 137, 0));
System.out.println("Colour is orange");
}
if(source == black) {
c = Color.BLACK;
System.out.println("Colour is black");
}
if(source == pink) {
c = Color.PINK;
System.out.println("Colour is pink");
}
if(source == purple) {
c = (new Color(153, 51, 255));
System.out.println("Colour is purple");
}
if(source == dot) {
}
if(source == line) {
}
if(source == square) {
}
if(source == circle) {
}
if(source == ellipse) {
}
if(source == text) {
}
}
@Override
public void mouseClicked(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
}
@Override
public void mousePressed(MouseEvent event)
{
mouseX1 = event.getX();
mouseY1 = event.getY();
}
@Override
public void mouseReleased(MouseEvent event)
{
mouseX2 = event.getX();
mouseY2 = event.getY();
// trigger a call to paintComponent
repaint();
}
@Override
public void mouseDragged(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
}
@Override
public void mouseEntered(MouseEvent event)
{
System.out.println("Enter");
}
@Override
public void mouseExited(MouseEvent event)
{
System.out.println("Exit");
}
@Override
public void mouseMoved(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
}
@Override
public void paintComponent(Graphics g)
{
if (mouseX1 < 0 || mouseY1 < 0)
return;
Graphics2D g2 = (Graphics2D) g;
System.out.println("Drawing a line");
g2.setColor(c);
g2.drawLine(mouseX1, mouseY1, mouseX2, mouseY2);
}
}
public PaintingGUI createToolBar() {
JFrame frame = new JFrame("Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingMenu meMenu = new DrawingMenu();
frame.setJMenuBar(meMenu.createMenuBar());
MyButtonListener panel = new MyButtonListener();
panel.addMouseListener(panel);
panel.addMouseMotionListener(panel);
frame.setContentPane(panel);
panel.setLayout(new BorderLayout());
JToolBar toolBar = new JToolBar("Drag me!");
panel.add(toolBar, BorderLayout.PAGE_END);
// Make some buttons. Here we will paste an image icon on each
JButton red = new JButton("red");
red.setBackground(Color.RED);
red.setContentAreaFilled(false);
red.setOpaque(true);
JButton green = new JButton("gre");
green.setBackground(Color.GREEN);
green.setContentAreaFilled(false);
green.setOpaque(true);
JButton blue = new JButton("blu");
blue.setBackground(Color.BLUE);
blue.setContentAreaFilled(false);
blue.setOpaque(true);
JButton yellow = new JButton("yel");
yellow.setBackground(Color.YELLOW);
yellow.setContentAreaFilled(false);
yellow.setOpaque(true);
JButton orange = new JButton("ora");
orange.setBackground(new Color(255, 137, 0));
orange.setContentAreaFilled(false);
orange.setOpaque(true);
JButton black = new JButton("bla");
black.setBackground(Color.BLACK);
black.setContentAreaFilled(false);
black.setOpaque(true);
JButton pink = new JButton("pin");
pink.setBackground(Color.PINK);
pink.setContentAreaFilled(false);
pink.setOpaque(true);
JButton purple = new JButton("pur");
purple.setBackground(new Color(153, 51, 255));
purple.setContentAreaFilled(false);
purple.setOpaque(true);
ImageIcon temp = new ImageIcon("C:\\Java Programs\\drawing\\dot.JPG");
Image img = temp.getImage();
Image newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon dotpic = new ImageIcon(newimg);
JButton dot = new JButton(dotpic);
temp = new ImageIcon("C:\\Java Programs\\drawing\\line.JPG");
img = temp.getImage();
newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon linepic = new ImageIcon(newimg);
JButton line = new JButton(linepic);
temp = new ImageIcon("C:\\Java Programs\\drawing\\square.JPG");
img = temp.getImage();
newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon squarepic = new ImageIcon(newimg);
JButton square = new JButton(squarepic);
temp = new ImageIcon("C:\\Java Programs\\drawing\\circle.PNG");
img = temp.getImage();
newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon circlepic = new ImageIcon(newimg);
JButton circle = new JButton(circlepic);
temp = new ImageIcon("C:\\Java Programs\\drawing\\ellipse.JPG");
img = temp.getImage();
newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon ellipsepic = new ImageIcon(newimg);
JButton ellipse = new JButton(ellipsepic);
temp = new ImageIcon("C:\\Java Programs\\drawing\\text.PNG");
img = temp.getImage();
newimg = img.getScaledInstance(17,16, java.awt.Image.SCALE_SMOOTH);
ImageIcon textpic = new ImageIcon(newimg);
JButton text = new JButton(textpic);
// Add buttons to toolbar
toolBar.add(red);
toolBar.add(green);
toolBar.add(blue);
toolBar.add(yellow);
toolBar.add(orange);
toolBar.add(black);
toolBar.add(pink);
toolBar.add(purple);
toolBar.add(dot);
toolBar.add(line);
toolBar.add(square);
toolBar.add(circle);
toolBar.add(ellipse);
toolBar.add(text);
// Set a tooltip text for each button
red.setToolTipText("Colour is red");
green.setToolTipText("Colour is green");
blue.setToolTipText("Colour is blue");
yellow.setToolTipText("Colour is yellow");
orange.setToolTipText("Colour is orange");
black.setToolTipText("Colour is black");
pink.setToolTipText("Colour is pink");
purple.setToolTipText("Colour is purple");
dot.setToolTipText("Freehand line");
line.setToolTipText("Draw a straight line");
square.setToolTipText("Draw a Square");
circle.setToolTipText("draw a circle");
ellipse.setToolTipText("Draw an ellipse");
text.setToolTipText("Write something");
// Create an instance of the demo class
PaintingGUI demo = new PaintingGUI();
// Now we need to create an instance of our Button Listener class
MyButtonListener listener =
demo.new MyButtonListener(red, green, blue, yellow, orange, black, pink, purple, dot, line, square, circle, ellipse, text);
// Register action listener defined in the listener class
red.addActionListener(listener);
green.addActionListener(listener);
blue.addActionListener(listener);
yellow.addActionListener(listener);
orange.addActionListener(listener);
black.addActionListener(listener);
pink.addActionListener(listener);
purple.addActionListener(listener);
dot.addActionListener(listener);
line.addActionListener(listener);
square.addActionListener(listener);
circle.addActionListener(listener);
ellipse.addActionListener(listener);
text.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
return demo;
}
}
我尝试了许多不同的方法,并到处寻找解决方案,我对 java 很陌生,所以任何帮助将不胜感激。
最佳答案
您已经创建了两个不同的 MyButtonListener 实例。一个用作 JPanel 和鼠标监听器,另一个用作添加到按钮的操作监听器。
当按下按钮时,第二个实例会处理它并正确设置其颜色,但是执行绘制操作的 MyButtonListener 的第一个实例的 Color 仍然为 null。
尝试使用相同的 MyButtonListener 实例。您需要使用第二个构造函数,但稍微更改它,以便它像无参数构造函数一样进行 setOpaque(true) 调用。
关于java - 如何更改绘制 GUI 中线条的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319289/
我是一名优秀的程序员,十分优秀!