Reputation: 1
I was wondering what went wrong. I've done this before but for some reason I can't get this to work this time.
I designed the project in Netbeans. The most relevant part is the method initTiras, which is suppossed to draw something, but it doesn't. I included the code generated by NetBeans.
Here's the most simplified version of the code, and still it paints nothing.
So I created a new Frame in the proyect. Then I put a JPanel in it (in desing mode). The JPanel's called ElPanel and the JFrame LaFrame.
Here's the main class:
package ElPackage;
public class Resistores {
public static void main(String[] args) {
LaFrame UnaFrame = new LaFrame();
UnaFrame.setVisible(true);
}
}
Inside the LaFrame class:
package ElPackage;
import java.awt.Color;
import java.awt.Graphics;
public class LaFrame extends javax.swing.JFrame {
public LaFrame () {
initComponents();
initTiras();
}
public void initTiras() {
Graphics LasG = ElPanel.getGraphics();
LasG.setColor(Color.WHITE);
LasG.fillRect(10, 10, 30, 30);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ElPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
ElPanel.setBackground(new java.awt.Color(51, 0, 51));
ElPanel.setForeground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout ElPanelLayout = new javax.swing.GroupLayout(ElPanel);
ElPanel.setLayout(ElPanelLayout);
ElPanelLayout.setHorizontalGroup(
ElPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
ElPanelLayout.setVerticalGroup(
ElPanelLayout.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(ElPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ElPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JPanel ElPanel;
// End of variables declaration
}
Upvotes: 0
Views: 574
Reputation: 57381
Override paintComponent(Graphics g)
method of elPanel and add your code there. See for example
Use java naming convention - variables should start from lowercase letter.
Upvotes: 3