Phopojijo
April 3rd, 2009, 05:00 PM
Hey guys.
So my assignment is to draw fractals onto a Java Window.
No matter what I tried... nothing would paint.
Then I finally tried to use the buttons to change the jLabel's text... that didn't even work.
Here's all my code... I'll try to see if I can make it work but it's already been two solid days.
Also -- the GUI code is messy because it was made in the visual editor.
Edit: Hmm -- the exact same code works in the visual editor... but not outside... even though NetBeans says you can copy paste into an empty class and have it work.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Generated this code from the Netbeans visual editor, then edited
* the output so that I could actually render to the jPanel.
* @author Scott
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Fractal_GUI5b extends javax.swing.JFrame {
/** Creates new form TempWindow */
public Fractal_GUI5b() {
initComponents();
}
public class Paints extends JPanel
{
final static int MAX_ITERATIONS = 32; //Maximum number of iterations.
final static double ESCAPE_MODULUS = 2.0; //Maximum modulus.
int height;
int width;
double xMax, yMax = 2.26; //Initial BottomRight Corner
double xMin, yMin = -2.24; //Initial TopLeft Corner
BufferedImage topFrameBuffer;
Graphics2D topFrameBufferHelper;
public Paints()
{
super();
}
public void updatePanel()
{
jLabel1.setText("Helllo");
//topFrameBuffer = new BufferedImage(800,550,BufferedImage.TYPE_INT_RGB);
//topFrameBuffer.setRGB(40,40,Color.RED.getRGB());
//topFrameBuffer.setRGB(41,40,Color.RED.getRGB());
//topFrameBuffer.setRGB(40,41,Color.RED.getRGB());
//topFrameBuffer.setRGB(41,41,Color.RED.getRGB());
//topFrameBufferHelper = (Graphics2D) topFrameBuffer.getGraphics();
//jPanel2.paintComponents(topFrameBufferHelper);
//topFrameBufferHelper.dispose();
//jPanel2.
}
public void paintComponents(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(topFrameBuffer, null, 0, 0);
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new Paints();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setBackground(new java.awt.Color(226, 226, 255));
jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 50));
jButton1.setText("Refresh");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Zoom Out");
jLabel1.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
jLabel1.setText("Click and Drag to Zoom Box");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(12, Short.MAX_VALUE))))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("Goodbye!");
jLabel1.repaint();
jPanel2.updatePanel();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
public javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
public Paints jPanel2;
// End of variables declaration
}
So my assignment is to draw fractals onto a Java Window.
No matter what I tried... nothing would paint.
Then I finally tried to use the buttons to change the jLabel's text... that didn't even work.
Here's all my code... I'll try to see if I can make it work but it's already been two solid days.
Also -- the GUI code is messy because it was made in the visual editor.
Edit: Hmm -- the exact same code works in the visual editor... but not outside... even though NetBeans says you can copy paste into an empty class and have it work.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Generated this code from the Netbeans visual editor, then edited
* the output so that I could actually render to the jPanel.
* @author Scott
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Fractal_GUI5b extends javax.swing.JFrame {
/** Creates new form TempWindow */
public Fractal_GUI5b() {
initComponents();
}
public class Paints extends JPanel
{
final static int MAX_ITERATIONS = 32; //Maximum number of iterations.
final static double ESCAPE_MODULUS = 2.0; //Maximum modulus.
int height;
int width;
double xMax, yMax = 2.26; //Initial BottomRight Corner
double xMin, yMin = -2.24; //Initial TopLeft Corner
BufferedImage topFrameBuffer;
Graphics2D topFrameBufferHelper;
public Paints()
{
super();
}
public void updatePanel()
{
jLabel1.setText("Helllo");
//topFrameBuffer = new BufferedImage(800,550,BufferedImage.TYPE_INT_RGB);
//topFrameBuffer.setRGB(40,40,Color.RED.getRGB());
//topFrameBuffer.setRGB(41,40,Color.RED.getRGB());
//topFrameBuffer.setRGB(40,41,Color.RED.getRGB());
//topFrameBuffer.setRGB(41,41,Color.RED.getRGB());
//topFrameBufferHelper = (Graphics2D) topFrameBuffer.getGraphics();
//jPanel2.paintComponents(topFrameBufferHelper);
//topFrameBufferHelper.dispose();
//jPanel2.
}
public void paintComponents(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(topFrameBuffer, null, 0, 0);
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new Paints();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setBackground(new java.awt.Color(226, 226, 255));
jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 50));
jButton1.setText("Refresh");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Zoom Out");
jLabel1.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
jLabel1.setText("Click and Drag to Zoom Box");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(12, Short.MAX_VALUE))))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("Goodbye!");
jLabel1.repaint();
jPanel2.updatePanel();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
public javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
public Paints jPanel2;
// End of variables declaration
}