NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

FIG:7.25
##DrawRainbow

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawRainbow extends JPanel {

private final static Color VIOLET = new Color(128, 0, 128);
private final static Color INDIGO = new Color(75, 0, 130);
private Color[] colors = {Color.WHITE, Color.WHITE, VIOLET, INDIGO, Color.BLUE,
Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED};

public DrawRainbow() {
setBackground(Color.WHITE);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = 20;

int centerX = getWidth() / 2;
int centerY = getHeight() - 10;

for (int counter = colors.length; counter > 0; counter--) {
g.setColor(colors[counter - 1]);
g.fillArc(centerX - counter * radius,
centerY - counter * radius,
counter * radius * 2,
counter * radius * 2,
0,
180);
}
}
}



FIG:7.26
##DrawRainbowTest

import javax.swing.JFrame;

public class DrawRainbowTest {

public static void main(String[] args) {
DrawRainbow panel = new DrawRainbow();
JFrame application = new JFrame();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(400, 250);
application.setVisible(true);
}
}


FIG:8.17
##MyLine

import java.awt.Color;
import java.awt.Graphics;

public class MyLine {

private int x1;
private int y1;
private int x2;
private int y2;
private Color color;

public MyLine(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}

public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}


FIG:8.18
##DrawPanel

import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

private SecureRandom randomNumbers = new SecureRandom();
private MyLine[] lines;

public DrawPanel() {
setBackground(Color.WHITE);
lines = new MyLine[5 + randomNumbers.nextInt(5)];

for (int count = 0; count < lines.length; count++) {
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);

Color color = new Color(randomNumbers.nextInt(256),
randomNumbers.nextInt(256),
randomNumbers.nextInt(256));

lines[count] = new MyLine(x1, y1, x2, y2, color);
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

for (MyLine line : lines) {
line.draw(g);
}
}
}


FIG:8.19
##TestDraw


import javax.swing.JFrame;

public class TestDraw {
public static void main(String[] args) {
DrawPanel panel = new DrawPanel();
JFrame app = new JFrame();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.setSize(300, 300);
app.setVisible(true);
}
}



FIG:9.13
##LebelDemo


import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class LabelDemo {

public static void main(String[] args) {
JLabel northLabel = new JLabel("North");
ImageIcon labelIcon = new ImageIcon("GUItip.gif");
JLabel centerLabel = new JLabel(labelIcon);
JLabel southLabel = new JLabel(labelIcon);
southLabel.setText("South");
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(northLabel, BorderLayout.NORTH);
application.add(centerLabel, BorderLayout.CENTER);
application.add(southLabel, BorderLayout.SOUTH);

application.setSize(300, 300);
application.setVisible(true);

}
}



FIG:129
##TextFieldFrame

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class TextFieldFrame extends JFrame {
private final JTextField textField1;
private final JTextField textField2;
private final JTextField textField3;
private final JPasswordField passwordField;

public TextFieldFrame() {
super("Testing JTextField and JPasswordField");
setLayout(new FlowLayout());

textField1 = new JTextField(10);
add(textField1);

textField2 = new JTextField("Enter text here");
add(textField2);

textField3 = new JTextField("Uneditable text field", 21);
textField3.setEditable(false);
add(textField3);

passwordField = new JPasswordField("Hidden text");
add(passwordField);

TextFieldHandler handler = new TextFieldHandler();
textField1.addActionListener(handler);
textField2.addActionListener(handler);
textField3.addActionListener(handler);
passwordField.addActionListener(handler);
}

private class TextFieldHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
String string = "";

if (event.getSource() == textField1)
string = String.format("textField1: %s", event.getActionCommand());
else if (event.getSource() == textField2)
string = String.format("textField2: %s", event.getActionCommand());
else if (event.getSource() == textField3)
string = String.format("textField3: %s", event.getActionCommand());
else if (event.getSource() == passwordField)
string = String.format("passwordField: %s", event.getActionCommand());

JOptionPane.showMessageDialog(null, string);
}
}
}



FIG:12.10
##TextFieldTest

import javax.swing.JFrame;

public class TextFieldTest {

public static void main(String[] args) {
TextFieldFrame textFieldFrame = new TextFieldFrame();
textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textFieldFrame.setSize(350, 100);
textFieldFrame.setVisible(true);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.