Bom gente estou com um problema aqui, vejam que nesta imagem estou utilizando o Windows 7:
Parece que depois do click do mouse o repaint() não é só do jPanel, ele pega de tudo ( Bug utilizando o Windows com a mesma compilação ):
Nesta outra estou utilizando o Ubuntu 10.10 Maverick Meerkat:
Class CadPanel:
[code]package com.cad;
import java.awt.Color;
public class CadPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Color lineColor;
private Point markerPoint;
private Queue<Line> lines;
public CadPanel() {
setOpaque(true);
setBorder(new LineBorder(new Color(0, 0, 0)));
setBackground(Color.WHITE);
setLayout(null);
lines = new LinkedList<Line>();
lines.add(new Line(new Point(1, 1), new Point(1, 1)));
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
lines.peek().setPoint1(new Point(e.getX(), e.getY()));
}
@Override
public void mouseReleased(MouseEvent arg0) {
lines.peek().setPoint2(new Point(arg0.getX(), arg0.getY()));
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
markerPoint = new Point(arg0.getX(), arg0.getY());
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(lineColor);
try {
if (Screen.d == (Draw.LINE)) {
g.setColor(lineColor);
g.drawLine(lines.peek().getPoint1().x, lines.peek().getPoint1().y, lines.peek().getPoint2().x, lines.peek().getPoint2().y);
} else if (Screen.d == Draw.RECTANGLE) {
Rectangle rectangle = new Rectangle(new Point(lines.peek().getPoint1().x, lines.peek().getPoint1().y), new Point(lines.peek().getPoint2().x, lines.peek().getPoint2().y));
rectangle.drawCordinates();
g.drawRect(rectangle.getPointx(), rectangle.getPointy(), rectangle.getWidth(), rectangle.getHeight());
}else if(Screen.d == Draw.ELIPSE){
Elipse elipse = new Elipse(new Point(lines.peek().getPoint1().x, lines.peek().getPoint1().y), new Point(lines.peek().getPoint2().x, lines.peek().getPoint2().y));
elipse.drawCordinates();
g.drawArc(elipse.getPointx(), elipse.getPointy(), elipse.getWidth(), elipse.getHeight(), 0, 360);
}
markerPoint = new Point(lines.peek().getPoint2().x, lines.peek().getPoint2().y);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (RuntimeException e) {
throw new RuntimeException(e);
}
}
public void setLineColor(Color c) {
this.lineColor = c;
}
public String getCordinatesAsString() {
return markerPoint.x + ":" + markerPoint.y;
}
}[/code]
Class Screen:
[code]package com.cad;
import java.awt.EventQueue;
public class Screen {
private JFrame frmMinicadMax;
private JFileChooser fileChooser;
private JLabel cordinatesLabel;
private CadPanel panel = new CadPanel();;
public static Draw d;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Screen window = new Screen();
window.frmMinicadMax.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Screen() {
initialize();
}
private void initialize() {
frmMinicadMax = new JFrame();
frmMinicadMax.setIconImage(Toolkit.getDefaultToolkit().getImage(Screen.class.getResource("/icon.png")));
frmMinicadMax.setTitle("Mini-Cad - Max Lider");
frmMinicadMax.setBounds(100, 100, 800, 600);
frmMinicadMax.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMinicadMax.getContentPane().setLayout(null);
JPanel sideMenuPanel = new JPanel();
sideMenuPanel.setBounds(12, 12, 120, 219);
frmMinicadMax.getContentPane().add(sideMenuPanel);
sideMenuPanel.setLayout(null);
JButton jBLine = new JButton("");
jBLine.setToolTipText("Line");
jBLine.setIcon(new ImageIcon(Screen.class.getResource("/line.png")));
jBLine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
d = Draw.LINE;
}
});
jBLine.setBounds(14, 49, 94, 23);
sideMenuPanel.add(jBLine);
JButton jBRect = new JButton("");
jBRect.setToolTipText("Rectangle");
jBRect.setIcon(new ImageIcon(Screen.class.getResource("/rectangle.png")));
jBRect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d = Draw.RECTANGLE;
}
});
jBRect.setBounds(14, 83, 94, 23);
sideMenuPanel.add(jBRect);
JButton jBArrow = new JButton("Arrow");
jBArrow.setToolTipText("Arrow pointer");
jBArrow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d = Draw.ELIPSE;
}
});
jBArrow.setBounds(14, 11, 94, 23);
sideMenuPanel.add(jBArrow);
JButton jBElipse = new JButton("");
jBElipse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d = Draw.ELIPSE;
}
});
jBElipse.setIcon(new ImageIcon(Screen.class.getResource("/elipse.png")));
jBElipse.setBounds(14, 118, 96, 23);
sideMenuPanel.add(jBElipse);
cordinatesLabel = new JLabel("");
cordinatesLabel.setBounds(709, 516, 65, 14);
frmMinicadMax.getContentPane().add(cordinatesLabel);
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent arg0) {
cordinatesLabel.setText(panel.getCordinatesAsString());
}
});
panel.setBounds(183, 11, 578, 494);
frmMinicadMax.getContentPane().add(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JMenuBar menuBar = new JMenuBar();
frmMinicadMax.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNew = new JMenuItem("New");
mnFile.add(mntmNew);
JMenuItem mntmOpen = new JMenuItem("Open");
mntmOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileChooser = new JFileChooser("C:");
fileChooser.showDialog(frmMinicadMax, "Open");
fileChooser.setVisible(true);
}
});
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mnFile.add(mntmExit);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmChangeStrokesColor = new JMenuItem("Change Strokes Color");
mntmChangeStrokesColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
mnEdit.add(mntmChangeStrokesColor);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
JMenuItem mntmAbout = new JMenuItem("About");
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Mini-Cad\n");
}
});
JMenuItem mntmContact = new JMenuItem("Contact");
mntmContact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "");
}
});
mnHelp.add(mntmContact);
mnHelp.add(mntmAbout);
}
}
[/code]
[size=18]Então por favor me ajudem a encontrar o que está errado…[/size]