Have large pixels on java graphics - java

I am somewhat new to java, and I'm trying to make a game that has large "pixels" that are actually 5*5 pixels large. I have tried to turn the graphics into an image and then resize the image, but it always returns a huge amount of errors. I am implementing ActionListener and using timer to create a game loop, if that means anything important.
I have the variable screen set as a createVolatileImage(160, 160);
public void actionPerformed(ActionEvent e) {
timer.start();
Graphics draw = screen.getGraphics();
draw.setColor(new Color(50, 100, 50));
draw.fillRect(0, 0, 100, 100);
draw.setColor(Color.black);
draw.drawString("Text",10,10);
draw = getGraphics();
draw.drawImage(screen, 0, 0, 800, 800, 0, 0, 160, 160, null);
draw.dispose();
}

Related

How do I get Java graphic location information?

How do I get Java graphic location information? do I get the location information of the 2D object that I created? I created the object as follows. I need the location information of these objects.
public void paint(Graphics g) {
//backGround
g.setColor(Color.darkGray);
g.fillRect(1, 1, 400, 400);
//left object
g.setColor(Color.green);
g.fillRect(Pxi, Pyi, 20, 20);
//right object
g.setColor(Color.yellow);
g.fillRect(Pxs, Pys, 20, 20);
g.dispose();
}
g.fillRect (Pxs, Pys, 20, 20) I need the location information of these objects.
If you use g.fillRect(x, y, 20, 20) you draw colored rectangle into your screen (I mean Canvas or JPanel) at x and y position. In your case Pxs and Pys coordinates of drawing in user space.I think you should read this to understand more about Java2D API: https://docs.oracle.com/javase/tutorial/2d/overview/coordinate.html

Java,Swing - cannot draw lines on a JFrame

I'm trying to draw vertical lines to separate days in a week on a JFrame. The code seems fine as no error but when I run it, it output a frame like the picture below. Am I missing anything?
public class WeekToView extends JFrame{
public WeekToView(){
setTitle("Sheffield Dental Care"); //set title
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = toolkit.getScreenSize();
setLocation(new Point(screenDimensions.width*1/4, screenDimensions.height*1/4)); //set location based on screen size
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
double size[][] = {{150, 150, 150, 150, 150}, // Columns
{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}; // Rows
container.setLayout(new TableLayout(size));
String daysInWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
JLabel daysInWeekLabels[] = new JLabel[daysInWeek.length];
for (int i = 0; i < daysInWeek.length; i++) {
daysInWeekLabels[i] = new JLabel(daysInWeek[i],SwingConstants.CENTER);
}
container.add(daysInWeekLabels[0], "0,0");
container.add(daysInWeekLabels[1], "1,0");
container.add(daysInWeekLabels[2], "2,0");
container.add(daysInWeekLabels[3], "3,0");
container.add(daysInWeekLabels[4], "4,0");
setSize(780,600); //set size based on screen size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //unresizable
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(getWidth()/5,0,getWidth()/5,getHeight());
g2.drawLine(getWidth()*2/5,0,getWidth()*2/5,getHeight());
g2.drawLine(getWidth()*3/5,0,getWidth()*3/5,getHeight());
g2.drawLine(getWidth()*4/5,0,getWidth()*5/5,getHeight());
}
}
There is no paintComponent() method in a JFrame. Whenever you attempt to override a method you should always use #Override before the method name. You will get a compile error if you don't override the method correctly.
You could override paint() but in general don't try to do custom painting in the paint() method of a JFrame.
Instead custom painting is done by overriding the paintComponent() method of the panel that you add to the frame.
Better yet you can use a JTable, which already provides you with a row/column based component.

Import image in Java

Im making a simple pong game and want to put a texture over my paddle but I don't know how to import image into my game. I tried doing in like here in 3rd example but it doesn't work:
Image myImage = getImage(getCodeBase(), "texture.png");
g.drawImage(myImage, 0, 0 , 10, 120, this);
g cannot be resolved
Here's some code:
public void run(){
Image myImage = getImage(getCodeBase(), "texture.png");
g.drawImage(myImage, 0, 0 , 10, 120, this);
GOval ball = makeBall();
add(ball);
GRect paddleLeft = makePaddle();
GRect paddleRight = makePaddle();
add(paddleLeft);
add(paddleRight);
bounce(ball, paddleLeft, paddleRight);
}
public static GRect makePaddle(){
GRect result = new GRect(0,0,WIDTH,HEIGHT);
result.setFilled(true);
result.setColor(Color.BLACK);
return result;
}
the texture.png is for the paddles
EDIT:
I got the texture to load, but I can't make it move with the paddles, I don't know why
WIDTH is width of the paddle, getWidth() - window. I guess the code I use to move the paddle should work for the texture but it doesnt
with the image.sendToFront() player's paddle's texture works, but the AI's doesn't
if(mouseY<getHeight()-HEIGHT){ // Player
paddleLeft.setLocation(WIDTH,mouseY);
image.setLocation(WIDTH,mouseY);
image.sendToFront();
}
else{
paddleLeft.setLocation(WIDTH,getHeight()-HEIGHT);
image.setLocation(WIDTH,getHeight()-HEIGHT);
image.sendToFront();
}
if(ball.getY()<getHeight()-paddleRight.getHeight()){ // AI
paddleRight.setLocation(getWidth()-2*WIDTH,ball.getY());
image2.setLocation(getWidth()-2*WIDTH,ball.getY());
image2.sendToFront();
}
else
paddleRight.setLocation(getWidth()-2*WIDTH,getHeight()-paddleRight.getHeight());
image2.setLocation(getWidth()-2*WIDTH,getHeight()-paddleRight.getHeight());
image2.sendToFront();
It seems like you are using a library from http://cs.stanford.edu/people/eroberts/jtf/ (based on the GRect class etc).
And it seems like you should be able to use a GImage as well. So the code could roughly look like
Image myImage = getImage(getCodeBase(), "texture.png");
//g.drawImage(myImage, 0, 0 , 10, 120, this);
GImage image = new GImage(myImage);
add(image);

Graphics2D Scaling twice in PaintComponent()

Why does this code output two lines that are the same size?
import java.awt.*;
import javax.swing.*;
public class G2Scale extends JPanel{
public static void main(String args[]) {
G2Scale g = new G2Scale();
g.setPreferredSize(new Dimension(200, 200));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(g);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.scale(0.5, 1.0);
g2.drawLine(5, 50, 100, 50);
g2.setColor(Color.GREEN);
g2.scale(1.0, 1.0);
g2.drawLine(5, 100, 100, 100);
}
}
I would expect these lines to be different sizes because they are scaled differently. From what I am seeing I am thinking that the scale is based off of the previous scale. Am I right about this?
If this is true, how would I get the second line to be scaled to what I thought it should be?
Thanks
All methods you call on a Graphics object that don't output something but instead change a property of it (like setColor, setFont, and so on), are stored in the context of the graphics object. Actually, you should think of a Graphics instance as a graphics context that contains and abstract all the information you need to draw into the screen.
So basically, yes, your second scale is based on the first, since the first one changes the graphics context and the second one acts on top of it.
There're two ways to change this behavior:
Reset the state your Graphics instance by aplying the opposite of your first change (in this case, the inverse scalling).
Make a copy of the Graphics object before applying any context change.
I'm more inclined to the second option, but here're some examples of both:
Graphics2D g2 = (Graphics2D) g;
// resetting the context state
g2.scale(0.5, 1.0);
g2.drawLine(5, 50, 100, 50);
g2.scale(2, 1.0);
// using a copy of the context
// note: casting is mandatory since 'create' returns a Graphics object
Graphics2D g2copy = (Graphics2D)g2.create();
g2copy.scale(1.0, 1.0);
g2copy.drawLine(5, 100, 100, 100);
// this one doesn't have any scale applied
g2.drawLine(5, 150, 100, 150);

Order of rectangles to display and can they be changed?

I working on a project in which I have to simulate a memory manager and show some memory snapshots. I have created a draw class via examples I have found here in which I override the paintComponet(). Everything draws fine.
I would like to be able to draw a rectangle to represent a memory partition and then overlay another rectangle over top to represent an incoming job (ie Job1 is in this partition3). What seems to occur is that I add the partition first (which will always be the case) and then when I add the job it will sit behind the partition block. Is there a way other than drawing the Job first to shift these after the job is created?
Here is the paint override
#Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// set up rendering to allow anti-aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// create the rectangle to represent the memory partition block
// x = address position h = amount of memory (y & w are predefined for the display block)
Rectangle2D rect = new Rectangle2D.Double(x, y, w, h); // create the rectangle
g2d.setPaint(partColor); // set it's color
g2d.fill(rect); // fill it in
// create the transparency for the text
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
g2d.setComposite(comp);
// draw the text with color, type and size and center the text in the block created above
g2d.setPaint(Color.black);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
g2d.drawString(text, (int)((w / 2) - (text.length()/2)), (int)h/2);
}
The call to draw is in my window class (this will place the partition in front of the job) but I need to order to be reversed without changing the order of the calls.
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
snapPanel.add(part1);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
Is there some reason you can't do this?
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
snapPanel.add(part1);
A more general answer would be to add a z component to each of your rectangles. Then you can loop through your rectangles in the paintComponent method, drawing them in z order.

Categories

Resources