我有一个简单的程序来编辑用形状绘制的图像。我有一个显示“面部”的JFrame,另一个显示工具栏。我试图链接它,以便'face'类中的选定形状可以通过工具栏上的按钮进行操作。我有以下lsitener:
(修改)
以下是我提供的更新内容...我试图实现它但仍然没有得到它(对不起批量代码):
public class FaceClass extends JFrame {
private static Face face;
//Face face = new Face();//create a face object
private JScrollBar jscbHort = new JScrollBar (JScrollBar.HORIZONTAL);
private MessagePanel messagePanel = new MessagePanel ("Face Class");
public FaceClass () {
this.face = new Face ();
JButton colorChange = new JButton ("Change Color");
//add buttons to panel
JPanel panel = new JPanel ();
panel.add(messagePanel, BorderLayout.CENTER);
panel.add(colorChange);
add(jscbHort, BorderLayout.SOUTH);
add(panel);
//Register Listeners
ColorListenerClass listener = new ColorListenerClass();
colorChange.addActionListener(listener);
jscbHort.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
double value = jscbHort.getValue(); //returns current value
of scrollbar 0-100
double maximumValue = jscbHort.getMaximum();
double newX = (value * messagePanel.getWidth() /
maximumValue);
messagePanel.setXCoordinate((int)newX);
}//end adjustment Value Changed
});//end adjustment listener */
//register mouse click activity
addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged (MouseEvent event) {
face.selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed (MouseEvent event) {
face.selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
}//end cons
/*************EnlargeListener Class**********************************/
class ColorListenerClass implements ActionListener{
@Override// necessary to respond to the event
public void actionPerformed (ActionEvent e) {
Face face = face.getFace();//THIS LINE KEEPS GIVING ME ERRORS
//System.out.println("Color Button Clicked");
if (face.getSelected() == face.getHead()){
System.out.println("Face is selected");
}//end if
else if (face.getSelected() == face.getMouth()){
System.out.println("Mouth is selected");
}//end if
else if (face.getSelected() == face.getEyeLeft() || face.getSelected() ==
face.getEyeRight()){
System.out.println("Eyes are selected");
}//end if
else
System.out.println("Nothing is selected");
}//end actionPerformed
}//end OKListenerClass
//////////////MAIN////////////////////////////////////////
public static final void main (String[] args) {
//draw face panel
//Face face = new Face();
JFrame frame = new JFrame();
frame.setTitle("Mugshot");
frame.add(face);
frame.setSize(500,400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//draw toolbar panel
JFrame frame1 = new FaceClass ();
frame1.setTitle("Toolbar");
frame1.setSize(200,150);
frame1.setLocation(200,100);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.setVisible(true);
}//end main
}//end face class
和'面子':
public class Face extends JPanel{
///Instance Variables
private final Shape head = new Ellipse2D.Float(100, 20, 300, 300);//head component
private final Shape eyeLeft = new Ellipse2D.Float(152,85, 80, 55);//left eye
private final Shape eyeRight = new Ellipse2D.Float(265,85, 80, 55);//right eye
private final Shape pupilRight = new Ellipse2D.Float(285, 85, 40, 55);//right pupil
private final Shape pupilLeft = new Ellipse2D.Float(172, 85, 40, 55);//left pupil
private final Shape mouth = new Rectangle2D.Float(180, 230, 140, 20);//mouth
private Shape selected = null;
private Face face;
public Face () {
this.face = face;
//register mouse click activity
addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
}
protected void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D)g;
graphics.setColor((selected == head) ? Color.CYAN : Color.GREEN);
graphics.fill(head);
graphics.setColor((selected == mouth) ? Color.YELLOW : Color.RED);
graphics.fill(mouth);
graphics.setColor((selected == eyeLeft || selected == eyeRight) ? Color.RED : Color.WHITE);
graphics.fill(eyeLeft);
graphics.fill(eyeRight);
graphics.setColor(Color.BLACK);
graphics.fill(pupilLeft);
graphics.fill(pupilRight);
g.drawLine(220, 185, 270, 185);
g.drawLine(220, 185, 260, 130);
}//end pC
public Face getFace() {
return face;
}
public Shape getSelected() {
return selected;
}//end getSelected
public void setSelected(Shape selected) {
this.selected = selected;
}//end setSelected
public Shape getHead() {
return head;
}//end getFace
public Shape getEyeLeft() {
return eyeLeft;
}//end getEyeLeft
public Shape getEyeRight() {
return eyeRight;
}//end getRightEye
public Shape getMouth() {
return mouth;
}//end getMouth
public void selectShapeUnder (int x, int y) {
Shape oldSelected = selected;
if (eyeLeft.contains(x, y)){
selected = eyeLeft;
}//end if
else if (eyeRight.contains(x, y)){
selected = eyeRight;
}//end else if
else if (mouth.contains(x, y)){
selected = mouth;
}//end else if
else if (head.contains(x, y)) {
selected = head;
}//end else if
else
selected = null;
if (selected != oldSelected)
repaint();
}//end selectShapeUnder
}//end Face class
答案 0 :(得分:1)
主要问题似乎是您在Face
中创建了新的ActionListener
对象。所以,通过这个:
然而,在工具栏按钮的监听器内部,您正在创建一个新的Face
,该方法是该方法的本地方法(尚未选择任何内容),并将其视为Face
您在步骤1中修改过。
您可能希望存储对作为类字段实际显示在帧中的Face
的引用。如果您实际上有两个不同的框架包含工具栏和面部,那么您需要在包含可以从工具栏框架调用的面部的框架上使用getFace()
方法。
修改强>
为了解决您的评论,这实际上取决于您的计划的结构。最简单的例子如下:
class FaceFrame extends JFrame {
private Face face;
public FaceFrame() {
super();
this.face = new Face();
}
public Face getFace() {
return face;
}
}
然后在你的另一帧:
class ToolbarFrame extends JFrame implements ActionListener {
private FaceFrame faceFrame;
...
public ToolbarFrame() {
super();
this.faceFrame = new FaceFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
...
Face face = faceFrame.getFace();
if (face.getSelected() == face.getFace()){
System.out.println("Face is selected");
}
}
}
这可能不是你正在做的最佳设计,但至少应该说明我在说什么。