Results 1 to 3 of 3
Thread: Writing classes in graphics
- 04-02-2008, 10:26 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 16
- Rep Power
- 0
Writing classes in graphics
Hey,
Have been practising writing classes and having been used passing some parameters through a methd. Now, I am faced with graphics : (
How or what can I pass through in my main for each of the parameters I have defined i.e. Graphics page, int x, int y, int rad, Color color. Obviosuly I know that x, y and rad are intergers but not sure what to pass thru for a graphics page and how to pass thru a color??
Also, I have deliberatley not put a return type in my method but with my new instance in main I am not sure what type to use for the new circle object??
Code:
package Chapter4;
import java.awt.*;
// assumes java.awt.* is imported
public class DrawCircle{
public DrawCircle(){
}
public DrawCircle (Graphics page, int x, int y, int rad, Color color)
{
page.setColor(color);
page.drawOval(x, y, rad, rad);
}
public static void main(String[]args){
DrawCircle circle = new DrawCircle();
newcircle = circle.drawcircle(4,4,4,blue);?????
}
}
- 04-03-2008, 08:39 AM #2
Java Code:import java.awt.*; import javax.swing.*; public class DrawingGraphics { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new GraphicComponent()); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); } } class GraphicComponent extends JPanel { DrawCircle drawCircle = new DrawCircle(); // Java supplies a graphics context which becomes available // (non–null) when/after the component becomes visible. protected void paintComponent(Graphics g) { // Where does the graphics context it come from? System.out.println("g = " + g.getClass().getName()); super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Now we have a real, live graphics context that we // can use for custom drawing/graphics. // Draw a resizable, centered circle: int w = getWidth(); int h = getHeight(); int diameter = 2*Math.min(w, h)/3; int x = (w - diameter)/2; int y = (h - diameter)/2; drawCircle.drawCircle(g, x, y, diameter, Color.red); } } class DrawCircle { public DrawCircle() { } public void drawCircle (Graphics page, int x, int y, int rad, Color color) { page.setColor(color); page.drawOval(x, y, rad, rad); } public static void main(String[] args){ final DrawCircle circle = new DrawCircle(); JPanel panel = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); circle.drawCircle(g,25,25,50,Color.blue); } }; panel.setPreferredSize(new Dimension(100,100)); JOptionPane.showMessageDialog(null, panel, "test", -1); } }
- 04-05-2008, 05:37 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Classes in graphics
By CyberFrog in forum New To JavaReplies: 0Last Post: 04-02-2008, 09:11 PM -
Graphics
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-25-2008, 06:24 PM -
graphics
By Joe2003 in forum Advanced JavaReplies: 4Last Post: 01-18-2008, 07:44 PM -
Graphics
By feniger in forum New To JavaReplies: 1Last Post: 12-29-2007, 04:22 PM -
Updating Graphics
By Greedful in forum Java 2DReplies: 2Last Post: 07-20-2007, 07:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks