Meaningless Drivel is fun!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "java 2d grpahics" Watch "java 2d grpahics" New topic
Author

java 2d grpahics

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
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!

Layne


Java API Documentation
The Java Tutorial
alun groome
Greenhorn

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

}
}
 
 
subject: java 2d grpahics
 
Similar Threads
Trouble implementing a GUI
2 dimensional JTabbedPane
java 2d graphics
Creating my first game
Fundamentals stuff