Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2012, 06:17 AM
aslanali555 aslanali555 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 aslanali555 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 25 sec
Reputation Power: 0
Homework - Can't call "paint" method from other method

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 with "?" 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();

}

}

Reply With Quote
  #2  
Old November 17th, 2012, 09:31 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,813 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 7 h 50 m 44 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
You don't call paint yourself. You ask the applet to call it by calling repaint()

Reply With Quote
  #3  
Old November 17th, 2012, 09:35 AM
aslanali555 aslanali555 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 aslanali555 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by bullet
You don't call paint yourself. You ask the applet to call it by calling repaint()



I try to repaint() but was not. Did you explain how use repaint() from code

Reply With Quote
  #4  
Old November 17th, 2012, 01:53 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Quote:
I try to repaint() but was not

Can you post the code that shows how you called repaint()?
The repaint method tells the java program to call the paint() method some time in the near future. Calling repaint() is the normal way to have the paint() method called.

Please edit your post and wrap the code in code tags.

Reply With Quote
  #5  
Old November 17th, 2012, 03:46 PM
aslanali555 aslanali555 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 aslanali555 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Can you post the code that shows how you called repaint()?
The repaint method tells the java program to call the paint() method some time in the near future. Calling repaint() is the normal way to have the paint() method called.

Please edit your post and wrap the code in code tags.


I want to run "paint method" when I pressed "sinus button"
But I can't call the "paint method" because.It wants the parameter(Graphics g).What I should write the parameter while I call the "paint method".

Reply With Quote
  #6  
Old November 17th, 2012, 03:49 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Call the repaint() method. Java will then call the paint() method with the correct Graphics object. You can't call the paint() method directly with the correct Graphics object.



Please edit your post and wrap the code in code tags.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Can't call "paint" method from other method

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap