Re: a Problem with java 2D
Daniel Pitts wrote:
(OP)
the following is my code (without import statements):
..hmm. That code was frustratingly close to being an SSCCE*.
The appropriate way to draw graphics is to create your own JComponent
subclass which override void paintComponent(Graphics g), and in that
method use the graphics object thats passed in.
..or to put that an SSCCE way.
<sscce>
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class NewJFrame extends JFrame {
private JPanel pane = null;
/** Creates new instance */
public NewJFrame() {
initComponents();
}
private void initComponents() {
Dimension min = new Dimension(300,300);
setMinimumSize(min);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pane = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw a line on JPanel
Graphics2D canvas = (Graphics2D)g;
canvas.setPaint(Color.BLUE);
canvas.draw(new
Line2D.Float(
1,1,
getWidth()/2,
getHeight()/2));
}
};
setContentPane(pane);
pane.setBackground(new Color(255, 204, 255));
pane.setForeground(new Color(51, 51, 255));
pack();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
</sscce>
* <http://www.physci.org/codes/sscce.html>
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1