public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {



Similar documents
A very simple car game in Java

Using A Frame for Output

Remote Method Invocation

Java Mouse and Keyboard Methods

CS 335 Lecture 06 Java Programming GUI and Swing

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Image Processing. In this chapter: ImageObserver ColorModel ImageProducer ImageConsumer ImageFilter

How to Convert an Application into an Applet.

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Event-Driven Programming

core 2 Handling Mouse and Keyboard Events

Java Example. A First Java Program. Pixels, Rasters, Sprites, and BitBlts. Exercise #1. Experiencing JAVAphobia?

Simple Line Drawing. Description of the Simple Line Drawing program

Advanced Network Programming Lab using Java. Angelos Stavrou

Graphical User Interfaces

CODE WIDTH HEIGHT ARCHIVE ALIGN VSPACE, HSPACE ALT APPLET APPLET ALIGN IMG

There are some important differences between an applet and a standalone Java application, including the following:

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

java.applet Reference

GUI Components: Part 2

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

AP Computer Science A Picture Lab Student Guide

Lecture VII JAVA SWING GUI TUTORIAL

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Chapter 2 Introduction to Java programming

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Programming with Java GUI components

1 Hour, Closed Notes, Browser open to Java API docs is OK

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Essentials of the Java(TM) Programming Language, Part 1

Fachbereich Informatik und Elektrotechnik Java Applets. Programming in Java. Java Applets. Programming in Java, Helmut Dispert

Block IQ. Marko Boon Jacques Resing

Part 3: GridWorld Classes and Interfaces

Mouse Event Handling (cont.)

The following program is aiming to extract from a simple text file an analysis of the content such as:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Essentials of the Java Programming Language

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Lösningsförslag till tentamen

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Android Programming: 2D Drawing Part 1: Using ondraw

Introduction to the Java Programming Language

The Design and Implementation of Multimedia Software

Lesson 10: Video-Out Interface

Rapid Android Development

Homework/Program #5 Solutions

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Writing ImageJ Plugins A Tutorial

Collections.sort(population); // Método de ordenamiento

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

public class demo1swing extends JFrame implements ActionListener{

Juce Programming Tutorial. Table of Contents

Affdex SDK for Windows!

Experiences with 2-D and 3-D Mathematical Plots on the Java Platform

Construction of classes with classes

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Two-Dimensional Arrays Java Programming 2 Lesson 12

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Software Design: Figures

Dev Articles 05/25/07 11:07:33

Final Project Report E3390 Electronic Circuit Design Lab. Electronic Notepad

Intel Retail Client Manager Audience Analytics

Building Java Programs

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Introduction to Java

Topic 11 Scanner object, conditional execution

Crash Course in Java

History OOP languages Year Language 1967 Simula Smalltalk

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

abstract windows toolkit swing

An Overview of Java. overview-1

Building a Multi-Threaded Web Server

LAB4 Making Classes and Objects

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Interactive Programs and Graphics in Java

Unit Testing & JUnit

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Swing. A Quick Tutorial on Programming Swing Applications

WRITING DATA TO A BINARY FILE

Transcription:

Application.java import javax.swing.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener { public Application() { super("application"); setlayout(new GridLayout(1, 1)); addwindowlistener(this); addmouselistener(this); addmousemotionlistener(this); pendialog = new JDialog(this, "Pen tool properties"); pendialog.setsize(300, 300); pendialog.setlayout(new BorderLayout()); colorchooser = new JColorChooser(); colorchooser.getselectionmodel().addchangelistener(this); pendialog.add(colorchooser, BorderLayout.CENTER); pendialog.setvisible(true); curvedialog = new JDialog(this, "Curve tool properties"); curvedialog.setsize(200, 200); curvedialog.setvisible(true); curvedialog.setlayout(new GridLayout(1, 1)); pen = new Pen(); pen.setposition(100, 100); pen.setcolor(colorchooser.getcolor()); ct = new CurveTool(); ct.setposition(110, 110); paths = new ArrayList(); pathcolors = new ArrayList(); public void statechanged(changeevent e) { Color newcolor = colorchooser.getcolor(); pen.setcolor(newcolor);

public void paint(graphics g) { super.paint(g); // Fetch back buffer's Graphics2D and draw into it Graphics2D g2 = (Graphics2D)getImGraphics(); g2.setcolor(color.white); g2.fillrect( 0, 0, getwidth(), getheight() ); pen.draw(g2); ct.draw(g2); for (int i = 0; i < paths.size(); i++) { g2.setcolor((color)pathcolors.get(i)); g2.draw((generalpath)paths.get(i)); // Now draw the back buffer g.drawimage(image, 0, 0, this); public void windowactivated(windowevent e) { public void windowclosed(windowevent e) { System.exit(0); public void windowclosing(windowevent e) { public void windowdeactivated(windowevent e) { public void windowdeiconified(windowevent e) { public void windowiconified(windowevent e) { public void windowopened(windowevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousepressed(mouseevent e) { if (currentpath == null) { currentpath = new GeneralPath(); paths.add(currentpath); pathcolors.add(pen.getcolor()); currentpath.moveto(e.getx(), e.gety()); public void mousereleased(mouseevent e) { if (currentpath!= null) { currentpath = null; public void mousedragged(mouseevent e) { pen.setposition(e.getx(), e.gety()); repaint(); if (currentpath!= null) { currentpath.lineto(e.getx(), e.gety());

public void mousemoved(mouseevent e) { pen.setposition(e.getx(), e.gety()); repaint(); protected Graphics getimgraphics() { if (image == null) { Dimension dim = getsize(); image = createimage(dim.width, dim.height); img = image.getgraphics(); return img; public static void main(string[] args) { Application app = new Application(); app.setvisible(true); app.setsize(600, 600); private JDialog curvedialog; private JDialog pendialog; private JColorChooser colorchooser; private Pen pen; private CurveTool ct; private Image image = null; private Graphics img = null; private GeneralPath currentpath; private ArrayList paths; private ArrayList pathcolors;

Pen.java import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class Pen implements ImageObserver { public Pen() { ImageIcon iic = new ImageIcon("c:/pen.gif"); img = Transparency.makeColorTransparent(iic.getImage(), new Color(0).white); x = 0; y = 0; public void setcolor(color c) { col = c; public Color getcolor() { return col; public void setposition(int x, int y) { this.x = x; this.y = y; public void draw(graphics2d graphics) { graphics.drawimage(img, AffineTransform.getTranslateInstance(x, y), this); public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return true; private Color col; private int x; private int y; private Image img; // Current color // On-screen position // On-screen position // Image representation

CurveTool.java import java.awt.color; import java.awt.graphics2d; import java.awt.image.*; import java.awt.geom.affinetransform; import javax.swing.imageicon; public class CurveTool implements ImageObserver { public CurveTool() { ImageIcon iic = new ImageIcon("c:/curve.gif"); img = Transparency.makeColorTransparent(iic.getImage(), new Color(0).white); x = 0; y = 0; public void setcolor(color c) { col = c; System.out.println(c); public Color getcolor() { return col; public void setposition(float x, float y) { this.x = x; this.y = y; public void draw(graphics2d graphics) { graphics.drawimage(img, AffineTransform.getTranslateInstance(x, y), this); public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return true; private Color col; // Current color private float x; // On-screen position private float y; // On-screen position private Image img; // Image representation

Transparency.java import java.awt.image.*; public class Transparency { public static Image makecolortransparent (Image im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerrgb = color.getrgb() 0xFF000000; public final int filterrgb(int x, int y, int rgb) { if ( ( rgb 0xFF000000 ) == markerrgb ) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; else { // nothing to do return rgb; ; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip);