Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Java / 6

.doc
Скачиваний:
9
Добавлен:
28.03.2016
Размер:
388.1 Кб
Скачать

Міністерство освіти і науки України

ДЕРЖАВНИЙ ВИЩИЙ НАВЧАЛЬНИЙ ЗАКЛАД

«НАЦІОНАЛЬНИЙ ГІРНИЧИЙ УНІВЕРСИТЕТ»

ІНСТИТУТ ЕЛЕКТРОЕНЕРГЕТИКИ

ФАКУЛЬТЕТ ІНФОРМАЦІЙНИХ ТЕХНОЛОГІЙ

Кафедра програмного забезпечення комп'ютерних систем

ЗВІТ

З ЛАБОРАТОРНОЇ РОБОТИ № 6

з дисципліни “Мережеве Java програмування”

Виконав:

ст. гр. КНіт-10-1

Левченко О. А.

Перевірив:

Зінченко О.В.

Харь О. Т.


Дніпропетровськ

2014

ТЕМА: Інтерфейси.

МЕТА РОБОТИ: Освоїти роботу з інтерфейсами.

ЗАВДАННЯ:

В результаті виконання роботи створений Java додаток повинний вивести на екран текстовий рядок, який має містити Ваші прізвище ім’я, по батькові, назву студентської групи та тему цієї роботі. Вивести на екран геометричні фігури, відповідно свого варіанту, використовуючи при цьому об’єктно-орієнтоване програмування та зробити меню.

18. Шестикінечна зірка (DrawPoly), еліпс (Ellipse)

Лістинг програми:

package lab6;

import java.awt.*;

import javax.swing.*;

import java.lang.reflect.*;

import java.util.*;

interface Figure

{

public void draw(Graphics g);

public int getX();

public void setX(int x);

public int getY();

public void setY(int y);

public int getRadius();

public void setRadius(int r);

public int getRadius1();

public void setRadius1(int r1);

public Color getColor();

public void setColor(Color c);

}

/* Poly6 */

class Poly6 implements Figure

{

protected int x, y;

protected Color c = Color.BLACK;

Poly6() {

x = 0;

y = 0;

}

Poly6(int x, int y, Color c) {

this.x = x;

this.y = y;

this.c = c;

}

@Override

public void draw(Graphics g) {

g.setColor(getColor());

g.drawLine(x, y, x+50, y+80);

g.drawLine(x, y, x-50, y+80);

g.drawLine(x-50, y+80, x+50, y+80);

g.drawLine(x-50, y+20, x+50, y+20);

g.drawLine(x-50, y+20, x+5, y+100);

g.drawLine(x+5, y+100, x+50, y+20);

}

@Override

public int getX() { return x; }

@Override

public void setX(int x) { this.x = x; }

@Override

public int getY() { return y; }

@Override

public void setY(int y) { this.y = y; }

@Override

public int getRadius() { return x;}

@Override

public void setRadius(int r) { this.x = x; }

@Override

public int getRadius1() { return x;}

@Override

public void setRadius1(int r) { this.x = x; }

@Override

public Color getColor() { return c; }

@Override

public void setColor(Color c) { this.c = c; }

}

/* Circle */

class Circle implements Figure

{

protected int x, y, r, r1;

protected Color c = Color.BLACK;

Circle() {

x = 0;

y = 0;

r = 0;

r1=0;

}

Circle(int x, int y, int r, int r1, Color c) {

this.x = x;

this.y = y;

this.r = r;

this.r1 = r1;

this.c = c;

}

@Override

public void draw(Graphics g) {

g.setColor(getColor());

g.drawOval(getX(), getY(), getRadius(), getRadius1());

}

@Override

public int getX() { return x; }

@Override

public void setX(int x) { this.x = x; }

@Override

public int getY() { return y; }

@Override

public void setY(int y) { this.y = y; }

@Override

public int getRadius() { return r;}

@Override

public void setRadius(int r) { this.r = r; }

@Override

public int getRadius1() { return r1;}

@Override

public void setRadius1(int r1) { this.r1 = r1; }

@Override

public Color getColor() { return c; }

@Override

public void setColor(Color c) { this.c = c; }

}

/* Poly6Panel */

class Poly6Panel extends JPanel{

private int x, y;

private Color c;

Poly6Panel(int x, int y, Color c){

this.x = x;

this.y = y;

this.c = c;

super.setSize(x+300, y+300);

}

@Override

protected void paintComponent(Graphics g){

Poly6 poly6 = new Poly6(x, y, c);

Graphics2D g2 = (Graphics2D)g;

g2.setStroke(new BasicStroke(3.0f));

super.paintComponents(g);

poly6.draw(g);

g.drawString("Шестиконечная звезда", x-180, 35);

}

}

/* CirclePanel */

class CirclePanel extends JPanel{

private int x, y, r, r1;

private Color c;

CirclePanel(int x, int y, int r, int r1, Color c){

this.x = x;

this.y = y;

this.r = r;

this.r1 = r1;

this.c = c;

super.setSize(x+r+3, y+r1+3);

}

@Override

protected void paintComponent(Graphics g){

Circle cir = new Circle(x, y, r, r1,c);

Graphics2D g2 = (Graphics2D)g;

g2.setStroke(new BasicStroke(3.0f));

super.paintComponents(g2);

cir.draw(g2);

g2.drawString("Эллипс", 25, 35);

}

}

/* Main */

public class MainJFrame extends JFrame {

/**

* Creates new form MainJFrame

*/

private static Poly6Panel p6;

private static CirclePanel pc;

private static Container c;

public MainJFrame() {

initComponents();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screenSize = tk.getScreenSize();

int screenHeight = screenSize.height;

int screenWidth = screenSize.width;

setLocation(screenWidth/3 , screenHeight/3 );

c = getContentPane();

Dimension cs = c.getSize();

c.setLayout(new BorderLayout());

p6 = new Poly6Panel(cs.width/2, cs.height/3, Color.blue);

pc = new CirclePanel(cs.width/3, cs.height/3, 140, 100, Color.blue);

JLabel copyr = new JLabel("Блажко В. А., КНит-10-2, Интерфейсы");

c.add(copyr, BorderLayout.NORTH);

}

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

jMenuItem1 = new javax.swing.JMenuItem();

jMenu2 = new javax.swing.JMenu();

jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();

jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("Лабораторная работа №6");

setResizable(false);

jMenu1.setText("Файл");

jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));

jMenuItem1.setText("Вийти");

jMenuItem1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jMenuItem1ActionPerformed(evt);

}

});

jMenu1.add(jMenuItem1);

jMenuBar1.add(jMenu1);

jMenu2.setText("Выбрать");

jRadioButtonMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, 0));

buttonGroup1.add(jRadioButtonMenuItem1);

jRadioButtonMenuItem1.setText("Шестиконечная звезда");

jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButtonMenuItem1ActionPerformed(evt);

}

});

jMenu2.add(jRadioButtonMenuItem1);

jRadioButtonMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, 0));

buttonGroup1.add(jRadioButtonMenuItem2);

jRadioButtonMenuItem2.setText("Эллипс");

jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButtonMenuItem2ActionPerformed(evt);

}

});

jMenu2.add(jRadioButtonMenuItem2);

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 400, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 279, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

this.dispose();

}

private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

//poly6

lab6.MainJFrame.c.remove(lab6.MainJFrame.pc);

lab6.MainJFrame.c.add(lab6.MainJFrame.p6, BorderLayout.CENTER);

repaint();

}

private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

// circle

lab6.MainJFrame.c.remove(lab6.MainJFrame.p6);

lab6.MainJFrame.c.add(lab6.MainJFrame.pc);

repaint();

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/*

* Set the Nimbus look and feel

*/

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/*

* If Nimbus (introduced in Java SE 6) is not available, stay with the

* default look and feel. For details see

* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

//</editor-fold>

/*

* Create and display the form

*/

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new MainJFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenu jMenu2;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JMenuItem jMenuItem1;

private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;

private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;

// End of variables declaration

}

Результат роботи програми:

Рис.1

Рис.2

Висновок: Виконуючи дану лабораторну роботу, я освоїл роботу з інтерфейсами.

9

Соседние файлы в папке Java