Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to creating graphics interfaces using java swing. It covers the basics of using jframe and jpanel components, as well as creating custom components by extending jpanel and overriding the paintcomponent() method. The document also touches upon the use of graphics and graphics2d classes for drawing shapes and animating components.
Typology: Study notes
1 / 10
import javax.swing.*;public static void main (String[] args) {public static void main (String[] args) {
JOptionPane.showMessageDialog (null, "Hello World!"); } // end method main import javax.swing.*; public static void main (String[] args) {
String filename = JOptionPane.showInputDialog (null,
"Enter filename:");
System.out.println(filename); } //
d^
h^
d^
i
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
// end method main
Ti l B
JF
Ti
tleBar
JFrame
JP
l C
P
MenuBar
JPanel ContentPane
JComponent
JComponent
Position determined by:
JComponent
Position determined by:- order in list- layout type
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
import javax.swing.*;… JFrame frame = new JFrame();
// Step 1: Make a window frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel contentPane = (JPanel) frame.getContentPane();JLabel label = new JLabel("Hello World!"); // Step 2: Make componentscontentPane.add(label);
// Step 3: Nest components
frame.setSize(300,300);
// Step 4: Display frame
frame.setVisible(true);
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
z^
z^
z^
import java.awt.*;
// Old library classes, you still need them
import javax.swing.*;
// New library classes, Swing extends AWT
class MyComponent extends Jpanel {
public void paintComponent (Graphics g) {
// your code to tell “g” how to “paint” your object goes here } // end method paintComponent
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
g.setColor(Color.red);
// Dip the paintbrush in red//
g.fillRect(20,50,100,100);
// Paint a
100x100 rectangle @ (20,50)
} // end method paintComponentpublic void paintComponent (Graphics g) {
Image image
new ImageIcon("rose
jpg") getImage();
Image image = new ImageIcon("rose.jpg").getImage();g.drawImage(image,2,2,this); } // end method paintComponent
bli
id
i^
tC
t (G
hi
public void paintComponent (Graphics g) {
g.setColor(Color.black);
// set color black
g.fillRect(0,0,this.getWidth(),this.getHeight()); // paint backgroundint red =
(int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
g int blue =
(int) (Math.random() * 255);
Color randomColor = new Color (red, green, blue);g.setColor(randomColor);
// set RGB color randomly
g.fillOval(70,70,100,100);
// paint 100x100 oval @ (70,70)
} // end method paintComponent
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
// end method paintComponent
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
import javax.swing.*;public class Main {
public static void pause () {
try {
Thread.sleep (5);
// wait 5ms
} catch (Exception e) {
e.printStackTrace (); }} } // end method pausepublic static void main (String[] args) {
JFrame frame = new JFrame();frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);frame.setSize (300,300);frame.setVisible (true);BouncingBallPanel ballPanel = new BouncingBallPanel();frame.getContentPane ().add (ballpanel);while (true) {while (true) {
pause();ballPanel.move();ballPanel.repaint(); } } //
d^
h d
i
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
// end method main
} // end class Main
import javax.swing.;import java.awt.;public class BouncingBallPanel extends JPanel {
int x = 100; int y = 100; int ballSize = 20; int run = 1; int rise = 3;public void paintComponent (Graphics g) {
g.setColor(Color.black);g^
fillRect (
getWidth() getHeight());
g.fillRect (0,0,getWidth(),getHeight());g.setColor(Color.red);g.fillOval(x,y,ballSize,ballSize);
} // method paintComponent (required!)public void move () {
if (x < 0 || x > getWidth() -
ballSize) {
run = -run; } if (y < 0 || y > getHeight() - ballSize) {
rise =
-rise;
rise
rise;
} x += run;y += rise;
} // end method move } //
d^
l^
i^
ll
l
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
// end class BouncingBallPanel
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II