Skip to main content

Can't run paint method from other method

No replies
aslanali555
Offline
Joined: 2012-11-15
Points: 0

I want to call "paint method" from other method.
It want to "Graphics g" parameter

Codes are below.The problem is in "actionPerformed method".I specified "?" sign.
Waiting for help in an emergency. Thank you in advance...

----------------------------------------------
import java.awt.Graphics;

public interface GrafikCiz {

public void paint(Graphics g);

}
---------------------------------
import java.awt.Graphics;

public class Grafik {

public GrafikCiz grafikciz;

public Grafik(GrafikCiz grafikciz) {
this.grafikciz = grafikciz;
}
}
public void paint(Graphics g) {
grafikciz.paint(g);

}
----------------------------------------------------------------------
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class Sinus extends Applet implements GrafikCiz {

@Override
public void paint(Graphics g) {

final int CYCLE = 4;
final int MAX = 1000;
int x1 = 0;
int x2 = 0;
g.setColor(Color.red);// color for axes
g.drawLine(0, 150, 700, 150);// x-axis
g.drawLine(240, 0, 240, 500);// y-axis
g.drawString("X-Axis", 430, 140);// Label for x-axis
g.drawString("Y-Axis", 200, 270);// Label for y-axis
g.setColor(Color.blue);// color for the sin curve
for (int i = -130; i <= 368; i++) {
x1 = (int) (100 * Math.sin(((i) * 2 * Math.PI * CYCLE) / (MAX)));
x2 = (int) (100 * Math.sin(((i + 1) * 2 * Math.PI * CYCLE) / (MAX)));
g.drawLine(i + 121, x1 + 138, (i + 1) + 121, x2 + 138);
}

}

}
------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class ButtonPanel extends JPanel implements ActionListener {
private JButton sin;
private JButton cos;
private JButton tan;
private JFrame frame;
private JPanel p;

public ButtonPanel() {
sin = new JButton("Sinus");
cos = new JButton("Cosinus");
tan = new JButton("Tanjant");
frame = new JFrame("Fonksiyonları Grafigi");
p = new JPanel();
sin.setVisible(true);
cos.setVisible(true);
tan.setVisible(true);
frame.setVisible(true);
p.setVisible(true);
sin.setSize(50, 30);
cos.setSize(50, 30);
tan.setSize(50, 30);
frame.setSize(300, 200);
frame.add(p);
p.add(sin);
p.add(cos);
p.add(tan);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sin.addActionListener((ActionListener) this);
cos.addActionListener((ActionListener) this);
tan.addActionListener((ActionListener) this);

}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == sin) {

Grafik grafik=new Grafik(new Sinus());
------???????? grafik.paint()//I want to call "paint method"

} else if (source == cos) {
Grafik grafik=new Grafik(new Cosinus());

}

else if (source == tan) {
Grafik grafik=new Grafik(new Tanjant());

}
}
}

public class Client extends javax.swing.JFrame {

public static void main(String[] args) {
ButtonPanel c = new ButtonPanel();

}

}