File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes java 2d grpahics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "java 2d grpahics" Watch "java 2d grpahics" New topic

java 2d grpahics

alun groome

Joined: Jan 21, 2005
Posts: 21

Im having some right hassel with java 2d graphics.
One class ( 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 (

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.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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!


Java API Documentation
The Java Tutorial
alun groome

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

I agree. Here's the link:
subject: java 2d grpahics
It's not a secret anymore!