Question:
Java Programming Help?
Christopher
2013-05-22 14:57:50 UTC
Can somebody please figure where the repaint is supposed to go, thanks!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

/**
* Date:5/15/13
* Description:Learning
*/
public class Sax extends JFrame implements KeyListener{

char c;
boolean n, b, m, o, k, h = false;
private Graphics g;

public void paint(Graphics g)
{
super.paint(g);
if (c == 'n'){
n = true;}
if (c == 'm'){
m = true;}
if (c == 'b'){
b = true;}
if (c == 'o'){
o = true;}
if (c == 'k'){
k = true;}
if (c == 'h'){
h = true;}

}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
c = e.getKeyChar();
if (n == true){
/*Neck and Bell*/
g.fillOval(406, 410, 80, 60);
g.fillRoundRect(271, 260, 120, 30, 10, 10);
}
if (m == true){
/*Mouthpiece*/
g.setColor(Color.BLACK);
g.fillOval(217, 259, 70, 32);
g.fillRect(238, 260, 50, 30);
}
if (b == true){
/*Body of Sax*/
g.setColor(Color.ORANGE);
g.fillArc(350, 560, 120, 120, 180, 180);
g.fillRoundRect(351, 260, 40, 380, 10, 10);
g.fillRoundRect(415, 423, 55, 200, 10, 10);
}
if (o == true){
/*Octave Key*/
g.setColor(Color.ORANGE);
g.drawArc(294, 249, 95, 30, 180, -180);
g.drawArc(294, 248, 95, 30, 180, -180);
g.drawArc(294, 247, 95, 30, 180, -180);
g.setColor(Color.BLACK);
g.drawArc(294, 250, 95, 30, 180, -180);
g.drawArc(294, 246, 95, 30, 180, -180);
}
if (k == true){
/*Keys*/
g.setColor(Color.BLACK);
g.fillOval(376, 350, 15, 15);
g.fillOval(376, 366, 15, 15);
g.fillOval(376, 382, 15, 15);
g.fillOval(360, 500, 15, 15);
g.fillOval(360, 516, 15, 15);
g.fillOval(360, 532, 15, 15);
}
if (h == true){
/*Hole In Bell and Outline of Bell*/
g.setColor(Color.BLACK);
g.fillOval(416, 417, 60, 40);
g.drawArc(410, 431, 70, 37, 186, 150);
}
}

}
Three answers:
Bob
2013-05-22 15:14:39 UTC
First, it is essential that you understand completely what you are doing. There are many errors in your code that should be fixed, and you should get to know the paint method well. The repaint() method simply calls the paint method again. Once you have a better understanding of everything, you will know when exactly to use this. But first, here are some things that (to me) appear to be incorrect in your program:



1. You are using a Graphics instance variable rather than the paint method's parameter to attempt to draw figures. This will not work. All of your painting MUST be done inside your paint method. You are probably wondering you should paint certain things when a key is pressed if you can only do that in the paint method. Say if button "b" is pressed you want circle "c" to appear. Create a private boolean, and set it to true in your key listener when b is pressed. Then repaint() (call the paint method again), and in your paint method, say "if b is pressed, g.draw(c)"



2. You should probably make char c an instance variable (make it private), and again, get rid of the graphics g instance variable. Also, it is a bad idea to initialize variables (give them values) at the top of your program. Instead, make a constructor method that looks like this:



public void Sax(){}



and initialize all the variables inside it so it looks like this:



private char c;

private boolean a, b, c, d, whatever;



public void Sax(){

c = null;

a = false;

... etc.

}



This method is called the constructor. Notice how it has the same name as the class. This means that whenever an instance of the class is created, this method will be called, and the instance variables will be given these default values to be potentially changed later.



3. I am not quite sure at all what you are trying to do with the rest of the program. It would have been better if you had written what the program was supposed to accomplish, or why you needed the repaint() method and all. Also, USE COMMENTS! Comments will help others (and yourself) understand what is going on your code.



4. You shouldnt be painting things on frames. Rather, you should do so on a Panel, which you should add to the frame. Also, you should look up how to correctly set up frames.



So, I know none of this probably made sense to you. Go research frames, panels, using the paint method in a panel, key listeners, and honestly, just creating objects in general. If you continue to try to code without a good understanding of the material, you will find yourself often getting confused in the future. (Trust me, I know).
dealto
2016-11-05 14:20:09 UTC
Why roll your very own while there is in all probability a calendar widget you may reuse? by the way, the Java API itself components many clever instructions and interfaces: Date, Calendar, GregorianCalendar, DateFormat, and SimpleDateFormat.
anonymous
2016-02-21 23:04:03 UTC
Un hijo en hogar de cualquier matrimonio produce que el matrimonio sea lleno, y ¿si todos te expone que eres infértil? ¿Dejaras el matrimonio medio lleno? No, aprovechas este método https://tr.im/TdR84 para quedarte embarazada. Jamás sospeché que la vida podría sonreírme y podía poseer un poco de suerte pero sí que me ha sonreído cuando después de muchas intentos de quedarme embarazadas, que no han funcionado he encontrado este libro y un periodo de un mes la prueba de embarazo salió positiva.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...