Hi,
I'm rather new to Java and Netbeans so please bare with me. I'm trying to draw 2D graphics on a jPanel and I am having a hard time getting it to work right. I'm not sure if I'm following the correct procedure for this or if I'm just missing something silly.
It seems like my graphic is being erased each time it is resized or if something appears on top of it like a menu bar. I have tried my best to have this redraw each time something happens but now I am at a dead end. Because now if a menu appears it erases the image, if I redraw the image while the menu is active it erases the menu.
When I resize the form with the controls docked something very interesting happens. I put an event that listens if the jScrollPane resizes, which reapplies the 2D graphics to the jPanel. It sometimes does it, sometimes not.
package my.SnapToGrid;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.InputEvent;
public class SnapGrid extends javax.swing.JFrame {
int ViewPortWidth = 1000;
int ViewPortHeight = 2000;
Color ViewPortBackgroundColor = Color.white;
Color ViewPortGridColor = Color.darkGray;
Color ViewPortSnapColor = Color.BLUE;
int GridWidth = 30;
int GridHeight = 30;
int npx, npy;
BufferedImage buf;
public SnapGrid() {
buf = new BufferedImage(ViewPortWidth, ViewPortHeight, BufferedImage.TYPE_INT_RGB);
initComponents();
jplGraphic.setBackground(ViewPortBackgroundColor);
jplGraphic.setPreferredSize(new Dimension(ViewPortWidth,ViewPortHeight));
}
private void jplGraphicMouseMoved(java.awt.event.MouseEvent evt) {
int x = evt.getX(), y = evt.getY();
int mx = x % GridWidth, my = y % GridHeight;
if (mx<GridWidth/2) npx = x - mx;
else npx = x + (GridWidth-mx);
if (my<GridHeight/2) npy = y - my;
else npy = y + (GridHeight-my);
status.setText(npx+", "+npy);
GenerateDrawing();
UpdateDrawing();
}
private void jplGraphicComponentMoved(java.awt.event.ComponentEvent evt) {
UpdateDrawing();
}
private void formComponentShown(java.awt.event.ComponentEvent evt) {
GenerateDrawing();
UpdateDrawing();
}
private void jplGraphicComponentResized(java.awt.event.ComponentEvent evt) {
UpdateDrawing();
}
private void jplGraphicMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
if(evt.getModifiers() == InputEvent.CTRL_MASK) {
java.awt.Toolkit.getDefaultToolkit().beep();
}
else{
Point ViewPortPosition = jscpViewPort.getViewport().getViewPosition();
int UnitsToScroll = evt.getUnitsToScroll();
jscpViewPort.getVerticalScrollBar().getHeight();
if(evt.getModifiers() == InputEvent.SHIFT_MASK) {
ViewPortPosition.x += UnitsToScroll;
if(ViewPortPosition.x < 0)ViewPortPosition.x = 0;
int WidthLimit = ViewPortWidth-jscpViewPort.getHorizontalScrollBar().getWidth();
if(ViewPortPosition.x > WidthLimit)ViewPortPosition.x = WidthLimit;
}
else{
ViewPortPosition.y += UnitsToScroll;
if(ViewPortPosition.y < 0)ViewPortPosition.y = 0;
int HeightLimit = ViewPortHeight-jscpViewPort.getVerticalScrollBar().getHeight();
if(ViewPortPosition.y > HeightLimit)ViewPortPosition.y = HeightLimit;
}
jscpViewPort.getViewport().setViewPosition(ViewPortPosition);
System.out.println("("+ViewPortPosition.x+", " +ViewPortPosition.y+")");
System.out.println(jscpViewPort.getHorizontalScrollBar().getWidth());
}
}
private void jscpViewPortComponentResized(java.awt.event.ComponentEvent evt) {
UpdateDrawing();
}
private void GenerateDrawing(){
Graphics2D g = (Graphics2D)buf.createGraphics();
g.setBackground(ViewPortBackgroundColor);
g.clearRect(0, 0, ViewPortWidth, ViewPortHeight);
g.setColor(ViewPortGridColor);
for (int j=GridHeight;j<ViewPortHeight;j+=GridHeight) {
for (int i=GridWidth;i<ViewPortWidth;i+=GridWidth) {
g.drawLine(i, j, i, j);
}
}
g.setColor(ViewPortSnapColor);
if (npx>=0 && npy>=0 && npx<=ViewPortWidth && npy<=ViewPortHeight) {
g.drawOval(npx-4, npy-4, 8, 8);
}
}
private void UpdateDrawing(){
jplGraphic.getGraphics().drawImage(buf, 0, 0, Color.BLACK, null);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SnapGrid().setVisible(true);
}
});
}
}
I didn't include all of the NetBeans generated code because it was simply too long.