Split the GUI area into sections and add separate JPanels to each section. Put the buttons in one JPanel and do the drawing in another JPanel.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Split the GUI area into sections and add separate JPanels to each section. Put the buttons in one JPanel and do the drawing in another JPanel.
If you don't understand my answer, don't ignore it, ask a question.
Can't we never learned JPanels, not sure what the teacher would think of that....
How does the teacher expect you to do drawing and have components shown on the same screen without using JPanels?
If you don't understand my answer, don't ignore it, ask a question.
I run my programs in eclipse and they run in java applets
Did you read the tutorial I linked to in post#11?
If you don't understand my answer, don't ignore it, ask a question.
Yes it had to do with JApplets
It was more about JPanels that JApplets
If you don't understand my answer, don't ignore it, ask a question.
Sorry thats what I meant.
I have no other suggestions other than that you use separate panels for components and for drawing.
If you don't understand my answer, don't ignore it, ask a question.
this is where I am at now buttons working they call the methods and repaint. How do I setup button 3 to flip back and forth between my img file and my drawpicture method
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; //Cole Williams //Making smiles and colors with buttons public class williamsclab10 extends JApplet implements ActionListener { Image fishimg; String imgFile = "../images/fish.gif"; TextField text = new TextField(10); int number = -1; private static Random background = new Random(); private JButton color; private JButton print; private JButton change; private JButton surprise; int count = 0; int A = 0; int B = 0; int C = 0; boolean vis = false; boolean showmypicture = false; public void init() { setSize(600, 400); Container content = getContentPane(); int red = Math.abs(background.nextInt()) % 256; int green = Math.abs(background.nextInt()) % 256; int blue = Math.abs(background.nextInt()) % 256; getContentPane().setBackground(new Color(red, green, blue)); fishimg = getImage(getCodeBase(), imgFile); setLayout(new FlowLayout()); text.addActionListener(this); add(text); color = new JButton("Change the color"); add(color); color.addActionListener(this); print = new JButton("Print my picture!"); add(print); print.addActionListener(this); change = new JButton("Change the picture!"); add(change); change.addActionListener(this); surprise = new JButton("Click here for a surpise!"); add(surprise); surprise.addActionListener(this); } public void actionPerformed(ActionEvent a) { // button 1 background change if (a.getSource() == color) { int red = Math.abs(background.nextInt()) % 256; int green = Math.abs(background.nextInt()) % 256; int blue = Math.abs(background.nextInt()) % 256; getContentPane().setBackground(new Color(red, green, blue)); } //button 2 prints picture centered else if (a.getSource() == print) { C=1; } //button 3 picture toggling else if (a.getSource() == change) { vis = true; showmypicture =! showmypicture; B = 1; count++; repaint(); } //button 4 surprise if (a.getSource() == surprise) { A = 1; count++; repaint(); } } private void drawpicture(Graphics g, int x, int y) { // tail g.setColor(Color.blue); g.drawLine(x, y, x + 30, y); g.drawLine(x + 30, y, x + 30, y + 30); g.drawLine(x + 30, y + 30, x, y); // body g.setColor(Color.blue); g.drawOval(x + 20, y + -33, 40, 40); g.setColor(Color.blue); g.drawOval(x + 36, y + -32, 10, 10); } public void paint(Graphics g) { super.paint(g); Dimension d = getSize(); text.setLocation(d.width / 2 - 30, 30); int centerX = d.width / 2; int centerY = d.height / 2; int widthOfPicture = 600; int heightOfPicture = 600; int x = centerX - widthOfPicture / 2; int y = centerY - heightOfPicture / 2; change.setSize(150, 50); change.setLocation(d.width - 600, d.height - 50); color.setSize(150, 50); color.setLocation(d.width - 150, d.height - 50); print.setSize(150, 50); print.setLocation(d.width - 600, d.height - 400); surprise.setSize(160, 50); surprise.setLocation(d.width - 160, d.height - 400); if (A == 1) { drawpicture(g,400,100); g.drawImage(fishimg, 50,100,200,200,this); } else if (B==1) { drawpicture(g,400,100); } } }
The listener should set some values that the paint() method can use to determine what is to be drawn.How do I setup button 3 to flip back and forth between
If you don't understand my answer, don't ignore it, ask a question.
I still haven't gotten the toggling between the two methods to work this is turning my hair grey
Try working out the logic by writing a list of the steps (pseudo code) that the program needs to do to toggle between the two methods. When you get a good design (we'll help) then write the code to do it.
If you don't understand my answer, don't ignore it, ask a question.