stand alone app help

im new to stand alone apps, how would i get my drawBean to display on the screen? thanks

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

publicclass Lab9extends JFrame{

public Lab9(){

addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

dispose();

System.exit(0);

}

});

}

privatevoid drawBean (Graphics g,int mid,int top){

g.setColor(Color.blue);

g.fillOval(150, 80, 25, 60);

g.fillOval(153, 140, 20, 100);

g.fillOval(145, 75, 40, 10);

g.fillOval(163, 70, 5, 5);

g.setColor(Color.green);

g.fillOval(83, 180, 10, 10);

g.fillOval(219, 125, 10, 10);

}

publicstaticvoid main(String[] args){

Lab9 frame =new Lab9();

frame.setSize(500, 400);

frame.setTitle("Lab9");

frame.setVisible(true);

}

}

[1915 byte] By [suomiathearta] at [2007-11-26 12:57:28]
# 1
Override the paintComponent method, and call drawBean there, passing the Graphics object that's passed to paintComponent into your method. http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
hunter9000a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 2

i dont know if i follow, i tried this, but nothing will display

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class Lab9 extends JFrame {

public Lab9() {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

});

}

private void drawBean (Graphics g, int mid, int top){

g.setColor(Color.blue);

g.fillOval(150, 80, 25, 60);

g.fillOval(153, 140, 20, 100);

g.fillOval(145, 75, 40, 10);

g.fillOval(163, 70, 5, 5);

g.setColor(Color.green);

g.fillOval(83, 180, 10, 10);

g.fillOval(219, 125, 10, 10);

}

void paintComponent(Graphics g){

drawBean(g, 50, 50);

}

public static void main(String[] args) {

Lab9 frame = new Lab9();

frame.setSize(500, 400);

frame.setTitle("Lab9");

frame.setVisible(true);

}

}

suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 3

Doh! Sorry, I gave you some bad advice. You shouldn't have the JFrame doing the painting, a JComponent added to the JFrame's content pane should do the painting. I recommend extending a JPanel and moving your paintComponent and drawBean methods there. Then add the JPanel to the JFrame's content pane.

hunter9000a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 4

ok, i set up my panels and panes and what not, but my button wont show up, this obviosly means my picture will not either how do i get this to work?

import java.math.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.applet.*;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JApplet;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class Lab9 extends JFrame implements ActionListener {

private Container pane;

private JButton colorButton;

private JPanel northPanel = new JPanel();

public Lab9() {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

});

}

public void init()

{

this.getContentPane().setLayout(new BorderLayout(30,5));

colorButton = new JButton("Move Picture");

northPanel.add(colorButton);

pane = getContentPane();

pane.add(northPanel,BorderLayout.NORTH);

colorButton.addActionListener(this);

}

public void actionPerformed (ActionEvent ae)

{

Graphics g=this.getGraphics();

if(ae.getSource()==colorButton){

}

}

private void drawBean (Graphics g, int mid, int top){

g.setColor(Color.blue);

g.fillOval(150, 80, 25, 60);

g.fillOval(153, 140, 20, 100);

g.fillOval(145, 75, 40, 10);

g.fillOval(163, 70, 5, 5);

g.setColor(Color.green);

g.fillOval(83, 180, 10, 10);

g.fillOval(219, 125, 10, 10);

}

void paintComponent(Graphics g){

drawBean(g, 50, 50);

}

public static void main(String[] args) {

Lab9 frame = new Lab9();

frame.setSize(500, 400);

frame.setTitle("Lab9");

frame.setVisible(true);

}

}

suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 5

You never call your init method, so your button isn't being created or added to the container.

You still have your custom painting happening in the JFrame, that won't work. Make another class that extends JPanel (MyPanel for instance) and do your custom painting there. Then instead of adding a JPanel, add a MyPanel to the JFrame.

hunter9000a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 6
how would i call the init method? i dont understand
suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 7
> how would i call the init method? i dont understandframe.init();You really need to learn the basics before you start building GUIs.
CaptainMorgan08a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 8
this is the last thing i ever have to do in java EVER! its my final lab, i just gotta get it to display the buttonframe.init();where do i put that?
suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 9

This is not related with your question, is an advide for your code. If you want that your JFrame exits program on close, it isn't necessary write listener method, this is part of JFrame methods.

public Lab9() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

It's clearer.

govisagod512a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 10

> this is the last thing i ever have to do in java

> EVER! its my final lab, i just gotta get it to

> display the button

>

> frame.init();

>

> where do i put that?

In your constructor. All the code in the init method belongs in your constructor, but it seemed simpler to tell you to just call the method there. By the way, why did you even make an init method?

hunter9000a at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 11
actually when i put frame.init() in the constructor it still doesnt show up, but i think ur saying that i gotta put my code from my init in there as well-- lemme seeMessage was edited by: suomiatheart
suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 12
> this is the last thing i ever have to do in java> EVER! its my final lab, i just gotta get it to> display the buttonThat's the spirit and a good work ethic to take with you through the rest of your life!
floundera at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 13
lol thanks i will cherish my java memories for a lifetime
suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 14

this is what i got so far import java.math.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.applet.*;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JApplet;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class Lab9 extends JFrame implements ActionListener {

private Container pane;

private JButton colorButton;

private JPanel northPanel = new JPanel();

public Lab9() {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

});

}

public void init()

{

this.getContentPane().setLayout(new BorderLayout(30,5));

colorButton = new JButton("Color Change");

northPanel.add(colorButton);

pane = getContentPane();

pane.add(northPanel,BorderLayout.NORTH);

colorButton.addActionListener(this);

}

public void actionPerformed (ActionEvent ae)

{

Graphics g=this.getGraphics();

if(ae.getSource()==colorButton){

}

}

dudeSkater myPicture = new dudeSkater (Color.yellow, Color.green, Color.blue);

public void paint (Graphics g)

{

myPicture.paint(g);

}

class dudeSkater{

private Color headColor;

private Color armColor;

private Color legColor;

private int x = 200;

private int y = 50;

public dudeSkater(Color h, Color a, Color l)

{

headColor = h;

armColor = a;

legColor = l;

}

public void setXY(int newX, int newY)

{

x = newX;

y = newY;

}

public void setColors(Color headColor, Color armColor, Color smileColor)

{

this.armColor = armColor;

this.legColor = legColor;

this.headColor = headColor;

}

public Color getheadColor()

{

return headColor;

}

public Color getEyeColor()

{

return armColor;

}

public Color getSmileColor()

{

return legColor;

}

public void paint (Graphics g){

g.setColor(headColor);

g.drawOval(x+20, y+20, 50, 50);

g.drawLine(x+27, y+30, x+90, y+30);

g.setColor(armColor);

g.drawOval(x+20, y+40, 10, 10);

g.drawOval(x+40, y+40, 10, 10);

g.drawLine(x+45, y+70, x+45, y+120);

g.drawLine(x+30, y+80, x+60, y+80);

g.drawLine(x+30, y+80, x+20, y+100);

g.drawLine(x+60, y+80, x+70, y+100);

g.setColor(legColor);

g.drawLine(x+45, y+120, x+20, y+155);

g.drawLine(x+45, y+120, x+50, y+155);

g.drawRect(x+0, y+155, 80, 5);

g.drawOval(x+10, y+160, 10, 10);

g.drawOval(x+70, y+160, 10, 10);

g.drawString("Lucky Pierre 4 Life", 100, 100);

}

}

public static void main(String[] args) {

Lab9 frame = new Lab9();

frame.setSize(500, 400);

frame.setTitle("Lab9");

frame.setVisible(true);

frame.init();

}

}

suomiathearta at 2007-7-7 16:53:52 > top of Java-index,Java Essentials,New To Java...
# 15
it works, but the button is hidden until you click it, whats goin on there?
suomiathearta at 2007-7-21 15:43:49 > top of Java-index,Java Essentials,New To Java...
# 16
Why should we care if you don't care enough to learn Java? And why should we answer a multiposting butthole? http://forum.java.sun.com/thread.jspa?threadID=5117094&tstart=0
floundera at 2007-7-21 15:43:49 > top of Java-index,Java Essentials,New To Java...