drawing smile face using fillArc - java

How do I get full smile face?
I am trying to draw a happy face with full mouth smile,
it is giving me results like a half moon.
How can I get result like the picture?
What code should I use ?
I tried to draw a curved line but im very beginner i could not find a way to fill it.
Here is my code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.*;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Smiley extends JFrame {
// constructor sets window's title bar string and dimensions (full screen)
public Smiley (){
super( "Happy Face" );
setSize( 1660, 1080 );
setVisible( true );
}
public void paint( Graphics g ){
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(7));
// call superclass's paint method
super.paint( g );
g.setColor(Color.yellow);
g.fillOval(500, 200, 400, 400);
g.setColor(Color.white);
g.fillOval(520, 350, 100, 100);
g.fillOval(780, 350, 100, 100);
g2.setPaint(Color.black);
g.drawOval(500, 200, 400, 400);
g.fillArc( 100, 120, 80, 80,180 , 180 );

I tried to draw a curved line but i could not find a way to fill it.
Well you can't fill a single curved line.
I assume you mean you want to fill the white part of the mouth between the two curved lines.
You might be able to use a GeneralPath. It will allow you to combine multiple lines/curves into a single shape. Then you paint the shape and fill it.
So the basic code might be something like:
GeneralPath gp = new GeneralPath();
gp.moveTo(...); // starting point
gp.curveTo(...); // first curve
gp.moveTo(...); // reset back to starting point
gp.curveTo(...); // second curve
g2.setColor( Color.WHITE );
g2.fill( gp );

Related

JFrame Graphics2D.drawline() x-axis origin problem in Java

I created a JFrame. And when I draw a line with x=0, y=0 starting point in the x and y axis with Graphics2D.drawline(), it does not start from the x=0 axis of the JFrame. What should I do? enter image description here
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class MyGraphics extends JFrame{
MyGraphics(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
g2D.setPaint(Color.blue);
g2D.drawLine(0, 0, 500, 500);
g2D.setStroke(new BasicStroke(5));
}
}
It starts from (0,0) but it is covered by top bar of window frame.
To see this, put
this.setUndecorated(true);
as the first line in constructor. It will show a window without top bar.

java how to draw a G-clef and F-clef using points or curves

I am making a music app using Graphics 2D, and I have managed to draw the stave and the music notes. I am now trying to draw a G-clef
and an F-clef
If there is another possible way to do it, I will appreciate.
NB: I have looked around for two days and I have seen questions that are similar but I haven't yet seen a solution.
Using points and curves is not the best way. Thanks to #David, I found out that unicodes work best. To use a unicode in printing a string,
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
public class UnicodeText {
public static void main(String[] args) {
JFrame f = new JFrame() {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Bravura", Font.PLAIN, 32);
g2.setFont(font);
g2.drawString("\uD834\uDD1E", 40, 80);// Gclef
g2.drawString("\uD834\uDD22", 40, 80);// Fclef
}
};
f.setSize(200,200);
f.setVisible(true);
}
}
Download the "bravura" font here and find the unicode standard chart here
Hope it helps someone.

Graphics2d methods produce no output

I've been trying to get the Graphics2d object to work without success. I've searched for an answer on both the Oracle tutorial site and Stackoverflow without finding an answer.
The problem I have is that when I call the methods lineTo, fill, and drawRect, I get a blank grey square in my window, instead of the shapes that I want.
package main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphicsTesting extends JPanel {
private static final long serialVersionUID = 6096199371167913312L;
static BufferedImage buffImag = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
static Graphics2D graff = buffImag.createGraphics();
Point2D.Double point = new Point2D.Double(10, 10);
static Graphics gra = buffImag.createGraphics();
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 4);
gp.moveTo(30, 55);
gp.lineTo(168, 384);
gp.lineTo(462, 81);
gp.lineTo(321, 423);
gp.lineTo(269, 243);
g2.setColor(new Color(112, 150, 134));
g2.fill(gp);
g2.setColor(new Color(56, 112, 232));
g2.draw(gp);
g2.setColor(new Color(152, 1, 210));
g2.drawRect(25, 152, 380, 405);
g2.drawImage(buffImag, 0, 0, 500, 0, 0, 500, 500, 500, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
GraphicsTesting gT = new GraphicsTesting();
frame.setContentPane(gT);
gT.paint(gra);
}
}
Your code seems fine and I tried to run it on my machine. It produces what you can see on the following screen shot. I think this is what you expect to get, right? Your problem might be coming from a faulty Java installation or an os-related issue. Which virtual machine are you using and on which operating system?
As a side note, your code is not complete though, as the following import is missing
import java.awt.geom.Point2D;
There are several issues with your code, but the main one causing your problem is that you are making the frame visible before adding the panel to it.
Move your setVisible(true) line to here:
frame.setContentPane(gT);
frame.setVisible(true);
gT.paint(gra);

How to I make a simple window that I can render to?

I've just finished a 2D vector/transformation library that I want to use on a simple example. I have a main loop that runs efficiently and update/render methods. I've always tried to understand what people are talking about when they use Java2D or jPanels or jFrames, but none of it makes sense to me.
I've made some 2D examples before, but it is using a jFrame with a Threaded canvas that I made when following a youtube tutorial. Its problem is that it is basically an integer array that allows for individual pixel setting and you can only use integers as positions, not floats like my library uses.
so my question is: how would I go about making a simple opening/closing window, that I can draw a sprite (should sprites just be some sort of slickUtil loaded thing, or will I have to load in individual pixels as I did before?) to, and that accepts float values for Cartesian coordinates with the origin at the centre.
Derive a class from JComponent and override the paintComponent method. It gets passed a Graphics object that can be cast into a Graphics2D object. The latter one has support for changing the coordinate system.
For drawing sprites: Loading individual pixels in a loop is very slow. There is a drawImage method in Graphics2D that supports everything you need for blitting sprites.
Here's an example to setup the Graphics2D object with a centered origin in a self-contained example:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Draw2D extends JFrame {
public Draw2D() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new DrawPane(), BorderLayout.CENTER);
pack();
}
public static void main(String[] args) {
Draw2D drawing = new Draw2D();
drawing.setVisible(true);
}
}
class DrawPane extends JComponent {
public DrawPane() {
setPreferredSize(new Dimension(640, 640));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// use anti-aliasing for smooth lines
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// move origin to center
g2.translate(getWidth() / 2, getHeight() / 2);
// scale as you need. Using negative y so that y points upward
// note that non-square window sizes will cause a different aspect ratio,
// you probably want to use Math.min(width, height) or something
g2.scale(getWidth() / 2, -getHeight() / 2);
// set color and thickness
g2.setColor(Color.red);
g2.setStroke(new BasicStroke(0.001f));
// draw coordinate lines
g2.draw(new Line2D.Float(-1f, 0f, 1.0f, 0f));
g2.draw(new Line2D.Float(0, -1.0f, 0.0f, 1.0f));
// draw a vector
g2.draw(new Line2D.Float(0f, 0f, 0.25f, 0.25f));
}
}

Java Swing transparency drawing issues

Edit:
I submitted a bug for the below (it may take a a few days to become approved though):
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7043319
Some more details:
It works with Windows Sun JDK 1.6 versions 13 and 17
It fails on Ubuntu 11.04 x64 with both OpenJDK 1.6.0_22 and Sun JDK 1.6.0_24
What I want is to make a background image panel with additional panels on top of it (with additional components - e.g. JButtons, custom shapes, etc. - in them) and draw all that correctly. I'm using JLayeredPane for that purpose in my app, but for the sake of an example the below code should suffice. I'm open to suggestions about how to do what I want regardless of the below problem.
I'm running into the issue that the painting is behaving really weird. It doesn't repaint fully (e.g. only the top part above the image), it repaints in - from what I've noticed increasingly spaced - steps (e.g. 1st paint, 3rd paint, 9th paint, 21st paint, 64th paint, etc.). My guess is that I'm going too much into implementation here - is there anything obviously wrong with the below?
On a separate note, there are three commented lines below. Interestingly enough, uncommenting any of them and commenting the following line solves the problem. The images are with the following attributes (and it seems it doesn't matter which image - just the size):
cat.jpg JPEG 640x533 640x533+0+0 8-bit DirectClass 110KB 0.000u 0:00.000
cat-small.jpg JPEG 200x167 200x167+0+0 8-bit DirectClass 7.99KB 0.000u 0:00.000
Here's the Java code I'm having issues with:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class SwingDrawingPrb {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame frame = new JFrame("SwingDrawingPrb");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
frame.setLocation(550, 50);
frame.setSize(1000, 800);
frame.setVisible(true);
// ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat-small.jpg"));
ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat.jpg"));
final JPanel imagePanel = new JPanel() {
// Color trans = new Color(255, 0, 0, 255);
Color trans = new Color(255, 0, 0, 64);
protected void paintComponent(Graphics g) {
System.out.println("painting");
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(trans);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.blue);
g.drawLine(0, 0, 1000, 1000);
}
};
imagePanel.setBounds(0, 0, image.getIconWidth() + 200, image.getIconHeight() + 200);
imagePanel.setLayout(null);
// JLabel imageLabel = new JLabel("Hello, world!");
JLabel imageLabel = new JLabel(image);
imageLabel.setBounds(100, 100, image.getIconWidth(), image.getIconHeight());
imageLabel.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved");
imagePanel.repaint();
}
});
imagePanel.add(imageLabel);
contentPane.add(imagePanel);
}
}
You need to add:
imagePanel.setOpaque(false);
See Backgrounds With Transparency for more information.

Categories