How would I go about using swing components and graphics in the same program. Whenever I seem to add a swing component my graphics disappear. Please help. Thanks.

My main Class

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

import javax.swing.*;


public class ResistorColorCodeProgram implements MouseMotionListener, MouseListener, KeyListener{

	public ResistorColorCodeProgram (){
		JFrame resistorCCFrame = new JFrame ("Resistor Color Code");
		
		
		resistorCCFrame.setBackground (Color.white);
		resistorCCFrame.setSize(600,500);
		JPanel resistorCCPanel = new JPanel ();
		resistorCCFrame.setLayout(new BorderLayout ());
		JPanel pane = new JPanel ();
		JButton b = new JButton ("Test");
		resistorCCFrame.add("North", pane);
		resistorCCFrame.add(pane);
		resistorCCFrame.setVisible(true);
		
		ResistorColorCodeGraphics rCCG = new ResistorColorCodeGraphics ();
		rCCG.ColorCodeDraw ();
		resistorCCFrame.add("Center",rCCG);
		
	}
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ResistorColorCodeProgram rccp = new ResistorColorCodeProgram ();
	}

	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

My Graphics Class

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


public class ResistorColorCodeGraphics extends JFrame{

	Image base, black, brown, red, orange, yellow, green, blue, violet, gray, white;
	
	
	public void ColorCodeDraw (){
		base = getToolkit().getImage("ResistorCCImages\\Resistor Base.gif");
		/*black = new Image ("Black Ring.gif");
		brown = new Image ("Brown Ring.gif");
		red = new Image ("Red Ring.gif");
		orange = new Image ("Orange Ring.gif");
		yellow = new Image ("Yellow Ring.gif");
		green = new Image ("Green Ring.gif");
		blue = new Image ("Blue Ring.gif");
		violet = new Image ("Violet Ring.gif");
		gray = new Image ("Gray Ring.gif");
		white = new Image ("White Ring.gif");
		*/
		
		repaint();
	}
	
	public void paint (Graphics g){
		super.paintComponents(g);
		
		g.drawImage(base, 100, 100, 300, 150, this);
		
		
		
	}
	
}

again. Thanks for any help.

I think you've jumbled your components a bit. ResistorColorCodeGraphics extends JFrame and you're attempting to add that to another JFrame? You can't put frames in frames.

ResistorColorCodeGraphics should extend JPanel and should override paintComponment(), not paint().

you mean like this?

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


public class ResistorColorCodeGraphics extends JPanel{

	Image base, black, brown, red, orange, yellow, green, blue, violet, gray, white;
	
	
	public void ColorCodeDraw (){
		base = getToolkit().getImage("ResistorCCImages\\Resistor Base.gif");
		/*black = new Image ("Black Ring.gif");
		brown = new Image ("Brown Ring.gif");
		red = new Image ("Red Ring.gif");
		orange = new Image ("Orange Ring.gif");
		yellow = new Image ("Yellow Ring.gif");
		green = new Image ("Green Ring.gif");
		blue = new Image ("Blue Ring.gif");
		violet = new Image ("Violet Ring.gif");
		gray = new Image ("Gray Ring.gif");
		white = new Image ("White Ring.gif");
		*/
		
		repaint();
	}
	
	public void paintComponent (Graphics g){
		super.paintComponents(g);
		
		g.drawImage(base, 100, 100, 300, 150, this);
		
		
		
	}
	
}

I still cannot get the picture to be displayed on the left and the buttons to be displayed on the right. I changed what (I believe) you told me to. Thanks for your help.

Yes, that should be fine for the graphics panel, though you don't need a repaint() in the constructor. If you want the graphic panel on the left, you'll have to add it to the WEST section and you'll need to use setPreferredSize() to size it if it contains no components of it's own. Small example thrown together from another old example of drawing

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class LinePanel extends JPanel {

    Timer animationTimer;
    float angle = 0f;
    int lineLen = 60;
    int x = 20;
    int y = 20;
    int moveX = 2;
    int moveY = 2;
    double angleChange = Math.PI/20;

    public LinePanel() {
        animationTimer = new Timer(30, new LineAnimation());
        animationTimer.start();
        
        // setting preferred size here because empty panels won't have any inherent size
        setPreferredSize(new Dimension(100,100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        double dx = Math.cos(angle) * lineLen;
        double dy = Math.sin(angle) * lineLen;
        g2d.clearRect(0, 0, getWidth(), getHeight());
        g2d.setColor(Color.BLUE);
        g2d.drawLine((int)(x - dx), (int)(y + dy), (int)(x + dx), (int)(y - dy));
    }

    class LineAnimation implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            angle += angleChange;
            x += moveX;
            y += moveY;
            if (x < 0 || x > getWidth()) {
                moveX *= -1;
                angleChange *= -1;
            }
            if (y < 0 || y > getHeight()) {
                moveY *= -1;
                angleChange *= -1;
            }
            repaint();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new BorderLayout());
                
                // here's our graphics panel
                f.add(new LinePanel(), BorderLayout.WEST);
                // here's a label
                f.add(new JLabel("Here's a label",SwingConstants.CENTER), BorderLayout.CENTER);
                f.setBounds(100, 100, 300, 300);
                f.setVisible(true);
            }
        });
    }
}

maaaaaan :-)

f.setBounds(100, 100, 300, 300); == f.setLocation(100,100);

@mKorbel: The original example I hacked this together from had no components except the panel.. setBounds() was convenient to make the frame actually have non-zero dimensions. setLocation() does not do the same :P

@ sirlink99 for your ResistorColorCodeGraphics.class is better to look for GridLayout http://download.oracle.com/javase/tutorial/uiswing/layout/grid.html and all Icon, ImageIcon or Image wrap to JLabel

then you initial layout should be

setLayout(new GridLayout(4, 3, 10, 10)); //or (3, 4..)

withOut some custom painting simple, quick and clear Container

Thanks I got it.

@Ezzaral agreed my bad,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.