Gradually incrementing stroke width in java - java

I have to draw the figure on the link in java as an assignment.The assignment
So far I have been able to draw the first arc using CubicCurve2D, as seen on the picture on the second link, but now I face the problem of incrementing the line width gradually.What I have achieved until now
Can anyone tell me if there is any way of achieving it. Thank you!
This is the code for the panel
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class GraphicsDemo extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.BLACK);
Graphics2D g2D = (Graphics2D) g;
g2D.setPaint(new Color(150,250, 187));
g2D.setStroke(new BasicStroke(1));
//g2D.drawArc(-50,50,300,300,0,90);
CubicCurve2D cubic = new CubicCurve2D.Double(0,230,80,90,130,220,80,190);
g2D.draw(cubic);
}
}
This is the code of the frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame implements MouseListener, MouseMotionListener{
GraphicsDemo graphicDemo = new GraphicsDemo();
int x, y;
String str="";
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Pressed";
System.out.println("Coordinates of x: " + x + " , coordinates of y :" +y);
repaint();
}
public void mouseReleased(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Released";
repaint();
}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Exited";
repaint();
}
// override MouseMotionListener two abstract methods
public void mouseMoved(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Moved";
repaint();
}
public void mouseDragged(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse dragged";
repaint();
}
public Test()
{
this.setSize(420,420);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(graphicDemo);
this.setVisible(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public static void main(String[] a){
new Test();
}
}

Related

Java program to paint shows nothing

Using eclipse, I wrote a program trying to copy mspaint in Windows,there is no errors when build or run, I only complete several functions to test if it can be paint.
I wrote the class named "drawings" to sotre drawings which has been paint,
if I set current=0,it shows nothing ; if I set current=1, a circle shows up but when I release mouse,the circle disappear.
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDraw extends JFrame{
static int current = 0; // choice of different drawings, 0 means pencil
// 1 means circle
static int index=0; // the number of drawings
static int R,G,B; // the color of drawings
static float Stroke=17.0f; // the stroke of drawings
JLabel statusBar = new JLabel();// show the state of mouse
drawings []indexList=new drawings[5000];// store drawings to paint
DrawArea drawArea = new DrawArea();
public MyDraw(){
R=G=B=0;// initialize color
//add components to main window
add(drawArea);
setVisible(true);
setSize(1000,1000);
createNewItem(); // new a drawing
add(statusBar, BorderLayout.SOUTH);
show();
}
// new a drawing
public void createNewItem(){
switch(current){
case 0 :indexList[index]=new pencil();break;
case 1 :indexList[index]=new circle();break;
}
}
public static void main(String args[]){
new MyDraw();
}
// the panel to paint on
class DrawArea extends JPanel{
public DrawArea(){
setBackground(Color.white);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
addMouseListener(new MousePolice1());
addMouseMotionListener(new MousePolice2());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
for(int j=0;j<=index;j++){
pass(indexList[index],g2d);
}
}
public void pass(drawings i,Graphics2D g2d){
i.draw(g2d);
}
}
class MousePolice2 extends MouseAdapter{
public void mouseDragged(MouseEvent e){
statusBar.setText(" Mouse Dragged #:[" + e.getX() +
", " + e.getY() + "]");
if (current == 0) {
indexList[index - 1].x1 = indexList[index].x2 =
indexList[index].x1 = e.getX();
indexList[index - 1].y1 = indexList[index].y2 =
indexList[index].y1 = e.getY();
index++;
createNewItem();
} else {
indexList[index].x2 = e.getX();
indexList[index].y2 = e.getY();
}
repaint();
}
}
class MousePolice1 extends MouseAdapter{
public void mousePressed(MouseEvent e){
statusBar.setText(" Mouse Pressed #:[" + e.getX() +
", " + e.getY() + "]");
indexList[index].x1 = indexList[index].x2 = e.getX();
indexList[index].y1 = indexList[index].y2 = e.getY();
if (current == 0 ) {
indexList[index].x1 = indexList[index].x2 = e.getX();
indexList[index].y1 = indexList[index].y2 = e.getY();
index++;
createNewItem();
}
}
public void mouseReleased(MouseEvent e){
statusBar.setText(" Mouse Released #:[" + e.getX() +
", " + e.getY() + "]");
if (current ==0) {
indexList[index].x1 = e.getX();
indexList[index].y1 = e.getY();
}
indexList[index].x2 = e.getX();
indexList[index].y2 = e.getY();
repaint();
index++;
createNewItem();
}
}
}
//the father
class drawings implements Serializable{
int x1,x2,y1,y2;
void draw(Graphics2D g2d){}
}
// the drawing pencil
class pencil extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1, y1, x2, y2);
}
}
class circle extends drawings
{
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke));
g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
}
}
Didn't I see it or where is the method show()?
Otherwise I just saw some programs which they need to do something like this with the windows:
setVisible(true);

I want to drag one object at a time but this code drags both the objects at a time

I want to drag one object at a time but this code drags both the objects at a time
package javaPgm;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Graphics.*;
public class Drag extends Applet
implements MouseListener, MouseMotionListener {
int width, height;
int x, y; // coordinates of upper left corner of the box
int mx, my; // the most recently recorded mouse coordinates
boolean isMouseDraggingBox = false;
public void init() {
width = getSize().width;
height = getSize().height;
x = width/2 - 20;
y = height/2 - 20;
addMouseListener( this );
addMouseMotionListener( this );
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) {
mx = e.getX();
my = e.getY();
if ( x < mx && mx < x+40 && y < my && my < y+40 ) {
isMouseDraggingBox = true;
}
e.consume();// consumes the current event
}
public void mouseReleased( MouseEvent e ) {
isMouseDraggingBox = false;
e.consume();
}
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent e ) {
if ( isMouseDraggingBox ) {
int new_mx = e.getX();
int new_my = e.getY();
x += new_mx - mx;
y += new_my - my;
mx = new_mx;
my = new_my;
repaint();
e.consume();
}
}
public void paint( Graphics g ) {
g.drawRect( 20, 40, 50, 85 );
g.drawString("circle",20 , 38);
Font f = new Font("TimesRoman", Font.PLAIN, 20);// to set font style and size
g.setFont(f);
g.drawString("RED=Positive charge", 175, 188);// to write a text
g.drawString("BLUE=negative charge", 199, 210);
g.drawRect(100,140,60,85);
g.setColor(Color.red);
g.fillOval(x,y,15,15);
g.setColor(Color.blue);
g.fillOval(x-110,y-135,15,15);
setBackground(Color.yellow);
for(int i=0;i<=950;i+=30){// to draw horizontal and vertical lines
g.setColor(Color.orange);
g.drawLine(i,0,i,950);
}
for(int j=0;j<=950;j+=30){
g.drawLine(0, j, 950, j);
}
}
}
The first thing you need is some way to tell the difference between your "objects".
The next thing you need is some way to reference this objects in a convenient method. You could use an array, but I'm lazy and prefer to use a List
Once you have these two things, painting the boxes is just a matter of looping through the List and determining which object was clicked is becomes basically the same process...
To that end, I wrapped a java.awt.Rectangle around a custom Box class, which also maintains color information, this makes it much easier to manage as Rectangle has some very useful methods and can be painted easily.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class WhatADag extends JApplet {
#Override
public void init() {
add(new DragPane());
}
public class DragPane extends JPanel {
private List<Box> boxes;
public DragPane() {
boxes = new ArrayList<>(25);
boxes.add(new Box(0, 0, 50, 50, Color.RED));
boxes.add(new Box(150, 150, 50, 50, Color.BLUE));
MouseAdapter handler = new MouseAdapter() {
private Box hitBox;
private Point offset;
#Override
public void mousePressed(MouseEvent e) {
Point mp = e.getPoint();
for (Box box : boxes) {
if (box.getBounds().contains(mp)) {
hitBox = box;
offset = new Point();
offset.x = mp.x - box.getBounds().x;
offset.y = mp.y - box.getBounds().y;
}
}
}
#Override
public void mouseReleased(MouseEvent e) {
hitBox = null;
}
#Override
public void mouseDragged(MouseEvent e) {
if (hitBox != null) {
Point mp = e.getPoint();
Rectangle bounds = hitBox.getBounds();
bounds.x = mp.x - offset.x;
bounds.y = mp.y - offset.y;
repaint();
}
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Box rect : boxes) {
g2d.setColor(rect.getColor());
g2d.fill(rect.getBounds());
}
g2d.dispose();
}
}
public class Box {
private Color color;
private Rectangle rectangle;
public Box(int x, int y, int width, int height, Color color) {
rectangle = new Rectangle(x, y, width, height);
setColor(color);
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public Rectangle getBounds() {
return rectangle;
}
}
}
Take a look at Performing Custom Painting and 2D Graphics for more details

Program not polygons

I got a program running and I call this class and well, after coding it it should do stuff when the mouse drags, but it just does nothing, where is the error? I mean it should work based on the code, but it just stares at me with apparent ambivalent contempt, mocking me.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simpleWhiteBoard extends JFrame
{
protected int lastX, lastY;
public simpleWhiteBoard()
{
super("Smile white board");
lastX = 0;
lastY = 0;
Container c = getContentPane();
c.setBackground(Color.white);
addMouseListener(new positionRecorder());
addMouseMotionListener(new LineDrawer());
setBounds(10, 20, 450, 400);
setVisible(true);
}
protected void record(int x, int y)
{
lastX = x;
lastY = y;
}
private class positionRecorder extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
record(e.getX(), e.getY());
}
public void mousePressed(MouseEvent e)
{
record(e.getX(), e.getY());
}
}
private class LineDrawer extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
Graphics g = getGraphics();
Polygon p = new Polygon(Constants.X, Constants.Y, Constants.X.length);
g.setColor(Color.yellow);
g.fillPolygon(p);
g.setColor(Color.red);
p = new Polygon(Constants.X, Constants.Y, Constants.X.length);
}
}
}//finale

Visual marker of mouse clicks using Java

I am creating a screen recorder software using Java. Almost 80% work has been completed. Now I need to create a visual marker of mouse clicks using Java. So that I can see in the playback video that where the mouse has been clicked. How can I do that?
Does anyone have any code example?
Very simple. Read the point where the mouse is clicked by the user with getX() and getY() methods of MouseListener. At the point, draw a oval with drawOval() methods of java.awt.Graphics class. Try the following code which I am sure solves your problem.
import java.awt.*;
import java.awt.event.*;
// no window closing code
public class MouseXY extends Frame implements MouseListener, MouseMotionListener
{
int x , y;
String str =" ";
public MouseXY()
{
setSize(500, 500);
setVisible(true);
addMouseListener(this); // register both the listeners with frame
addMouseMotionListener(this);
} // override the 5 abstract methods of ML
public void mouseEntered(MouseEvent e)
{
setBackground(Color.green);
x = e.getX();
y = e.getY();
str ="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
setBackground(Color.red);
x = e.getX();
y = e.getY();
str ="Mouse Exited";
repaint();
}
public void mouseClicked(MouseEvent e)
{
setBackground(Color.gray);
x = e.getX();
y = e.getY();
str ="Mouse Clicked";
repaint();
}
public void mouseReleased(MouseEvent e)
{
setBackground(Color.blue);
x = e.getX();
y = e.getY();
str ="Mouse Released";
repaint();
}
public void mousePressed(MouseEvent e)
{
setBackground(Color.lightGray);
x = e.getX();
y = e.getY();
str ="Mouse pressed";
repaint();
} // override the 2 abstract methods of MML
public void mouseDragged(MouseEvent e)
{
setBackground(Color.magenta);
x = e.getX();
y = e.getY();
str ="Mouse Dragged";
repaint();
}
public void mouseMoved(MouseEvent e)
{
setBackground(Color.yellow);
x = e.getX();
y = e.getY();
str = "Mouse Moved";
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x , y , 10 , 10);
g.drawString(x +", "+ y , x , y);
g.drawString(str , x , y -10); // to draw the string above y coordinate
}
public static void main(String args[ ])
{
new MouseXY();
}
}

a brush effect in java

I want to achieve the effect of java in the brush, the mouse returned to the point, move the mouse faster, thinner lines.
Here is a program which demonstrates what you are trying to achieve.
This program is not perfect, but should get you started with what you are trying to build.
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class PaintBrush {
JFrame f;
Canvas c;
int x=-1, y=-1;
public PaintBrush() {
f = new JFrame();
f.setSize(600, 400);
c = new Canvas() {
public void paint(Graphics g) {
// super.paint(g);
}
};
f.getContentPane().add(c);
c.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseDragged(MouseEvent e) {
if(x==-1){
x = e.getX();
y = e.getY();
}
int diffx = Math.abs(x - e.getX());
int diffy = Math.abs(y - e.getY());
System.out.println("diffx:"+diffx+"\t"+"diffy:"+diffy);
int speed = (int) Math.sqrt((diffx + diffy));
if(speed>1){
c.getGraphics().fillOval(x, y, 20-speed*2, 20-speed*2);
}else {
c.getGraphics().fillOval(x, y, 20, 20);
}
System.out.print("Speed:"+speed + "\t");
System.out.println("x:"+e.getX());
x = e.getX();
y = e.getY();
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new PaintBrush();
}

Categories