ShapesTest

getting an error that i don't understand. it's on the line that adds panel

to the JFrame application

.

import javax.swing.JFrame;

import javax.swing.JOptionPane;

publicclass ShapesTest

{

publicstaticvoid main(String[] args)

{

//obtain user choice

String input = JOptionPane.showInputDialog("Enter 1 to draw rectagles\n" +

"Enter 2 to draw ovals");

int choice = Integer.parseInt(input);//convert input to int

//create the panel w/ input

Shapes panel =new Shapes(choice);

JFrame application =new JFrame();//creates a new JFrame

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.add(panel);//add the panel to the frame

application.setSize(300,300);

application.setVisible(true);

}//end main

}//end class ShapesTest

error says something like: method add() is not applicable for the arguments (Shapes)

anyone?

[1716 byte] By [drapakia] at [2007-11-26 14:26:34]
# 1

Yea, you cant add Shapes to a Container.

Shapes are just drawable shapes. Theyre not actual components

or containers.

You need to create a JPanel (which is a container + a component) and

add that to the JFrame - preferably set it as the content pane.

JPanel panel = new JPanel(){

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D gfx = (Graphics2D)g;

gfx.setPaint(Color.RED);

gfx.fill(shape)

gfx.setPaint(Color.BLACK);

gfx.setStroke(new BasicStroke(3));

gfx.draw(shape);

}

}

frame.setContentPane(panel)

or

frame.getContentPane().add(panel)

in the future ask these questions in the Swing section.

duke?

TuringPesta at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...
# 2
duke!living and learning...
drapakia at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...
# 3

Here is something for you to play with.

Im not sure what your Shapes class is though?

Are you trying to use Shape or is this Class from a school class or something?

You need to import java.awt.geom.* for the Shape Class.

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class ShapeTest{

public static void main(String[] args){

new ShapeTest();

}

public ShapeTest(){

panel = new DrawPanel();

frame = new JFrame("Shapes");

frame.setSize(400, 400);

frame.setContentPane(panel);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public class DrawPanel extends JPanel{

public DrawPanel(){

}

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D gfx = (Graphics2D)g;

Shape shape = new Ellipse2D.Double(20, 20, 20, 20);

gfx.setPaint(Color.RED);

gfx.fill(shape);

gfx.setPaint(Color.BLACK);

gfx.setStroke(new BasicStroke(3));

gfx.draw(shape);

}

}

JFrame frame;

JPanel panel;

}

TuringPesta at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...
# 4

believe it or not, i was copying this code out of the book to see if i could get it to work.. and got an error! here is the class code: cause i think i'm still confused...

import java.awt.Graphics;

import javax.swing.JPanel;

public class Shapes

{

private int choice;//user's choice of which shape

//consturctor

public Shapes(int userChoice)

{

choice = userChoice;

}//end constructor

//draws cascade of shpaes

public void paintComponent(Graphics g)

{

super.paintComponent( g );

for(int i=0; 1<10; i++)

{

//pick the shape

switch (choice)

{

case 1: //rectangles

g.drawRect(10+i*10, 10+i*10, 50+i*10, 50+i*10 );

break;

case 2: //ovals

g.drawOval(10+i*10, 10+i*10, 50+i*10, 50+i*10);

break;

}//end switch

}//end for

}//end method

}//end class

drapakia at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...
# 5
i think you copied it wrong.this:public class Shapes {should bepublic class Shapes extends JPanel{With that change your first code should work fine (because Shapesis now a JPanel which is a Container which is capable of beinga content
TuringPesta at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...
# 6

by golly you got it!

no more errors but now i get a "The serializable class Shapes does not declare a static final serialVersionUID field of type long" warning on public class Shapes extends JPanel

under Shapes

those are some big words for a novice such as I!

drapakia at 2007-7-8 2:19:50 > top of Java-index,Java Essentials,Java Programming...