Files
2025-06-07 01:59:34 -04:00

168 lines
4.6 KiB
Java

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Color;
/**
* CS2335 Lab 6 Example
*
* <PRE>
* BasicGrpahics.java shows the basics of how to draw simple objects
* <PRE>
*
* @author <A HREF="mailto:tedchoc@cc.gatech.edu">Ted Choc</A>
* @version Version 1.4, February 20, 2003
*/
public class BasicGraphics {
/**
* JFrame to which everything is rendered
*/
private JFrame guiFrame;
/**
* Content Pane of the JFrame
*/
private Container contentPane;
/**
* The panel to which all the graphics will be drawn
*/
private JPanel graphicsPanel;
/**
* A modifier for guiFrame
* @param guiFrame new guiFrame
*/
public void setGuiFrame(JFrame guiFrame) {
this.guiFrame = guiFrame;
}
/**
* A modifier for contentPane
* @param contentPane new contentPane
*/
public void setContentPane(Container contentPane) {
this.contentPane = contentPane;
}
/**
* A modifier for graphicsPanel
* @param graphicsPanel new graphicsPanel
*/
public void setGraphicsPanel(JPanel graphicsPanel) {
this.graphicsPanel = graphicsPanel;
}
/**
* An accessor for guiFrame
* @return JFrame guiFrame
*/
public JFrame getGuiFrame() {
return this.guiFrame;
}
/**
* An accessor for contentPane
* @return Container contentPane
*/
public Container getContentPane() {
return this.contentPane;
}
/**
* An accessor for graphicsPanel
* @return JPanel graphicsPanel
*/
public JPanel getGraphicsPanel() {
return this.graphicsPanel;
}
/**
* Create the window and sets it up for drawing
*/
public void makeWindow() {
guiFrame = new JFrame("Basic Graphics Objects");
/* Handle Closing the Window */
guiFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
contentPane = guiFrame.getContentPane();
graphicsPanel = new JPanel() {
/* Overwrite JPanels paintComponent Method */
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawGraphics(g);
}
};
contentPane.add(graphicsPanel);
guiFrame.setSize(175, 240);
guiFrame.setVisible(true);
graphicsPanel.setBackground(Color.lightGray);
}
/**
* Method that draws the Graphics onto the graphicsPanel
* @param g graphics g object
*/
public void drawGraphics(Graphics g) {
/* Draws some Text */
g.setColor(Color.black);
g.drawString("Hey look, I'm some Text", 15, 15);
/* Draws a fancy 3-D Rectangle */
g.setColor(Color.red);
g.fill3DRect(50, 50, 50, 50, true);
g.setColor(Color.blue);
g.fillRect(60, 60, 30, 30);
/* Draws a line */
g.setColor(Color.yellow);
g.drawLine(50, 120, 130, 140);
/* Draws a circle */
g.setColor(Color.orange);
g.fillOval(140, 60, 20, 20);
/* Draws Complex Shape :) */
g.setColor(Color.red);
g.fillRoundRect(67, 145, 105, 30, 25, 25);
g.fillRoundRect(75, 170, 89, 33, 10, 10);
g.setColor(Color.white);
g.fillOval(72, 150, 18, 18);
g.fillOval(147, 150, 18, 18);
g.setColor(Color.yellow);
g.fillOval(84, 180, 6, 6);
g.fillOval(147, 180, 6, 6);
g.setColor(Color.black);
g.drawOval(72, 150, 18, 18);
g.drawOval(147, 150, 18, 18);
g.drawOval(84, 180, 6, 6);
g.drawOval(147, 180, 6, 6);
g.fillRoundRect(95, 150, 5, 50, 2, 2);
g.fillRoundRect(102, 150, 5, 50, 2, 2);
g.fillRoundRect(109, 150, 5, 50, 2, 2);
g.fillRoundRect(116, 150, 5, 50, 2, 2);
g.fillRoundRect(123, 150, 5, 50, 2, 2);
g.fillRoundRect(130, 150, 5, 50, 2, 2);
g.fillRoundRect(137, 150, 5, 50, 2, 2);
}
/**
* Main method that creates a new BasicGraphics
* @param args Command Line Arguments
*/
public static void main(String[] args) {
BasicGraphics test = new BasicGraphics();
test.makeWindow();
}
}