Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java 2d graphics

 
alun groome
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im having some right hassel with java 2d graphics.
One class (javaPiano.java) I have a Jpanel that draws a virtual keyboard, and play notes when you press the keys. I have a sperate class that is another jpanel which has music lines (drawStaff.java).

in the drawStaff class I have a method

public void drawNote (String noteName, Graphics2d g1) {

//code here
}

from the javapiano class I call the drawNote method by saying

String a="octave1C";
drawStaff.drawNote(a,g2);

the problem I am having, its drawing the note on the javapiano screen, rather than the drawStaff screen. And i really havent a clue why. I would appreciate some help on this please.

Thanks
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of g2 is the Graphics representation of javapiano. You need to set g2 to the Graphics instance for drawStaff.
 
alun groome
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have done this. This is the full code for drawNote in class drawStaff...


private Graphics2D g2;

public drawStaff() {
super();
}

public void drawNote(String NoteIn, Graphics2D g1) {

g2 = g1;
String noteIn = NoteIn;

if (noteIn == "octave1C") {
Point2D cPositionOct1 = new Point2D.Double(50, 100.0);
Shape cNoteOct1 = SonataFont.getGlyphShape(106, 120.0, cPositionOct1);


g2.fill(cNoteOct1);

}

}
 
alun groome
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code for drawStaff class. The pressed note in javaPiano class gets passed to the drawNote() method of drawStaff class. If the note being passed in matches the String value "octave1C", a flag gets set, setting the flag to 0.

I have then asked the drawStaff panel to repaint its self. When the repaint is called it shoul execute the paintComponent method of class drawStaff (the class from which repaint() is called), seeing that the flag has been set to 0 this time, and the should draw the note to the drawStaff panel. But it doesnt!!! Please help!!! this is delaying my development time greatly!!

thanks in advance


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

public class drawStaff
extends JPanel {

//Octave 1
private int octave1CFlag = 1;


/**
* Graphics2D variable
*/

private Graphics2D g2;

public drawStaff() {
super();
}

public void drawNote(String NoteIn) {


String noteIn = NoteIn;

if (noteIn == "octave1C") {


octave1CFlag = 0;
drawStaff myStaff = new drawStaff();
myStaff.repaint();

}

}

public void paintComponent(Graphics g) {
// superclass erase the old contents
super.paintComponent(g);
/**
* Set up java 2d graphics and set default colour to black
*/
g2 = (Graphics2D) g;
g2.setColor(Color.black);

if (octave1CFlag == 0) {
Point2D cPositionOct1 = new Point2D.Double(50, 100.0);
Shape cNoteOct1 = SonataFont.getGlyphShape(106, 120.0, cPositionOct1);
System.out.println("alun paint method");

g2.fill(cNoteOct1);

}

}

public static void main(String[] args) {
JPanel myPanel = new drawStaff();
JFrame myFrame = new JFrame();
myFrame.setContentPane(myPanel);
myFrame.setSize(800, 400);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);

}
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why are you creating a new instance of drawStaff?
[ January 21, 2005: Message edited by: Joe Ess ]
 
alun groome
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
It was suggested iin another forum to do that. Anyhow it doesnt work. Do you have any suggesitons for me?

thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic