我正在尝试在java中创建一个小型绘图工具。我希望用户绘制的颜色在选择按钮时更改。我试图设置我的按钮来做到这一点。但是当我运行应用程序并选择一个按钮时,颜色不会改变。我该如何解决这个问题?
myJPanel.java
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams) {
var alreadyGoingToLogin = toState.name === "login";
if(alreadyGoingToLogin) {
return
}
...
答案 0 :(得分:0)
使用不用于绘画的图形。
无论如何,不要这样绘制,覆盖paintComponent
方法以使其绘制您喜欢的内容,并在需要绘制时调用repaint
:
public class myJPanel extends JPanel implements MouseMotionListener, ActionListener
{
JButton red, blue, yellow, eraser;
JPanel p1;
Color currentColor;
int x, y;
public myJPanel() {
setBackground(Color.white);
setLayout(new BorderLayout());
p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
yellow = new JButton("Yellow");
eraser = new JButton("Eraser");
p1.add(yellow);
p1.add(eraser);
add(p1,"South");
addMouseMotionListener(this);
}
public void paintComponent(Graphics g) { // draw something on request
g.setColor(currentColor);
g.fillRect(x, y, 5, 5);
}
public void mouseDragged(MouseEvent evt)
{
Point pt = evt.getPoint();
x = pt.x;
y = pt.y;
repaint(); // a new point to draw, please call me back...
}
@Override
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
String choice = event.getActionCommand();
if (obj == yellow) {
currentColor = Color.yellow);
}
if (obj == eraser) {
currentColor = Color.white);
}
}
}