aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes java 2d graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "java 2d graphics" Watch "java 2d graphics" New topic
Author

java 2d graphics

alun groome
Greenhorn

Joined: Jan 21, 2005
Posts: 21
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

Joined: Oct 29, 2001
Posts: 8718
    
    6

The value of g2 is the Graphics representation of javapiano. You need to set g2 to the Graphics instance for drawStaff.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
alun groome
Greenhorn

Joined: Jan 21, 2005
Posts: 21
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

Joined: Jan 21, 2005
Posts: 21
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

Joined: Oct 29, 2001
Posts: 8718
    
    6



Why are you creating a new instance of drawStaff?
[ January 21, 2005: Message edited by: Joe Ess ]
alun groome
Greenhorn

Joined: Jan 21, 2005
Posts: 21
hi,
It was suggested iin another forum to do that. Anyhow it doesnt work. Do you have any suggesitons for me?

thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java 2d graphics
 
Similar Threads
Java 2D method + GTK
java 2d grpahics
ImageIcon and JLabel together
Creating my first game
Problem with Graphics