I have this code here

public void actionPerformed(ActionEvent arg0) {
		
		
		
		for (int i = 0; i <= 360; i++){
			for (int z = 0; z < 9; z++){
				xaxisWave1 [z][i] -= 1;
				if (xaxisWave1[z][360] < 0){
					System.out.println("Line out");
					for (int y = 0; y <= 360; y ++){
						xaxisWave1[z][y] += 1000;
					}
				}
			}
		}
		for (int i = 0; i <= 360; i++){
			for (int z = 0; z < 9; z++){
				xaxisWave2 [z][i] -= 15;
			}
		}
		
		
		repaint();
	}

This should make it so what every time a sine wave goes off of the screen it reprints at 1000 px along the x axis. I have multiple sine waves and it should sense it going off of the screen for all of them, but for some reason it only senses the first one. What is the problem? Thanks.

Add some println statements to show the values of the variables as they change and are used. The output should show you where your logic is wrong.

For example:
System.out.println("xaxisWave1[z][360]=" + xaxisWave1[z][360] + " for z=" + z);

Edited 5 Years Ago by NormR1: n/a

I have tried, but also my sine waves look funny. it's like the polygon is not a polygon of 361 values. it is more like it adds all of them together and then connects it. run this code and you will see.

for (int i = 0; i < 2; i++){
			for (int z = 0; z <= 360; z++){
				xaxisWave1 [i] [z] = (int) Math.toRadians(z * (25));
				yaxisWave1 [i] [z] = (((int)(Math.round( (Math.sin(Math.toRadians(z)) * -1) * 50))));
				yaxisWave1 [i] [z] += 50;	
				
			}
		}

that creates the values and then use this code to print

for (int i = 0; i < 9; i++){
			g.drawPolygon(xaxisWave1[i], yaxisWave1[i], 361);
		}

thanks for your help.

run this code and you will see

How?
Its not even a class.

Can you explain the logic of what the code you posted is supposed to do and how it does it?
Why do you think your algorithm is correct?

try putting the println statement that Norm mentioned inside your paintComponent method, and if you only see one set of values showing in your standard output, what you are actually doing is that you are only drawing a picture with only the last set of values you have calculated. just try putting the System.out inside your paintComponent.

for (int i = 0; i < 2; i++){
			for (int z = 0; z <= 360; z++){
				xaxisWave1 [i] [z] = (int) Math.toRadians(z * (25)); // assigns x values to the array of 361 values the x has a horizontal stretch by a factor of 25
				yaxisWave1 [i] [z] = (((int)(Math.round( (Math.sin(Math.toRadians(z)) * -1) * 50)))); // assigns a y value by using the Math.sin to create a sine wave. Strtches it vertically by a factor of 50
				yaxisWave1 [i] [z] += 50;	// adds 50 to the y value to translate the sine wave 50 pixels down
 
			}
		}
for (int i = 0; i < 9; i++){
			g.drawPolygon(xaxisWave1[i], yaxisWave1[i], 361);  // draws 9 sets of polygons each of which contain a single sine wave that contains 361 values.
		}

That is what is should do. and I don't know, but it may be the 2D array I am using that is throwing me off.

I also have another code that when I compare 3 user inputed values to the answers generated by the computer they are wrong. I have printed out the answers and what I have put in and even when they are IDENTICAL it still says that the user is wrong.

Edited 5 Years Ago by sirlink99: n/a

it still says that the user is wrong.

What code are you talking about here? What is the purpose of talking about code none of us has seen?

this is the whole graphics code

import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class ResistorColorCodeGraphics extends JPanel implements ActionListener{

	Image base, black, brown, red, orange, yellow, green, blue, violet, gray, white;
    Image click;
	int ring1Col, ring2Col, ring3Col;
	Image ring1, ring2, ring3;
	int ring1ColAns, ring2ColAns, ring3ColAns;
	String resistance;
	int res;
	Font display = new Font ("Arial", 30,30);
	
	public void ColorCodeDraw (){
		base = getToolkit().getImage("ResistorCCImages\\Resistor Base.gif");
		black = getToolkit().getImage("ResistorCCImages\\Black Ring.gif");
		brown = getToolkit().getImage("ResistorCCImages\\Brown Ring.gif");
		red = getToolkit().getImage("ResistorCCImages\\Red Ring.gif");
		orange = getToolkit().getImage("ResistorCCImages\\Orange Ring.gif");
		yellow = getToolkit().getImage("ResistorCCImages\\Yellow Ring.gif");
		green = getToolkit().getImage("ResistorCCImages\\Green Ring.gif");
		blue = getToolkit().getImage("ResistorCCImages\\Blue Ring.gif");
		violet = getToolkit().getImage("ResistorCCImages\\Violet Ring.gif");
		gray = getToolkit().getImage("ResistorCCImages\\Gray Ring.gif");
		white = getToolkit().getImage("ResistorCCImages\\White Ring.gif");
		click = getToolkit().getImage("ResistorCCImages\\ClickRing.gif");
		ring1 = click;
		ring2 = click;
		ring3 = click;
		resistanceCalc();
		
		repaint();
	}
	
	private void resistanceCalc() {
		// TODO Auto-generated method stub
		res = (int) (Math.random () * 10);
		resistance = "" + res;
		ring1ColAns = res;
		System.out.println (res);
		res = (int) (Math.random () * 10);
		resistance += res;
		ring2ColAns = res;
		System.out.println (res);
		res = (int) (Math.random () * 10);
		ring3ColAns = res;
		System.out.println (res);
		if (res == 0){}
		if (res == 1){resistance += "0";}
		if (res == 2){resistance += "00";}
		if (res == 3){resistance +="K";}
		if (res == 4){resistance += "0 K";}
		if (res == 5){resistance += "00 K";}
		if (res == 6){resistance += "000 K";}
		if (res == 7){resistance += "0000 K";}
		if (res == 8){resistance += "00 000 K";}
		if (res == 9){resistance += "000 000 K";}
		
		
		
		
	}

	public void paintComponent (Graphics g){
		super.paintComponent(g);
		
		g.drawImage (base, 0, 50, 500, 350, this);
		
		g.drawImage (ring1, 125, 129, 166, 175, this);
		g.drawImage (ring2, 165, 129, 166, 175, this);
		g.drawImage (ring3, 205, 129, 166, 175, this);
		g.setFont(display);
		g.drawString (resistance + " Ohms", 160,50);
		
		
		}
	private int ring1ColChoice, ring2ColChoice, ring3ColChoice;

	public void printRings(int ringsel, int col) {
		if (ringsel == 1){
			ring1ColChoice = col;
			setRing (col, ringsel);
		}
		if (ringsel == 2){
			ring2ColChoice = col;
			setRing (col, ringsel);
		}
		if (ringsel == 3){
			ring3ColChoice = col;
			setRing (col, ringsel);
		}
		
		
	}

	private void setRing(int col, int ringsel) {
		// TODO Auto-generated method stub
		if (col == 0){
			if (ringsel == 1){ ring1 = black;}
			if (ringsel == 2){ ring2 = black;}
			if (ringsel == 3){ ring3 = black;}
		}
		else if (col == 1){
			if (ringsel == 1){ ring1 = brown;}
			if (ringsel == 2){ ring2 = brown;}
			if (ringsel == 3){ ring3 = brown;}
		}
		else if (col == 2){
			if (ringsel == 1){ ring1 = red;}
			if (ringsel == 2){ ring2 = red;}
			if (ringsel == 3){ ring3 = red;}
		}
		else if (col == 3){
			if (ringsel == 1){ ring1 = orange;}
			if (ringsel == 2){ ring2 = orange;}
			if (ringsel == 3){ ring3 = orange;}
		}
		else if (col == 4){
			if (ringsel == 1){ ring1 = yellow;}
			if (ringsel == 2){ ring2 = yellow;}
			if (ringsel == 3){ ring3 = yellow;}
		}
		else if (col == 5){
			if (ringsel == 1){ ring1 = green;}
			if (ringsel == 2){ ring2 = green;}
			if (ringsel == 3){ ring3 = green;}
		}
		else if (col == 6){
			if (ringsel == 1){ ring1 = blue;}
			if (ringsel == 2){ ring2 = blue;}
			if (ringsel == 3){ ring3 = blue;}
		}
		else if (col == 7){
			if (ringsel == 1){ ring1 = violet;}
			if (ringsel == 2){ ring2 = violet;}
			if (ringsel == 3){ ring3 = violet;}
		}
		else if (col == 8){
			if (ringsel == 1){ ring1 = gray;}
			if (ringsel == 2){ ring2 = gray;}
			if (ringsel == 3){ ring3 = gray;}
		}
		else if (col == 9){
			if (ringsel == 1){ ring1 = white;}
			if (ringsel == 2){ ring2 = white;}
			if (ringsel == 3){ ring3 = white;}
		}
		repaint();
	}
	private JFrame ansFrame;
	public void checkAnswer() {
		System.out.println (ring1ColChoice);
		System.out.println (ring2ColChoice);
		System.out.println (ring3ColChoice);
		// TODO Auto-generated method stub
		if (ring1ColChoice == ring1ColAns && ring2ColChoice == ring3ColAns && ring3ColAns == ring2ColChoice){
			ansFrame = new JFrame ("");
			JLabel label = new JLabel ("Your answer is CORRECT!");
			JPanel pane = new JPanel ();
			JButton nu = new JButton ("New Number"); nu.setActionCommand("new"); nu.addActionListener(this);
			JButton close = new JButton ("Close"); close.setActionCommand ("Close"); close.addActionListener(this);
			pane.add(label);
			pane.add(nu);
			pane.add(close);
			ansFrame.add(pane);
			ansFrame.setSize (200,100);
			ansFrame.setVisible(true);
		}
		else if (ring1ColChoice != ring1ColAns || ring2ColAns != ring2ColChoice || ring3ColAns != ring2ColChoice){
			JOptionPane.showMessageDialog(null, "You are Incorrect");
		}
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if ("new".equals(e.getActionCommand ())){
			System.out.println("");
			System.out.println("");
			System.out.println("");
			System.out.println("");
			resistanceCalc();
			ring1ColChoice = 0; ring2ColChoice = 0; ring3ColChoice = 0;
			ring1 = click; ring2 = click; ring3 = click;
			repaint();
			System.out.println("");
			ansFrame.dispose();
		}
		if ("Close".equals(e.getActionCommand ())){
			ansFrame.dispose();
		}
		
	}
}

and here is the launcher for it

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class ResistorColorCodeProgram implements ActionListener{

	JButton bl,br,red,or,ye,gr,blu,vi,gray,wh;
	int mx, my;
	int ringC1, ringC2, ringC3;
	int col;    // 0 = black, 1 = brown, ... gray = 8, white = 9
	int ringsel = 1; // selects a ring1 = ring 1, 2 = ring2, 3 = ring3
	int lastRing;
	int lastCol;
	private ResistorColorCodeGraphics rCCG = new ResistorColorCodeGraphics ();
	
	public ResistorColorCodeProgram (){
		JFrame resistorCCFrame = new JFrame ("Resistor Color Code");
		
		
		resistorCCFrame.setBackground (Color.white);
		resistorCCFrame.setBounds(100,100, 100,100);
		resistorCCFrame.setSize(600,500);
		
		resistorCCFrame.setLayout(new BorderLayout ());
		
		ImageIcon bls = new ImageIcon ("ResistorCCImages\\BlackC.gif");
		ImageIcon brs = new ImageIcon ("ResistorCCImages\\BrownC.gif");
		ImageIcon reds = new ImageIcon ("ResistorCCImages\\RedC.gif");
		ImageIcon ors = new ImageIcon ("ResistorCCImages\\OrangeC.gif");
		ImageIcon yes = new ImageIcon ("ResistorCCImages\\YellowC.gif");
		ImageIcon grs = new ImageIcon ("ResistorCCImages\\GreenC.gif");
		ImageIcon blus = new ImageIcon ("ResistorCCImages\\BlueC.gif");
		ImageIcon vis = new ImageIcon ("ResistorCCImages\\VioletC.gif");
		ImageIcon grays = new ImageIcon ("ResistorCCImages\\GrayC.gif");
		ImageIcon whs = new ImageIcon ("ResistorCCImages\\WhiteC.gif");
		JButton check = new JButton ("Check"); check.setActionCommand("Check Answer"); check.addActionListener(this);
		
		bl = new JButton (bls); bl.setActionCommand("Black"); bl.addActionListener(this);
		br = new JButton (brs); br.setActionCommand ("Brown"); br.addActionListener (this);
		red = new JButton (reds); red.setActionCommand("Red"); red.addActionListener(this);
		or = new JButton (ors); or.setActionCommand("Orange"); or.addActionListener(this);
		ye = new JButton (yes); ye.setActionCommand ("Yellow"); ye.addActionListener(this);
		gr = new JButton (grs); gr.setActionCommand ("Green"); gr.addActionListener (this);
		blu = new JButton (blus); blu.setActionCommand("Blue"); blu.addActionListener(this);
		vi = new JButton (vis); vi.setActionCommand("Violet"); vi.addActionListener(this);
		gray = new JButton (grays); gray.setActionCommand ("Gray"); gray.addActionListener (this);
		wh = new JButton (whs); wh.setActionCommand("White"); wh.addActionListener (this);
		
		
		JPanel ringPanel = new JPanel ();
		
		JButton ring1B = new JButton ("Ring 1"); ring1B.setActionCommand("ring1"); ring1B.addActionListener(this);
		JButton ring2B = new JButton ("Ring 2"); ring2B.setActionCommand("ring2"); ring2B.addActionListener(this);
		JButton ring3B = new JButton ("Ring 3"); ring3B.setActionCommand("ring3"); ring3B.addActionListener(this);
		JButton add = new JButton ("Add Ring"); add.setActionCommand ("Add Ring"); add.addActionListener(this);
		
		ringPanel.add(add, JButton.CENTER);
		ringPanel.add(ring3B, JButton.CENTER);
		ringPanel.add(ring2B, JButton.CENTER);
		ringPanel.add(ring1B, JButton.CENTER);
		
		JPanel buttonPanel = new JPanel ();
		buttonPanel.setLayout(new GridLayout (11,1));
		buttonPanel.add(bl);
		buttonPanel.add(br);
		buttonPanel.add(red);
		buttonPanel.add(or);
		buttonPanel.add(ye);
		buttonPanel.add(gr);
		buttonPanel.add(blu);
		buttonPanel.add(vi);
		buttonPanel.add(gray);
		buttonPanel.add(wh);
		buttonPanel.add(check);
		resistorCCFrame.add(buttonPanel, BorderLayout.EAST);
		resistorCCFrame.add(ringPanel, BorderLayout.NORTH);
		resistorCCFrame.setVisible(true);
		
		
		rCCG.ColorCodeDraw ();
		resistorCCFrame.add(rCCG, BorderLayout.CENTER);
		
	}
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ResistorColorCodeProgram rccp = new ResistorColorCodeProgram ();
	}

	
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if ("Black".equals (e.getActionCommand())){
			col = 0;
		}
		if ("Brown".equals(e.getActionCommand ())){
			col = 1;
		}
		if ("Red".equals(e.getActionCommand())){
			col = 2;
		}
		if ("Orange".equals(e.getActionCommand())){
			col = 3;
		}
		if ("Yellow".equals(e.getActionCommand())){
			col = 4;
		}
		if ("Green".equals(e.getActionCommand())){
			col = 5;
		}
		if ("Blue".equals(e.getActionCommand())){
			col = 6;
		}
		if ("Violet".equals(e.getActionCommand())){
			col = 7;
		}
		if ("Gray".equals(e.getActionCommand())){
			col = 8;
		}
		if ("White".equals(e.getActionCommand())){
			col = 9;
		}		
		if ("ring1".equals(e.getActionCommand ())){
			ringsel = 1;
		}
		if ("ring2".equals(e.getActionCommand ())){
			ringsel = 2;
		}
		if ("ring3".equals(e.getActionCommand ())){
			ringsel = 3;
		}
		if ("Add Ring".equals(e.getActionCommand())){
			rCCG.printRings(ringsel, col);
		}
		if ("Check Answer".equals(e.getActionCommand ())){
			rCCG.checkAnswer ();
		}
	}

	
	
}

thanks for your help

it doesn't have anything to do with it (other then being places in the same final program when done) I just didn't want to start a new thread for each small problem I have.

The problem is whenever it asks me for a certain ohm resistor i print out the answers to make sure I am correct. I enter in the correct values and then click check it says I'm wrong. I then check my code that I printed out with the original. They are identical. I don't understand why it is saying I am wrong.

it says I'm wrong.

How does it do that?
Can you give the details on what you enter and what the computer outputs?

see this part of the code in your Graphics code:

else if (ring1ColChoice != ring1ColAns || ring2ColAns != ring2ColChoice || ring3ColAns != ring2ColChoice){

the last comparison is wrong. I assume you want to have ring3ColChoice as the last part...

I have this portion of code here

public void checkAnswer() {
		System.out.println (ring1ColChoice);
		System.out.println (ring2ColChoice);
		System.out.println (ring3ColChoice);
		// TODO Auto-generated method stub
		if (ring1ColChoice == ring1ColAns && ring2ColChoice == ring3ColAns && ring3ColAns == ring2ColChoice){
			ansFrame = new JFrame ("");
			JLabel label = new JLabel ("Your answer is CORRECT!");
			JPanel pane = new JPanel ();
			JButton nu = new JButton ("New Number"); nu.setActionCommand("new"); nu.addActionListener(this);
			JButton close = new JButton ("Close"); close.setActionCommand ("Close"); close.addActionListener(this);
			pane.add(label);
			pane.add(nu);
			pane.add(close);
			ansFrame.add(pane);
			ansFrame.setSize (200,100);
			ansFrame.setVisible(true);
		}
		else if (ring1ColChoice != ring1ColAns || ring2ColAns != ring2ColChoice || ring3ColAns != ring2ColChoice){
			JOptionPane.showMessageDialog(null, "You are Incorrect");
		}
		
		
	}

Which checks my answer to the number generated by the computer. if I am correct it opens a window saying I am correct and asks me if I would like another code. if I am incorrect it prints out a message saying that I am incorrect.

I can't believe I didn't notice that. Thanks that fixed the problem. BTW there was also two problems in the if statement above that were the same type of mistake.

Edited 5 Years Ago by sirlink99: n/a

Some suggestions on your debug output:

System.out.println ("1res=" + res);
		res = (int) (Math.random () * 10);
		resistance += res;
		ring2ColAns = res;
		System.out.println ("2res=" + res);
		res = (int) (Math.random () * 10);
		ring3ColAns = res;
		System.out.println ("3res=" + res);
  ...
		System.out.println ("r1C=" + ring1ColChoice + ", r1A=" +ring1ColAns);
		System.out.println ("r2C=" + ring2ColChoice + ", r2A=" +ring2ColAns);
		System.out.println ("r3C=" + ring3ColChoice + ", r3A=" +ring3ColAns);

A list of numbers on the console without any labels is hard to figure out

I now have a problem with my resistance generator. Whenever the first number generated is a zero then it should swap the second value with the first and subtract one from the third.

the resistor color codes go as follows:

Ring #:...1....2.......3
Black.....0....0.....x10^0
Brown.....1....1.....x10^1
Red.......2....2.....x10^2
Orange....3....3.....x10^3
Yellow....4....4.....x10^4
Green.....5....5.....x10^5
Blue......6....6.....x10^6
Violet....7....7.....x10^7
Gray......8....8.....x10^8
White.....9....9.....x10^9

(ignore the dots)

so if you want a 510 ohm resistor then the colors would go

green, brown, brown
5 1 x10^1 > 510


The reason the swap works is because if you have a resistor that is 100 ohms you could write it like this:

Black, Brown, Red
0 1 x10^2 > 0100 = 100 ohms < this is what I am trying to avoid

or you could write it like this

Brown, Black Brown
1 0 x10^1 > 100 < this is what I am trying to program

Notice that in the last color the value goes down by one

Here is my code that should assign all the values in the second order and then subtract one from the last color.

int swap = 0;
	private void resistanceCalc() {
		// TODO Auto-generated method stub
		res = (int) (Math.random () * 10);
		if (res == 0){resistance += " "; swap = 1;ring2ColAns = res;}
		else {
		resistance = "" + res; ring1ColAns = res;
		}
		System.out.println (res);
		
		res = (int) (Math.random () * 10);
		if (swap == 1){
			ring1ColAns = res; resistance += res;
		}
		else{
			resistance += res;
			ring2ColAns = res;
		}
		
		
		if (swap == 1){
			res = ((int) (Math.random () * 10)) - 1;         // this is where it should reduce the last color by a value of 1 if the first colors have been swapped
			ring3ColAns = res;
		}
		else {
			res = (int) (Math.random () * 10);
			ring3ColAns = res;
		}
		
		if (res == 0){}
		if (res == 1){resistance += "0";}
		if (res == 2){resistance += "00";}
		if (res == 3){resistance +="K";}
		if (res == 4){resistance += "0 K";}
		if (res == 5){resistance += "00 K";}
		if (res == 6){resistance += "000 K";}
		if (res == 7){resistance += "0000 K";}
		if (res == 8){resistance += "00 000 K";}
		if (res == 9){resistance += "000 000 K";}
		
		swap = 0;
		System.out.println (ring1ColAns + ", " + ring2ColAns + ", " + ring3ColAns);
		
	}

My code, for some reason does not reduce the value of the last color. I don't know why. Thanks for your help.

Edited 5 Years Ago by sirlink99: n/a

Try debugging your code by adding printlns to show the value of variables and the logic flow.

res = ((int) (Math.random () * 10)) - 1;         
// this is where it should reduce the last color by a value of 1

This code is setting the value of res to something. It is not subtracting 1

yes, but the subtract 1 outside of the brackets should be subtracting one from the generated value in the brackets, which would be the original value that has been generated.

This article has been dead for over six months. Start a new discussion instead.