给定一个JFrame,我通过处理MouseListener来绘制线条。现在我添加了一个按钮,从Color chooser中选择一种颜色。我希望在选择颜色后,我绘制的所有线条都会选择颜色。
以下是我尝试向ButtonListener添加代码的代码,但无法取得任何成功。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myJPanelstd extends JPanel implements MouseMotionListener
{
JButton bmessage;
JPanel p1;
JButton changeColor;
public myJPanelstd(){
setBackground(Color.pink);
setLayout(new BorderLayout());
p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
bmessage = new JButton();
changeColor= new JButton("CHANGE COLOR");
p1.add(bmessage);
p1.add(changeColor);
add(p1,"South");
changeColor.addActionListener(new ButtonListener());
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent evt)
{
Point pt = evt.getPoint();
String sx = "x = " + pt.getX();
String sy = "y = " + pt.getY();
bmessage.setText("you are doing fine");
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Color c = JColorChooser.showDialog(null, "Choose a Color", sampleText.getForeground());
//if (c != null)
//sampleText.setForeground(c);
}
}
public void mouseDragged(MouseEvent evt)
{
bmessage.setText("Nice Drawing!");
Point pt = evt.getPoint();
Graphics gg = getGraphics();
gg.setColor(Color.RED);
gg.fillRect(pt.x,pt.y,5,5);
getGraphics().setColor(Color.RED);
getGraphics().fillRect(pt.x,pt.y,5,5);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
Image myImage = Toolkit.getDefaultToolkit().getImage("images/fred.jpg");
g.drawImage(myImage, 0, 0, this);
g.setColor(Color.yellow);
g.drawString("Draw on Me", 250,150);
}
}
答案 0 :(得分:2)
您需要存储所有绘制的元素并在paintComponent()
中重新绘制它们,否则当您最小化/调整大小/覆盖窗口并发现/等时它们将消失。
例如,您创建的每一行都存储在ArrayList中并在paintComponent()
开头的循环中重新绘制它们,然后当您更改颜色时,所有线条都将以新颜色绘制。
不要忘记在换色后重绘()
简单的伪代码 - 只是为了看到这个想法 - 它不会编译
class A extends JPanel
{
ArrayList<Shape> shapes;
public A()
{
shapes = new ArrayList<Shape>();
}
public void afterMouseRelease();
{
//paint the shape
shapes.add(yourNewShape); // store it for later
}
public void paintComponen(Graphics g)
{
super.paintComponent();
setColor()
for(Shape s : shapes)
{
// paint it again
}
// rest of painting
}
}
前一段时间我在我的应用程序中使用了java.awt.Shape的Shape类和Graphics2D。
答案 1 :(得分:0)
使用JColorChooser.showDailog(-,-);
在动作监听器下选择颜色
使用Java swing
引用样本颜色选择器// ColorPicker.java
// A quick test of the JColorChooser dialog.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorPicker extends JFrame {
public ColorPicker() {
super("JColorChooser Test Frame");
setSize(200, 100);
final Container contentPane = getContentPane();
final JButton go = new JButton("Show JColorChooser");
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color c;
c = JColorChooser.showDialog(
((Component)e.getSource()).getParent(),
"Demo", Color.blue);
contentPane.setBackground(c);
}
});
contentPane.add(go, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
ColorPicker cp = new ColorPicker();
cp.setVisible(true);
}
}