I try to create a class that was similar to the famous Processing, but soon I found a problem, since in Processing there are the setup () and draw () functions, in to which it is possible to perform certain functions, such as square (10,10,50), I tried to imitate it by creating a class, in which there is a Graphics object called "gfx" to which the various figures are added. If you read this papyrus ... thank you and I put below the PFrame class (that is the one that imitates Processing) and the BohClass class which will be the test class.
package PFrame.com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PFrame {
private JFrame frame = new JFrame();
private DPane pane = new DPane(); //DPane is a class that extends JPanel
private Graphics2D gfx;
private Color selColor = new Color(255,255,255); //selected color
private boolean fill = true; //the shapes are filled?
public int width = 200, height = 200; //starting frame dimensins
public PFrame() {
//SKY
pane.setPreferredSize(new Dimension(width,height));
//HEAD
title(); //this functions give to the frame the name of the class (in this case "BohClass")
frame.setResizable(false);
//BODY
frame.add(pane);
pane.addComponentListener(new ResizeListener());
//TAIL
size(width,height); //function that setting size
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gfx = (Graphics2D) pane.getGraphics();
pane.paint(); //DPane function
}
//--|METHODS|--//
//FRAME CONFIG
public final void size(int x, int y) {
width = x;
height = y;
pane.setPreferredSize(new Dimension(width,height));
frame.pack();
frame.setLocationRelativeTo(null);
gfx = (Graphics2D) pane.getGraphics();
}
public final void exit() {
System.exit(0);
}
public final void hide() {
frame.setVisible(false);
}
public final void show() {
frame.setVisible(true);
}
public final void location(int XY) {
frame.setLocation(XY,XY);
}
public final void location(int X, int Y) {
frame.setLocation(X,Y);
}
public final void title() {
frame.setTitle((getClass() + "").split(" ")[1]);
}
public final void title(Object title) {
frame.setTitle(title.toString());
}
public final void resizable() {
frame.setResizable(!frame.isResizable());
}
public final void resizable(boolean resizable) {
frame.setResizable(resizable);
}
//2D CONFIG
public final void fill(boolean fill) {
this.fill = fill;
}
public final void color(int RGB) {
selColor = new Color(RGB,RGB,RGB);
}
public final void color(int R, int G, int B) {
selColor = new Color(R,G,B);
}
//2D
public final void square(int X, int Y, int L) {
rect(X,Y,L,L);
}
public final void square(int X, int Y, int L, int A) {
rect(X,Y,L,L,A);
}
public final void square(int X, int Y, int L, int Ax, int Ay) {
rect(X,Y,L,L,Ax,Ay);
}
public final void rect(int X, int Y, int W, int H) {
gfx.setColor(selColor);
if(fill) gfx.fillRect(X, Y, W, H);
else gfx.drawRect(X, Y, W, H);
}
public final void rect(int X, int Y, int W, int H,int A) {
gfx.setColor(selColor);
if(fill) gfx.fillRoundRect(X, Y, W, H, A, A);
else gfx.drawRoundRect(X, Y, W, H, A, A);
}
public final void rect(int X, int Y, int W, int H,int Ax, int Ay) {
gfx.setColor(selColor);
if(fill) gfx.fillRoundRect(X, Y, W, H, Ax, Ay);
else gfx.drawRoundRect(X, Y, W, H, Ax, Ay);
}
public final void circle(int X, int Y, int d) {
ellipse(X,Y,d,d);
}
public final void ellipse(int X, int Y, int W, int H) {
gfx.setColor(selColor);
if(fill) gfx.fillOval(X, Y, W, H);
else gfx.drawOval(X, Y, W, H);
}
public final void triangle(int Ax, int Ay, int Bx, int By, int Cx, int Cy) {
gfx.setColor(selColor);
if(fill) gfx.fillPolygon(new Polygon(new int[] {Ax,Bx,Cx}, new int[] {Ay,By,Cy}, 3));
else gfx.drawPolygon(new Polygon(new int[] {Ax,Bx,Cx}, new int[] {Ay,By,Cy}, 3));
}
//PANEL CONFIG
public final void background(int RGB) {
pane.setBackground(new Color(RGB,RGB,RGB));
}
public final void background(int R, int G, int B) {
pane.setBackground(new Color(R,G,B));
}
public final void clear() {
gfx.clearRect(0, 0, width, height);
}
//PUBLIC METHODS
public void setup() {}
public void loop() {}
//PRIVATE CLASS PANEL
private class DPane extends JPanel {
private static final long serialVersionUID = 57423L;
public void paint() {
setup();
while(true) {
loop();
}
}
}
//LISTENERS
class ResizeListener implements ComponentListener {
public void componentResized(ComponentEvent e) {
width = pane.getWidth();
height = pane.getHeight();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
}
}
and...
import PFrame.com.*;
public class BohClass extends PFrame {
public void setup() {
size(800,600);
background(100);
}
public void loop() {
color(72,28,47);
triangle(60,10,10,60,110,60);
color(255);
square(12,12,123);
}
public static void main(String[] args) {
new BohClass();
}
}
Like others have said, you should not have a while(true) loop inside your painting function.
Shameless self-promotion: here is a tutorial on custom painting in Swing, coming from a Processing background. Basically you need to create a Timer that triggers a repaint.
Related
I am trying to make this program that has two images that move in the straight line and when they read the end of frame, they turn their direction... But the thing is, the images aren't appearing on the screen idk why.. Here is my code for Actor class
public class Actor {
private Image img;
private int x,y,width,height;
private final int RIGHT=1,LEFT=-1;
private byte direction=RIGHT;
public Actor(Image img, int x,int y, int width, int height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public void movement(int frameWidth){
setX(getX()+direction);
if(getX()<0) direction= RIGHT;
if(getX()>(frameWidth-width)) direction= LEFT;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
and here is my main class:
public class game extends JFrame implements Runnable{
private int framewidth=1000;
private int frameheight=1500;
Image image= new ImageIcon("pics/buffy.png").getImage();
Image image2= new ImageIcon("pics/buffythelayer.jpg").getImage();
private Thread thread;
private int picX=100;
private int c=1;
private int xSpeed=3;
private int xFly=1;
private int yFly=100;
private Actor greenCar,pinkCar;
public game(){
setBounds(100,100,framewidth,frameheight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
thread= new Thread(this);
thread.start();
greenCar=new Actor(image,30,70,98,40);
pinkCar=new Actor(image2,400,70,98,40);
}
public void paint(Graphics g){
g.fillRect(xFly, yFly, 10, 10);
g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), null);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), null);
if(c==2){
g.setColor(Color.CYAN);
g.fillOval(100, 200, 150, 200);
}
}
public static void main(String[] args) {
new game();
}
public void run() {
while(true)
{
xFly++;
greenCar.movement(framewidth);
pinkCar.movement(framewidth);
/*if(picX>280){
xSpeed=-xSpeed;
picX=picX+xSpeed;
c=2;
}
if(picX>=100){
xSpeed=3;
picX=picX+xSpeed;
}*/
repaint();
try{
thread.sleep(13);
}
catch(InterruptedException e){
}
}
}
}
I think I see the problem. When you run the code below, you set the last value, the ImageObserver, to null.
g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), null);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), null);
Instead, you should write it like this:
g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), this);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), this);
Therefore, the JFrame is the object that is notified as the image loads and can be drawn on the screen correctly.
If that's not the case, then you should add super.paint(g) to your paint method.
Your paint(g) method should look like this:
public void paint(Graphics g){
super.paint(g);
g.fillRect(xFly, yFly, 10, 10);
g.drawImage(greenCar.getImg(), greenCar.getX(), greenCar.getY(), this);
g.drawImage(pinkCar.getImg(), pinkCar.getX(), pinkCar.getY(), this);
if(c==2){
g.setColor(Color.CYAN);
g.fillOval(100, 200, 150, 200);
}
}
I hope this helps.
The problem is you run thread before you construct the car object, so
creat object first, the run the thread
greenCar=new Actor(image,30,70,98,40);
pinkCar=new Actor(image2,400,70,98,40);
thread.start();
and you forget set image in Actor constructor
public Actor(Image img, int x,int y, int width, int height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.img = img;
}
so i'm new in stackoverflow.
I am about to create a line, a triangle anything, but i'm just focusing on the line and in a good Object orient Programming.
So i create the class Point2D:
package draw;
/**
*
* #author Pedro
*/
public class Point2D {
private int x,y;
// Construtores
public Point2D(){
this(0,0);
}
public Point2D(int x, int y){
this.x=x;
this.y=y;
}
// Set's e Get's
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
Later i create the class line using the class point2D to get the starting point and final point
package draw;
/**
*
* #author Pedro
*/
public class Linha extends Figura{
private Point2D pinicial;
private Point2D pfinal;
//construtores
public Linha(int xinicial, int yinicial, int xfinal, int yfinal){
pinicial=new Point2D(xinicial, yinicial);
pfinal=new Point2D(xfinal, yfinal);
}
public Linha(Point2D pinicial, Point2D pfinal){
this.pinicial=pinicial;
this.pfinal=pfinal;
}
//Get's e Set's
public Point2D getPinicial() {
return pinicial;
}
public void setPinicial(Point2D pinicial) {
this.pinicial = pinicial;
}
public Point2D getPfinal() {
return pfinal;
}
public void setPfinal(Point2D pfinal) {
this.pfinal = pfinal;
}
}
And then i created a Jframe with a button called "line" and put it a panel inside the jFrame that is the place where its going to get the line draw.
The problems is ... I dont know how to draw the line or how should i cal it.
Can you help me?
simply, in your JPanel class ovverride paintComponent() method:
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g)
g.drawLine(x1, y1, x2, y2);
}
}
Where x1, y1, x2, and y2, are the cords of your line.
If you ONLY want it to draw line AFTER the button is pressed, create a global boolean variable, in your main class, and when the button is pressed, set it to true, then when you create your JPanel do:
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
if (myBoolean) {
super.paintComponent(g)
g.drawLine(x1, y1, x2, y2);
}
}
}
Though the advice given in the other answer is too good, but just couldn't stop myself, from adding a word or two to the same, like, in order for the painting to take place, you need to call repaint(), from inside the actionPerformed attached to JButton.
As already stated by #camickr, the use of getPreferredSize() inside the extended class for Drawing, that will provide a valid staging area, where the drawing needs to be done. This example might can help too in this direction.
Moreover, in case you wanted to keep all the lines which have been drawn on the Board so far intact, then you can simply store them in a List and iterate on this List to draw them all again, whenever a new line is to be drawn.
A simple example is as follows:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class LineExample {
private DrawingBoard board;
private JButton drawLineButton;
private Random random;
private ActionListener buttonAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
int width = board.getWidth();
int height = board.getHeight();
Line line = new Line(random.nextInt(width),
random.nextInt(height),
random.nextInt(width),
random.nextInt(height));
board.setValues(line);
}
};
public LineExample() {
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Drawing Lines Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout(5, 5));
board = new DrawingBoard(400, 400);
contentPane.add(board, BorderLayout.CENTER);
drawLineButton = new JButton("LINE");
drawLineButton.addActionListener(buttonAction);
contentPane.add(drawLineButton, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new LineExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class DrawingBoard extends JPanel {
private int width;
private int height;
private List<Line> lines;
public DrawingBoard(int w, int h) {
width = w;
height = h;
lines = new ArrayList<Line>();
}
public void setValues(Line line) {
lines.add(line);
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Line line : lines) {
int xs = line.getXStart();
int ys = line.getYStart();
int xe = line.getXEnd();
int ye = line.getYEnd();
g.drawLine(xs, ys, xe, ye);
}
}
}
class Line {
private Point startPoint;
private Point endPoint;
public Line(int xs, int ys, int xe, int ye) {
startPoint = new Point(xs, ys);
endPoint = new Point(xe, ye);
}
public int getXStart() {
return startPoint.getX();
}
public int getYStart() {
return startPoint.getY();
}
public int getXEnd() {
return endPoint.getX();
}
public int getYEnd() {
return endPoint.getY();
}
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Snooker.paint(Snooker.java:34)
this is error what I've got.
Here is the code:
Main Class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Snooker extends JPanel {
public static final int WIDTH = 900;
public static final int HEIGHT = 450;
public static final Color c = Color.black;
public static final int SIZE_ball = 10;
private Table table;
private Ball ball;
private Cue cue;
public Snooker() {
table = new Table(0,0,c,WIDTH,HEIGHT);
ball = new Ball(150,150,Color.RED,SIZE_ball);
}
public void paint(Graphics g) {
super.paint(g);
table.drawTableOn(g);
ball.drawBallOn(g);
cue.drawCueOn(g);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Snooker");
frame.setLayout(new BorderLayout());
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Snooker game = new Snooker();
frame.add(game);
frame.setVisible(true);
game.requestFocusInWindow();
}
}
Graphics Class:
import java.awt.Color;
import java.awt.Graphics;
public class GraphicsItem {
protected int x,y;
protected Color color;
private static final int SIZE_tableX = 900;
private static final int SIZE_Cue = 30;
public static final int R_ball = 5;
public static int CoorY = 150;
public static int CoorX = 150;
public GraphicsItem(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void moveBallBy(int dx, int dy) {
x += dx;
y += dy;
}
public void drawTableOn(Graphics g) {
g.setColor(color.BLACK);
g.fillRect(x, y, SIZE_tableX, SIZE_tableX/2);
}
public void drawBallOn(Graphics g) {
g.setColor(color.BLACK);
g.fillOval(x,y,R_ball,R_ball);
}
public void drawCueOn(Graphics g) {
g.setColor(color.BLACK);
g.drawLine(x,y,SIZE_Cue,SIZE_Cue);
}
}
Also there are 5 more classes. Cue,Ball,Table and CueBall(extends Ball), BroadCloth(extends Table) . There have just attitude of their objects.
advice to solve?
You have to initialize cue in the constructor of the class Snooker.
Your constructor should be:
public Snooker() {
table = new Table(0,0,c,WIDTH,HEIGHT);
ball = new Ball(150,150,Color.RED,SIZE_ball);
cue = new Cue( ... );
}
As it stands, cue has not been instantiated, and throws a NullPointerException when you try to access its methods.
I'm trying to make a program that shows inheritance and polymorphism. The program is supposed to show a flag (specifically, the flag of Guinea).
This is the code:
import java.awt.*;
import javax.swing.*;
public class FlagB extends JPanel
{
public void paint (Graphics g)
{
Flag Guinea = new Flag(50,290,560);
Guinea.drawFlag(g);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Bolivian Flag");
frame.add(new FlagB());
frame.setSize(1000,725);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Flag
{
private FlagBase fb;
private FitRect rp; //rp for rectangle parameters
private LeftTriColor lt;
private MiddleTriColor mt;
private RightTriColor rt;
private int x;
private int y;
private int z;
public Flag(int x,int y,int z)
{
this.x = x;
this.y = y;
this.z = z;
fb = new FlagBase();
rp = new FitRect(x,y,z);
lt = new LeftTriColor(x,y,z);
mt = new MiddleTriColor(x,y,z);
rt = new RightTriColor(x,y,z);
}
public void drawFlag(Graphics g)
{
fb.FlagBase(g);
lt.drawRect(g);
mt.drawRect(g);
rt.drawRect(g);
}
}
class FlagBase // The outline of the flag
{
public void FlagBase(Graphics g)
{
g.setColor(Color.BLACK);
g.drawRect(49,49,876,562);
}
}
class FitRect
{
protected int l;
protected int length;
protected int width;
public FitRect(int x,int y,int z)
{
x = l;
y = length;
z = width;
}
}
class LeftTriColor extends FitRect
{
public LeftTriColor(int x,int y,int z)
{
super(x,y,z);
}
public void drawRect(Graphics g)
{
g.setColor(Color.RED);
g.fillRect(l,l,length,width);
}
}
class MiddleTriColor extends FitRect
{
public MiddleTriColor(int x,int y,int z)
{
super(x,y,z);
}
public void drawRect(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillRect(l+length+1,l,length,width);
}
}
class RightTriColor extends FitRect
{
public RightTriColor(int x,int y,int z)
{
super(x,y,z);
}
public void drawRect(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(l+2*length+3,l,length+1,width);
}
}
The only thing that shows up in the new window is the flag's outline. I think it has something to do with the argument passing of FitRect. Is that it, or is it something else?
I've been using JCreator.
This should solve it.
class FitRect
{
protected int l;
protected int length;
protected int width;
public FitRect(int x,int y,int z)
{
l = x; //was x = l
length = y; //was y = length
width = z; //was z = width
}
}
I am working on a java 2d game library. I want a method named paintImage() to do graphics.drawImage() every time paintImage() is called.
public void paintImage(image1, x, y){
//i want it to run graphics.drawImage every time it is called.
}
public void anotherMethod(){
paintImage(...);
paintImage(...);
//paint as many times as i want.
}
public void paintComponent(Graphics graphics){
graphics.drawImage();
super.paintComponents();
}
Thanks for your time and please leave a suggestion, sorry but its kind of hard to explain this.
For Single Image Display
public class DrawingDemo {
private JPanel panel;
private MyImage imageData;
public DrawingDemo() {
...
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (imageData != null) {
g.drawImage(imageData.getImage(), imageData.getX(), imageData.getY(), this);
}
}
};
...
}
public void paintImage(Image image1, int x, int y) {
imageData = new MyImage(image1, x, y);
panel.repaint();
}
public void anotherMethod() {
paintImage(...);
paintImage(...);
}
}
public class MyImage { // bean class for storing image information
private Image image;
private int x;
private int y;
public MyImage(Image image, int x, int y) {
this.image = image;
this.x = x;
this.y = y;
}
public Image getImage(){
return image;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
... you can add setter methods
}
UPDATE : For multiple image display
private JPanel panel;
private ArrayList<MyImage> imageData; // or any other data structure you like
public DrawingDemo() {
imageData = new ArrayList<>();
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (MyImage myImage : imageData) {
g.drawImage(myImage.getImage(), myImage.getX(), myImage.getY(), this);
}
}
};
frame.add(panel);
frame.setVisible(true);
}
public void paintImage(Image image1, int x, int y) {
imageData.add(new MyImage(image1, x, y));
panel.repaint();
}
public void anotherMethod() {
paintImage(new ImageIcon("/home/blackadmin/Desktop/image.jpg").getImage(), 0, 0);
paintImage(new ImageIcon("/home/blackadmin/Desktop/image2.jpg").getImage(), 50, 50);
paintImage(new ImageIcon("/home/blackadmin/Desktop/image3.jpg").getImage(), 100, 100);
}
OUTPUT :
Have a look at this answer
Comment if you don't understand anything, hope this will help
What I think you're looking to do is to make changes to some states in your class and then redrawing your images with changes based on those state changes -- in other words perhaps you're looking to do animation. If so, then your image drawing should all be done either within the paintComponent method using its Graphics object, or in another method called by paintComponent one that uses the Graphics object passed into paintCocalzmponent. This can be done by passing a Graphics parameter into the other method. Your anotherMethod would then request that the JVM repaint the GUI by calling repaint(). For example:
public void anotherMethod() {
x++;
y++;
repaint(); // this will stimulate JVM to call paint/paintComponent
}
private void paintImage(Graphics g, BufferedImage img, int x, int y2) {
g.drawImage(img, x, y2, this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
paintImage(g, image1, x, y);
}
A complete example of this is as follows:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.Transient;
import javax.swing.*;
public class PaintEg extends JPanel {
private static final int IMG_W = 30;
private static final int IMG_H = IMG_W;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 20;
private BufferedImage image1;
private int x;
private int y;
public PaintEg() {
image1 = createImg();
new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
anotherMethod();
}
}).start();
}
private BufferedImage createImg() {
BufferedImage img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setBackground(Color.red);
g2.clearRect(0, 0, IMG_W, IMG_H);
g2.setColor(Color.blue);
g2.fillRect(IMG_W / 4, IMG_H / 4, IMG_W / 2, IMG_H / 2);
g2.dispose();
return img;
}
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public void anotherMethod() {
x++;
y++;
repaint(); // this will stimulate JVM to call paint/paintComponent
}
private void paintImage(Graphics g, BufferedImage img, int x, int y2) {
g.drawImage(img, x, y2, this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
paintImage(g, image1, x, y);
}
private static void createAndShowGUI() {
PaintEg paintEg = new PaintEg();
JFrame frame = new JFrame("PaintEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}