This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/10/27

I need help with java Graphics

Kartoffelbrot Kartoffelbrot

2013/10/27

#
I want to paint my MandelbrotSet, that I've made yesterday, now outside of Greenfoot. Therefore I have a JFrame with a JLabel with a size of 600x400. Now my problem is, that I use some classes wrong, but I don't know how. The painting of the set works in Greenfoot. Here is the important part of the code (I'm unexperienced in tis materia):
public void drawM(double xMin, double xMax, double yMin, double yMax){
        calcIterationLimit();
        double w = (double) disp.getWidth();
        double h = (double) disp.getHeight();
        double dx = xMax-xMin;
        double dy = yMax-yMin;
        Graphics b = disp.getGraphics();//IMPORTANT
        for(double x = 0; x < disp.getWidth(); x++){
            for(double y = 0; y < disp.getHeight(); y++){
                double mx = ((dx/w)*x)+xMin;
                double my = ((-dy/h)*y)+yMax;
                Color color = calcColorFor(mx, my);
                b.setColor(color);//IMPORTANT
                b.drawRect((int) x, (int) y, (int) x, (int) y);//IMPORTANT
            }
        }
        ImageIcon icon = new ImageIcon();//IMPORTANT
        icon.paintIcon(disp, b, disp.getWidth(), disp.getHeight());//IMPORTANT
        disp.setIcon(icon);//IMPORTANT
    }
disp is the JLabel. There is now failure in the Mandelbrot set calculating. It has to be in the usage of the java classes.
SPower SPower

2013/10/27

#
Well, your approach is wrong. When you want to redraw in Java (in this case), you call repaint() and implement the method paintComponent(Graphics graphics), and draw there. Example:
// in your loop, where you want to redraw every frame:
repaint();

// implement this method in the object's class you want to redraw
public void paintComponent(Graphics graphics) {
    // redraw here using graphics
    // you might as well cast it into a Graphics2D:
    Graphics2D g = (Graphics2D) g;
}
Kartoffelbrot Kartoffelbrot

2013/10/27

#
Do I use ImageIcon correctly? Are my sizes correct? What should be the repaint method?
SPower SPower

2013/10/27

#
Again, your approach is just wrong. It's not that you're using one class wrong here, but in my opinion it should just be rewritten here. I'll give you an example:
// the main cycle:
public static void main(String[] args)
{
    JFrame frame = new MyFrame(); // coming up
    frame.setVisible(true);
}

// in MyFrame
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener
{
private Timer timer;
public MyFrame()
{
setSize(new Dimension(600,400));
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

timer = new Timer(1000/60,this); // 60 times per second
timer.start();
}
public void actionPerformed(ActionEvent e)
{
    repaint(); // redraw every frame
}

public void paintComponent(Graphics b)
{
// your drawing code here
}
Do you see what I'm getting at? Also, you should not do a lot of calculations in paintComponent: you can do that in actionPerformed. But beware, this might not be executed on the main thread (welcome to the painful world of multi-threading). As a fistrule: all the graphics stuff on the main thread (and thus in paintComponent), and calculations preferably not in paintComponent.
You need to login to post a reply.