java 2d graphics

  • Thread starter alun Groome via JavaKB.com
  • Start date
A

alun Groome via JavaKB.com

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
 
A

Andrew Thompson

A

alun Groome via JavaKB.com

Hi,

Thanks for the reply. It doesnt even paint on the piano now. I must have done somethine somewhere down the line. I have pasted the below code snippets. So any ideas of what can be done to print the note on the drawStaff screen? Thanks

drawStaff.class

public class drawStaff
extends JPanel {

//Octave 1
private int octave1CFlag = 1;


/**
* Graphics2D variable
*/

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);
//System.out.println("alun draw method");

g2.fill(cNoteOct1);





}




}

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);

/**
* Draw treble staff
*/
// drawStaff(1600, 300, g2);

Point2D staff1Position = new Point2D.Double(0, 100);
Shape staff1 = SonataFont.getGlyphShape(28, 60.0, staff1Position);

g2.fill(staff1);

/**
* Draw bass staff
*/
//drawStaff(1600, 500, g2);

Point2D staff2Position = new Point2D.Double(0, 300);
Shape staff2 = SonataFont.getGlyphShape(28, 60.0, staff2Position);

g2.fill(staff2);

/**
* Draw treble and bass clefs
*/
//Set position for treble cleff
Point2D trebleClefPosition = new Point2D.Double(0.0, 100.0);
//Set position for bass cleff
Point2D bassClefPosition = new Point2D.Double(0.0, 300.0);
Shape trebleClef = SonataFont.getGlyphShape(6, 60.0, trebleClefPosition);
Shape bassClef = SonataFont.getGlyphShape(30, 60.0, bassClefPosition);

g2.fill(trebleClef);
g2.fill(bassClef);


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) {
//code here
}
}
 
A

alun Groome via JavaKB.com

Hi thanks for the reply. Hopefully you can help me further, as nothign seems to be working now!. Just need the note drawn in the drawStaff panel, and not the the javaPiano panel.


javaPiano.java (this passes a message to the drawNote method of drawStaff class saying which note has been pressed.

if (octave1CFlag == 0) {
//set javapiano key to yellow
g2.setColor(Color.yellow);
g2.fill(octave1C);

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


*******************************************************************
drawStaff.java

public class drawStaff
extends JPanel {



/**
* Graphics2D variable
*/

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);

}

}

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);

//here draws the music staff when class first loads
//this bit works fine


}

}
public static void main(String[] args) {
//code here

}
}

cheers alun
 
A

alun Groome via JavaKB.com

Sorry again. I am new to this forum, and only just relaised you wanted a working version of the code. Can you please let me know if you still need this. Realyl appreciate your help on this.

Thanks
 
N

Nigel Wade

alun said:
Hi thanks for the reply. Hopefully you can help me further, as nothign
seems to be working now!. Just need the note drawn in the drawStaff
panel, and not the the javaPiano panel.
javaPiano.java (this passes a message to the drawNote method of drawStaff
class saying which note has been pressed.
if (octave1CFlag == 0) {
//set javapiano key to yellow
g2.setColor(Color.yellow);
g2.fill(octave1C);

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

The reason is here. As Andrew guessed, you are passing the Graphics of the
piano to the drawNote() method. This will result in the note being drawn in
the piano.

drawNote should be made to draw in the Graphics of the staff. If it's class
is derived from a Swing component it can get its own Graphic with
getGraphics(). Otherwise it needs to be told the correct Graphics to use.
 
A

alun Groome via JavaKB.com

thanks for the reply. I have been told about getGraphics(), but I do not know how to use it? I have tried int he drawNote method of drawStaff class

Graphics g = getGraphics();

g.draw bla bla;


it doesnt work though. Am i using it right?

Thanks

Alun
 
A

alun Groome via JavaKB.com

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);

}
 
A

Andrew Thompson

Below is the code for drawStaff class.

Unfortunately, it is still not something I can see on screen, and
I don't have time to go hunting through graphical code..

F:\drawStaff.java:50: cannot find symbol
symbol : variable SonataFont
location: class drawStaff
Shape cNoteOct1 = SonataFont.getGlyphShape(106, 120.0, cPositionOct1);
^
1 error
...Please help!!! this is delaying my development time greatly!!

<drily>
Perhaps you need to use Elance (and offer a lot of money for a rush job),
since we (generally) care not one iota about your lack of forward planning.
</drily>
 
S

sanjay manohar

Try this -
only paint on the staff from the Staff class's paint method.
only paint on the piano from the Piano class's paint method.

Don't call any other class's painting methods from within a paint
routine.
Hints- choose different capitalisation / names for Methods and Classes.
 
T

Thomas Weidenfeller

alun said:
thanks for the reply. I have been told about getGraphics(),

Don't use it!
but I do not know how to use it?

Good, don't use it. See the comp.lang.java.gui FAQ for the reason why
not to use it. Also, reserve an hour or two to read Sun's TSC "Painting"
article. You badly need to learn the basics of the painting mechanism in
AWT/Swing. A pointer is in the FAQ, too.

A link to a copy of the FAQ is in my signature.

/Thomas
 
A

alun Groome via JavaKB.com

thanks for the replys!. I completley undertsand why you say paint in javapiano class from javapiano, and pain in drawstaff from drawstaff class. But the JavaPiano class needs to tell the DrawStaff class a note has been pressed, so it can then draw it!. I have got this working by doing all the painting in the JavaPiano class, but I wanted to have two seperate displays. I wanted the JavaPiano class to only be responsible for painting the keys when they are pressed and to output sound.

cheers
 
S

sanjay manohar

Excellent. This is why Data models come in useful: they store data.
In short, the data object is accessible from your piano and from the
staff.
If a note is pressed in the piano class, it adds this information to
the data object, by calling a method.
The data object then signals the change to the staff, which is forced
to repaint.
The staff's paint method looks at the data, and sees what notes it
needs to paint, and paints them.

Here is a Very simplified version:

class F extends JFrame{
NoteData data=new NoteData();
Staff staff = new Staff();
Piano piano = new Piano();
public F{
//...do init
//add staff, add piano to content pane
staff.data=this.data;
piano.data=this.data;
}
}

class NoteData{
public Vector notes = new Vector();
public void addNote(String notename){
notes.add(notename);
//notify any listeners here - i.e. should trigger Staff to repaint
}
}

class Staff{
NoteData data;
public void paint(Graphics g){
for(int i=0;i<data.notes.size();i++){
// draw each note in the data class
Point2D p = new Point2D.Double(50+i*20,100);
Shape s = SonataFont.getGlyphShape(106, 120, p);
g.fill(cNoteOct1);
}
}
}

class Piano{ // draw the piano and respond to key presses
NoteData data;
void keyPressed(){
data.addNote("octave1C"); // add the note to the data
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top