Forums Register Login
java 2d grpahics

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

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.

I'm not sure what the problem is exactly. Can you post some more code to show us what you are trying to do? Do you override paintComponent() in both javaPiano and drawStaff? If so, post those methods to start with and we can work from there.

On a side note, Sun's coding conventions dictate that class names should start with upper-case letters. Variable and method names start with lower case letters. It will greatly reduce some confusion here if you adopt these conventions in your own code.

Keep coding!

thanks for the reply. Sorry about the conventions. I have only really just started programming.

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() {

public void drawNote(String NoteIn) {

String noteIn = NoteIn;

if (noteIn == "octave1C") {

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



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

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




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


This thread has been viewed 825 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 25, 2018 13:08:30.