Results 1 to 9 of 9
Thread: Java Graphics
- 12-07-2009, 05:22 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Java Graphics
Hi I am trying to get the Java Graphics to work but the paint function is messing up the GUI.
following is my code:
I apologize if the code is not very pretty.Also I can send the images for the labels if anyone wants it.Java Code:/* Sample Program:Displaying a window File:Sample.java Author:Shweta Gupte */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import javax.swing.border.*; import java.awt.geom.*; import java.applet.*; //import java.awt.Graphics; class Sample extends JFrame implements ActionListener { private static final int FRAME_WIDTH =700; private static final int FRAME_HEIGHT=700; private static final int FRAME_X_ORIGIN=0; private static final int FRAME_Y_ORIGIN=0; private JRadioButton [] radioButton; private final String CIRCLE="Circle"; private JLabel image1,image2,image3,image4; private JButton circleBtn,RectBtn,SquareBtn,ovalBtn; private TitledBorder title; private int oval=0,rect=0; public static void main ( String [] args){ Sample frame = new Sample(); frame.setVisible(true); } public Sample(){ Container contentPane; JPanel drawPanel,manipulatePanel,buttonPanel,colorPanel,RadioPanel; String [] btnText= {"Translate","Resize"}; //Set Frame properties setTitle("Shape Shifter"); setSize( FRAME_WIDTH, FRAME_HEIGHT); setLocation( FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); //changeBkColor(); contentPane = getContentPane( ); contentPane.setLayout(new BorderLayout()); buttonPanel =new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.setBackground(Color.blue); drawPanel = new JPanel(); drawPanel.setBorder(BorderFactory.createTitledBorder("Draw")); drawPanel.setBackground(Color.blue); //Fill and Coordinates Panel colorPanel = new JPanel(); colorPanel.setBorder(BorderFactory.createRaisedBevelBorder()); colorPanel.setLayout(new GridLayout(0,1)); JCheckBox fillBtn =new JCheckBox("Fill"); fillBtn.setBackground(Color.blue); JCheckBox coordBtn =new JCheckBox("Coordinates"); coordBtn.setBackground(Color.blue); colorPanel.add(fillBtn); colorPanel.add(coordBtn); colorPanel.setBackground(Color.blue); manipulatePanel = new JPanel(); manipulatePanel.setLayout(new FlowLayout()); title= BorderFactory.createTitledBorder("Manipulate"); title.setTitleJustification(TitledBorder.RIGHT); manipulatePanel.setBorder(title); manipulatePanel.setBackground(Color.blue); RadioPanel =new JPanel(); RadioPanel.setLayout(new GridLayout(0,1)); RadioPanel.setBorder(BorderFactory.createLoweredBevelBorder()); radioButton =new JRadioButton[btnText.length]; for (int i=0; i<radioButton.length;i++){ radioButton[i]= new JRadioButton(btnText[i]); //radioButton[i].addItemListener(this); radioButton[i].setBackground(Color.blue); RadioPanel.add(radioButton[i]); } manipulatePanel.add(RadioPanel); //Oval button ovalBtn =new JButton(); //Setimage gif image3=new JLabel (new ImageIcon( "myOval.gif")); ovalBtn.add(image3); drawPanel.add(ovalBtn); ovalBtn.addActionListener(this); //Circle button circleBtn =new JButton(); //Setimage gif image1=new JLabel (new ImageIcon( "myCircle.gif")); circleBtn.add(image1); drawPanel.add(circleBtn); circleBtn.addActionListener(this); //rectangle Button RectBtn =new JButton(); //setimage gif image2=new JLabel (new ImageIcon( "myRectangle.gif")); RectBtn.add(image2); drawPanel.add(RectBtn); RectBtn.addActionListener(this); //Square button SquareBtn =new JButton(); //Setimage gif image4=new JLabel (new ImageIcon( "mySquare.gif")); SquareBtn.add(image4); drawPanel.add(SquareBtn); SquareBtn.addActionListener(this); buttonPanel.add(drawPanel); buttonPanel.add(colorPanel); buttonPanel.add(manipulatePanel); contentPane.add(buttonPanel,BorderLayout.NORTH); //setDefaultCloseOperation(EXIT_ON_CLOSE); changeBkColor(); } /*public void paint(Graphics g){ //g.setColor(Color.red); if(oval==1){ g.drawOval(200,200,220,220); //g.setColor(Color.green); } if(rect==1) g.fillRect(230,230,250,250); }*/ public void actionPerformed(ActionEvent event){ if(event.getSource() instanceof JButton){ JButton clickedButton =(JButton) event.getSource(); repaint(); //System.out.println(clickedButton); if(clickedButton== ovalBtn){ oval=1; rect=0; System.out.println("here"); clickedButton=null; } if(clickedButton== circleBtn){ //draw Circle } if(clickedButton ==RectBtn){ oval=0; rect=1; System.out.println("here1"); clickedButton=null; //Draw Rect } if(clickedButton== SquareBtn){ //Draw Square } } System.out.println("here2"); //repaint(); } private void changeBkColor(){ Container contentPane = getContentPane(); contentPane.setBackground(Color.BLACK); } }
I have commented out the paint function.but I need it to draw the circle ,oval etc.
Any alternatives or suggestions appreciated.
Thanks
svaidya
-
Yes, read the Sun graphics tutorials. They will show you how to do painting in Swing. For one, don't paint on a JFrame and in its paint method but rather on a JPanel in its paintComponent method.Any alternatives or suggestions appreciated.
- 12-07-2009, 02:06 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Made some changes.The following was added in the actionperformed method.I set the background color to the JPanel so the GUI not messing up any more.But the draw still not drawing things.I think I am missing something small.
My DrawBoard class:Java Code:if(clickedButton== ovalBtn){ DrawBoard circle= new DrawBoard(200,100); oval=1; rect=0; System.out.println("here"); clickedButton=null; }
Java Code:class DrawBoard extends JPanel{ private int xPosition; private int yPosition; private int width; private int height; public DrawBoard (int x, int y){ xPosition = x; yPosition = y; width = 92; height = 92; } public void draw (Graphics2D g2){ Ellipse2D.Double ring = new Ellipse2D.Double (xPosition, yPosition, width, height); g2.draw(ring); } }
- 12-07-2009, 02:48 PM #4
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Try to change
toJava Code:if(clickedButton== ovalBtn)
Java Code:if(clickedButton.equals("ovalBtn"))
-
- 12-07-2009, 03:04 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Hi
I dont think thats the problem.The if statement has a System.out which does print when I click the Oval btn.For some reason the draw function is not getting accessed.i dont know how to draw.
So about the JComponents PaintComponent where is it to be added in the DrawShapes?
Thanks
shwtea
-
- 12-07-2009, 03:07 PM #8
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
I better be quiet. :o
- 12-07-2009, 05:11 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Yes I have been going through the tutorials but they confused me.
So my basic question is I have my main in one class file and other class file has paintcomponent method etc.I want to call that class:
Just by typing circle = new Circle( x,color,30,50) if circle is instance of Circle class.The Circle.java code is what I am trying to use.Gives me errors like Circle class not found even if the files are in the same folder.Java Code:if(clickedButton== ovalBtn){ //DrawBoard circle= new DrawBoard(200,100); circle= new Circle (3,red,30,50); oval=1; rect=0; System.out.println("here"); clickedButton=null; }
If I do circle. whatever method I should be able to draw correct? like circle.draw()?Java Code:import java.awt.*; import javax.swing.*; public class Circle extends JPanel { private int diameter, x, y; private Color color; //----------------------------------------------------------------- // Constructor: Sets up this circle with the specified values. //----------------------------------------------------------------- public Circle (int size, Color shade, int upperX, int upperY) { diameter = size; color = shade; x = upperX; y = upperY; } //----------------------------------------------------------------- // Draws this circle in the specified graphics context. //----------------------------------------------------------------- public void draw (Graphics page) { page.setColor (color); page.fillOval (x, y, diameter, diameter); } //----------------------------------------------------------------- // Diameter mutator. //----------------------------------------------------------------- public void setDiameter (int size) { diameter = size; } //----------------------------------------------------------------- // Color mutator. //----------------------------------------------------------------- public void setColor (Color shade) { color = shade; } //----------------------------------------------------------------- // X mutator. //----------------------------------------------------------------- public void setX (int upperX) { x = upperX; } //----------------------------------------------------------------- // Y mutator. //----------------------------------------------------------------- public void setY (int upperY) { y = upperY; } //----------------------------------------------------------------- // Diameter accessor. //----------------------------------------------------------------- public int getDiameter () { return diameter; } //----------------------------------------------------------------- // Color accessor. //----------------------------------------------------------------- public Color getColor () { return color; } //----------------------------------------------------------------- // X accessor. //----------------------------------------------------------------- public int getX () { return x; } //----------------------------------------------------------------- // Y accessor. //----------------------------------------------------------------- public int getY () { return y; } }
I am having some issues with basics
Similar Threads
-
Java 2d Graphics Drag and Drop
By shawleigh17 in forum AWT / SwingReplies: 9Last Post: 07-15-2009, 09:27 PM -
Help me with graphics
By 7oclock in forum New To JavaReplies: 12Last Post: 04-04-2009, 11:20 PM -
Want to learn Java Graphics
By loggen in forum New To JavaReplies: 7Last Post: 01-03-2009, 04:15 PM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM -
Problem in printing java graphics
By Mahendra in forum Java 2DReplies: 0Last Post: 01-23-2008, 12:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks