PDA

View Full Version : Java Assignment



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

}

flibitijibibo
April 3rd, 2009, 06:29 PM
From what I see, you call repaint for panel1, but not updatePanel, only for panel2. So, if that's true, the changes are being made, but are never being displayed.

Phopojijo
April 3rd, 2009, 06:42 PM
Yeah I did that too but took it down when I found out the button wasn't even working. (Couldn't even setText() a simple frickin' JLabel)

Never quite understood why I needed to though -- would rendering a bufferedimage to a JPanel require repaint and updatepanel?

((Recoding this shit from scratch, btw -- not sure what the Visual Editor did and didn't do... but I still need to know the right way to do shit otherwise I'll just keep frickin' up.))

MetKiller Joe
April 3rd, 2009, 07:32 PM
Have you tried awt with a frame? It might be simpler than swing.

Arteen
April 3rd, 2009, 11:14 PM
Try changing paintComponents() to paint(), and calling this.repaint() in updatePanel(). I was able to get something displayed that way.

Phopojijo
April 3rd, 2009, 11:23 PM
Have you tried awt with a frame? It might be simpler than swing.Yeah we are explicitly told to use JPanel.

Arteen -- Still not getting much outta that... but I'll keep trying.

Arteen
April 3rd, 2009, 11:32 PM
Really? For me, it made the background dark and drew 4 red pixels on the screen after I pressed the refresh button, which seems to be all that the code is supposed to do at the moment. That didn't happen on your end?



public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempWindow().setVisible(true);
}
});
}Also, what is that TempWindow() object? I forgot to mention that I changed it to new Fractal_GUI5b().setVisible(true);, and I also uncommented the updatePanel() code.

Phopojijo
April 3rd, 2009, 11:50 PM
Solved it... sort of... Graphics2D was buggering up in the paint() call.

Ignoring Graphics2D altogether and just keeping the graphics call helped.

((Obviously bind row and column to some value otherwise the blue pixel won't appear anywhere))


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Scott
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;


public class Fractal_GUI5c extends JFrame {

JPanel jPanel1;
Paints renderBuffer;
JButton jButton1;
JButton jButton2;
JLabel jLabel1;
BufferedImage topFrameBuffer;
int column;
int row;

public Fractal_GUI5c()
{
initialize();
}

public void initialize()
{
//Declarations
jPanel1 = new JPanel();
renderBuffer = new Paints();
jButton1 = new JButton();
jButton2 = new JButton();
jLabel1 = new JLabel();

//Window Settings
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 340);
setResizable(false);
setVisible(true);
setTitle("Loose Screw Fractal Viewer"); //Yes that's what all those LS's meant.
//It's a habit from all the other code I write.
//So that we can figure out who wrote it.

setLayout(new BorderLayout());

//Configure the Panels
jPanel1.setBackground(new Color(216,216,255));
jPanel1.setBorder(new javax.swing.border.LineBorder(new Color(0,0,0),2,true));
jPanel1.setPreferredSize(new Dimension(400,40));
jPanel1.setLayout(new FlowLayout());

renderBuffer.setBackground(Color.BLACK);//To show people that the component just needs refreshing.
//People tend to understand that Black = Buffering from Youtube and such.
//Ahhh Ergonomics.

//Add the Panels
this.add(jPanel1, BorderLayout.PAGE_END);
this.add(renderBuffer, BorderLayout.CENTER);

//Configure and add the buttons and labels.
jButton1.setText("Refresh");
jPanel1.add(jButton1);

jLabel1.setText(" Click and Drag to Zoom Box ");
jPanel1.add(jLabel1);

jButton2.setText("Zoom Out");
jPanel1.add(jButton2);

//Button Functionality
renderBuffer.repaint();

}//end initialize.

private class Paints extends JPanel
{
public Paints()
{
super();
}

//Stuff for the Paints class here.
public void paint(Graphics g)
{
//super.paint(g);
topFrameBuffer = new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);
topFrameBuffer.setRGB(column,row,Color.BLUE.getRGB ());
g.drawImage(topFrameBuffer, WIDTH, WIDTH, rootPane);
}

}

public static void main(String[] args) {
new Fractal_GUI5c();
}

}
Edit:

Really? For me, it made the background dark and drew 4 red pixels on the screen after I pressed the refresh button, which seems to be all that the code is supposed to do at the moment. That didn't happen on your end?



public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempWindow().setVisible(true);
}
});
}Also, what is that TempWindow() object? I forgot to mention that I changed it to new Fractal_GUI5b().setVisible(true);, and I also uncommented the updatePanel() code.

Yeah it's possible it was a typo... but in my rewrite (5c) it appears to work if I just drop Graphics2D