我不確定如何存儲使用類型形狀的一個數組列表的多個不同的形狀。這是主要的。存儲形狀在JComponent中
public class A1 {
public static boolean rDraw = false;
public static boolean eDraw = false;
public static boolean lDraw = false;
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton rect = new JButton("Rectangle");
rect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==rect){
rDraw = true;
eDraw = false;
lDraw = false;
}
}
});
JButton ellipse = new JButton("Ellipse");
ellipse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==ellipse){
rDraw = false;
eDraw = true;
lDraw = false;
}
}
});
JButton edge = new JButton("Edge");
edge.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == edge){
rDraw = false;
eDraw = false;
lDraw = true;
}
}
});
JButton label = new JButton("Label");
JTextField labelField = new JTextField(20);
JPanel panel = new JPanel();
panel.add(rect);
panel.add(ellipse);
panel.add(edge);
panel.add(label);
panel.add(labelField);
frame.add(panel,BorderLayout.NORTH);
JComponent canvas = new Canvas();
frame.add(canvas,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setTitle("Graph Draw");
frame.setVisible(true);
}
}
在我的畫布中,我不確定如何使用drawShape?以及在paintComponent中放置什麼來存儲形狀。
public class Canvas extends JComponent {
private Point p,p2;
private Rectangle r;
private Ellipse2D ep;
private Line2D l;
ArrayList<Shape> shapes = new ArrayList<Shape>();
public Canvas(){
class MyMouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
p = e.getPoint();
r = new Rectangle(p.x, p.y, p.x - p.x, p.y - p.y);
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
p = e.getPoint();
ep = new Ellipse2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
p = e.getPoint();
l = new Line2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
if (r.width != 0 || r.height != 0)
{
addRectangle(r);
}
r = null;
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
if (ep.getWidth() != 0 || ep.getHeight() != 0){
addEllipse(ep);
}
ep = null;
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
if (l.getP1() != null || l.getP2() != null){
addLine(l);
}
l = null;
}
}
}
class MyMouseMotionListener implements MouseMotionListener{
@Override
public void mouseDragged(MouseEvent e) {
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
int x = Math.min(p.x, e.getX());
int y = Math.min(p.y, e.getY());
int width = Math.abs(p.x - e.getX());
int height = Math.abs(p.y - e.getY());
r.setBounds(x, y, width, height);
repaint();
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
int x = Math.min(p.x, e.getX());
int y = Math.min(p.y, e.getY());
int width = Math.abs(p.x - e.getX());
int height = Math.abs(p.y - e.getY());
ep.setFrame(x, y, width, height);
repaint();
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
p2 = e.getPoint();
l.setLine(p, p2);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
MouseListener mListener = new MyMouseListener();
addMouseListener(mListener);
MouseMotionListener mmListener = new MyMouseMotionListener();
addMouseMotionListener(mmListener);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Shape shape:shapes){
}
if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
g2d.draw(r);
}
else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
g2d.draw(ep);
}
else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
g2d.draw(l);
}
}
public void addRectangle(Rectangle rect){
if(r != null){
Rectangle cR = new Rectangle(rect);
shapes.add(cR);
repaint();
}
}
public void addEllipse(Ellipse2D ellipse){
if (ep != null){
Ellipse2D cEp = new Ellipse2D.Double(ellipse.getX(),ellipse.getY(),ellipse.getWidth(),ellipse.getHeight());
shapes.add(cEp);
repaint();
}
}
public void addLine(Line2D line){
if(l != null){
Line2D cL = new Line2D.Double(line.getP1(), line.getP2());
shapes.add(cL);
repaint();
}
}
}
'「...以及在paintComponent中放置什麼來存儲形狀。」 - 這不應該是paintComponent方法的一部分。此方法用於繪製組件上的項目,僅此而已。它不應該用於程序邏輯。用於存儲形狀的代碼應該在您的鼠標偵聽器類型代碼中,而不是在paintComponent中。 –
在paintComponent方法的for循環中,應該有'g2d.draw(shape);'。 –
for循環後的paintComponent方法中的所有代碼都應該被刪除。 –