Whats wrong with my paint component?
I'm quite new to this so please be gentle. I've been trying to creat a custom component that does some custom painting, i have attempted but have failed, i do not see what is wrong with the code, as far as i know its how it should be. I use the validate method to no avail. What does work is when i call the paintComponent() method directly, but as far as i know that is not the best way of doing things.
package javaapplication5;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
publicclass VertexViewextends JComponent{
//private VertexModel model;
private RenderingHints renderHints =new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
private Point location;
privateint width = 10;
privateint height = 10;
privatefinal Color borderColor =new Color(255,255,255);
privatefinal Color fillColor =new Color(70,130,214);
private Dimension dimension =new Dimension(height,width);
public VertexView(){
}
public VertexView(Point p){
location=p;
repaint();
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHints(renderHints);
g2D.setPaint(fillColor);
Rectangle2D e =new Rectangle2D.Double(location.getX(),location.getY(),(double)width,
(double)height);
g2D.fill(e);
g2D.setStroke(new BasicStroke(3));
g2D.setPaint(borderColor);
g2D.draw(e);
}
}
-
package javaapplication5;
publicclass MainFrameextends javax.swing.JFrame{
public MainFrame(){
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
privatevoid initComponents(){
jPanel1 =new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.addMouseListener(new java.awt.event.MouseAdapter(){
publicvoid mouseClicked(java.awt.event.MouseEvent evt){
jPanel1MouseClicked(evt);
}
});
javax.swing.GroupLayout jPanel1Layout =new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout =new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
privatevoid jPanel1MouseClicked(java.awt.event.MouseEvent evt){
VertexView v =new VertexView(evt.getPoint());
jPanel1.add(v);
jPanel1.validate();
//v.paintComponent(this.getGraphics());
}
publicstaticvoid main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable(){
publicvoid run(){
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
I just need to know what is wrong with this code.
Message was edited by:
Carpediem

